Log4j2 - create new log file - java

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

Related

Turning off Log4j2 by setting Root level to "off" doesnt work

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?

No console output using log4j2 and slf4j

am I missing something using this configuration? I thought that root logger would log all events from error level to the bottom one. If I try to log event at info level, there is no output. This config gives me only error leveled logs. I am using slf4j to create logger objects through Logger-factory class.
<?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>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
For info level logs, change the configuration, in particular Root level to info as show below
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
By changing level to info, the program will start logging both info, error logs in the console.

Change Quartz appender in log4j2

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.

No logs created unless it tests

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.

How to use log4j2 file appender feature in unix environment

I am new to unix. I have below log4j2 config file. When I use this file in unix environment, the file app.log is not created. Could any one tell me how to redirect the logs to file in unix environment
<?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} %method - %msg%n" />
</Console>
<File name="file" fileName="C:\Users\ppatrana\Desktop\app.log">
<!-- <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern> -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} %method - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="CONSOLE" />
</Root>
<Root level="trace">
<AppenderRef ref="file" />
</Root>
</Loggers>
</Configuration>
You have to change the filename :
<File name="file" fileName="/tmp/app.log">
More about linux filesystem : http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard
I think your real problem is that you have two root loggers. Try the following instead:
<Loggers>
<Root level="trace">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="file" />
</Root>
</Loggers>
You also need to fix the filename as mentioned by #ToYonos.

Categories

Resources