I have a problem with logging within Junit tests.
In log4j2.xml, I have changed <Root level="info"> to <Root level="off">:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5p %c{1.} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="off">
<AppenderRef ref="STDOUT"/>
</Root>
<!-- hibernate debugging -->
<Logger name="org.hibernate.SQL" level="DEBUG">
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="org.hibernate.type" level="TRACE">
<AppenderRef ref="STDOUT" />
</Logger>
</Loggers>
</Configuration>
But it doesn't help, I still get the same amount of logs. I need to make the logs dissapear to make progress in an test performance investigation, to be able to run my own allways despised System.out.println() in some of the tests. How can I achive that?
Related
I'm trying to redirect all Quartz logging to a separate file, but it still keeps logging into the console. What am I doing wrong in the configuration file?
Here's a simplified version of my log4j2.xml
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<!--stuff-->
</Console>
<RollingFile name="Quartz">
<!--stuff-->
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.quartz" level="ALL">
<AppenderRef ref="Quartz"/>
</Logger>
<Logger name="com.rotoplastyc" level="ALL">
<AppenderRef ref="Console" />
</Logger>
<Root level="OFF">
</Root>
</Loggers>
</Configuration>
Found out that, as #teppic suggested in the comments, I needed the log4j-slf4j-impl lib in order for it to work properly, I was currently using the slf4j-simple lib which only logs into the console.
I have configuration for save logs to file, but logs are append to existing content. I want to create always new file. How can I do that?
My log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="File" fileName="out.log">
<PatternLayout pattern="[%d{ISO8601} %-5level] %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="File"/>
</Root>
<Root level="info">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
The FileAppender has a property named append which has a default value of true, so configure it like this:
<File name="File" fileName="out.log" append="false">
Documentation can be found at https://logging.apache.org/log4j/2.x/manual/appenders.html#FileAppender
I'm using log4j2 for logging in my app. Basically it's logging uncatched exceptions (Error.log) and logging changes of my data at service layer (journal.log; journalJSON.log).
And here's the thing, when I'm starting service layer tests every log appears in the file and console, but when I'm using application deployed in tomcat they're only in console. What's happening?
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="Error" fileName="logs/error.log"
immediateFlush="true" append="true">
<PatternLayout pattern=" \n\n %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<RandomAccessFile name="Journal" fileName="logs/journal.log" immediateFlush="true" append="true">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %msg%n"/>
</RandomAccessFile>
<File name="JournalJSON" fileName="logs/journalJSON.log" immediateFlush="true" append="false">
<JSONLayout complete="true" charset="UTF-8" compact="false" eventEol="false"/>
</File>
</appenders>
<Loggers>
<Logger name="com.user.controller" level="error" additivity="false">
<AppenderRef ref="Error" level="error"/>
<AppenderRef ref="Console" level="error"/>
</Logger>
<Logger name="com.user.service" level="info" additivity="false">
<appender-ref ref="JournalJSON" level="info"/>
<appender-ref ref="Journal" level="info"/>
<appender-ref ref="Console" level="info"/>
</Logger>
<Root level="warn">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
Well, there are a couple of possibilities I can think of:
Your configuration file isn't being found and it is using the default configuration.
The logger for your application isn't com.user.controller or com.user.service and the root logger is being used.
I'm new to java, and I've written a simple java program to test a few things out before attempting the same things with the more complicated app that I'm almost ready to deploy. I'm using Eclipse, and I've exported the package as a jar file. When I run the jar file, everything appears to be working correctly. Log4j2 is logging properly to the console, and the application is doing what I want it to. The problem I'm having is that the log files are not being created / written to. This all works correctly when I'm running the app from Eclipse. What I was expecting is that I could move the .jar file anywhere I wanted. That I could then execute the app and a logs folder would be created in that same directory and a trace log and an error log would be created and written to.
Here is the log4j2.xml that I'm using:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<RollingFile name="trace-log" fileName="${log-path}/logging-trace.log" filePattern="${log-path}/logging-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/logging-error.log" filePattern="${log-path}/logging-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
<Logger name="multithreadTest" level="info" additivity="false">
<appender-ref ref="trace-log" level="debug"/>
<appender-ref ref="error-log" level="error"/>
<appender-ref ref="console-log" level="debug"/>
</Logger>
</Loggers>
</Configuration>
Here are the contents of the manifest file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ log4j-api-2.5.jar log4j-core-2.5.jar
Class-Path: .
Rsrc-Main-Class: multithreadTest.RunTest
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
I'm executing the application like this:
java -jar mtest.jar
Any help would be greatly appreciated.
add ref to all your loogers
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
<AppenderRef ref="trace-log"/>
<AppenderRef ref="error-log"/>
</Root>
Thanks dom farr. I changed this:
<Logger name="multithreadTest" level="info" additivity="false">
<appender-ref ref="trace-log" level="debug"/>
<appender-ref ref="error-log" level="error"/>
<appender-ref ref="console-log" level="debug"/>
</Logger>
to this:
<Logger name="multithreadTest" level="info" additivity="false">
<AppenderRef ref="trace-log" level="debug"/>
<AppenderRef ref="error-log" level="error"/>
<AppenderRef ref="console-log" level="debug"/>
</Logger>
and it is now working correctly. I'm not sure why. I guess I need to read more about the difference between the two. Not sure why it would work correctly in Eclipse if this is incorrect XML. Anyway, it is working now. Thanks again.
I have two appenders. One is attached to the console and one is to a log file. But that's not really that important. The issue I have is I want everything to go to the file at INFO level. The console is different however. There a bunch of loggers I don't want touching the console at WARN or INFO level because they spray a lot more information than the user needs.
So lets say I have three loggers A, B, C. A, B, and C should all go to the file appenders at INFO level. A should go to the console for INFO, B should go to the console for WARN and C should go to the console for ERROR. What does a log4j config to do this look like?
Something like this should work:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT" ignoreExceptions="false">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="A" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="info" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Logger name="B" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="warn" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Logger name="C" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="error" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Root level="trace">
<AppenderRef ref="STDOUT" level="trace" />
<AppenderRef ref="RollingFile" level="trace"/>
</Root>
</Loggers>