My log4j2 log file always have double output of each line - java

I have seen a lot of posts about duplicate entries on log using log4j2. The solution seems to be add the additivity attribute and set it to false, but it seems is not working for me.
Here's my log4j2.xml
<Configuration status="DEBUG">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" additivity="false">
<PatternLayout pattern="%d{dd-MM-yyyy HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="File" fileName="C:\Laguna\Logs\Laguna.log">
<PatternLayout pattern="%d{dd-MM-yyy HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="off">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
<Logger name="br.com.flutuante.laguna" level="trace" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Logger>
</Loggers>
</Configuration>
I'm having duplicate logs in both console and file logs.
I have tried add additivity="false" everywhere and even removed ROOT entry but can't get it right.
Can anyone help me?
Maybe i'm coding the class wrong?
I'm doing this to get the logger object...
static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(Laguna.class.getName());
and using it like this...
logger.info("Arquivo de configuraĆ§Ć£o carregado");

It might be because you are missing a Root logger, try adding one with level="OFF" if you don't want any other logs.
Edit: Not sure if this is what you want, it should turn off all logging except the specified loggers:
<Logger name="br.com.flutuante.laguna" level="trace"/>
<Root level="off">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
or just change the level in Root to set a default logging level while setting the specified logger to trace

Related

log4j child class not overriding root class configuration

Can someone give me pointers on below. I tought the child class level (trace) should override the root class level (warn), but I only see the warn and above logs.
I tought the child class level (trace) should override the root class level (warn). but I only see the warn and above logs.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="30">
<Appenders>
<Console name="StdOut" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %msg%n" />
</Console>
<File name="FileAppender" filename="LogOutput.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="FileAppender" />
</Root>
<!-- Uncomment the Logger definition below to have ONLY warn-level messages
and above be directed to the console -->
<Logger name="com.fdmgroup.logging.Demo" >
<AppenderRef ref="StdOut" level="trace"/>
</Logger>
<Logger name="com.fdmgroup.exercise.Runner" >
<AppenderRef ref="StdOut" level="trace"/>
<AppenderRef ref="FileAppender" level="trace"/>
</Logger>
</Loggers>
</Configuration>
You didn't set a TRACE threshold on the logger, but on the appender reference. Hence your "com.fdmgroup.logging.Demo" logger has a:
WARN threshold for the logger (inherited from the root logger),
a TRACE threshold for the "StdOut" appender,
no threshold for the "FileAppender" inherited from the root logger (the default value for additivity is `true).
A message is logged only if it not rejected by filter on the logger and the appender, hence only WARN messages are logged.
You probably wanted to set the level for loggers, not appender refs:
<Loggers>
<Root level="WARN">
<AppenderRef ref="FileAppender" />
</Root>
<Logger name="com.fdmgroup.logging.Demo" additivity="false" level="TRACE">
<AppenderRef ref="StdOut"/>
</Logger>
<Logger name="com.fdmgroup.exercise.Runner" level="TRACE">
<AppenderRef ref="StdOut"/>
</Logger>
</Loggers>

Log4J different log level for each package in log4j2.xml

I have a java web app with log4j2.xml
I need to have a different level for each package
for example:
com.myexample.firstmodule.* this should be with INFO level
com.myexample.secondmodule.* this should be with TRACE level
I found some answers telling how to do that in .propreties file, but I couldn't understand how to do it with .xml
Anyone can help with that, please?
Thanks in advance
The log4j website contains detailed information about how to do this, but here's the basic example of an xml configuration they give:
<?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>
<Logger name="com.foo.Bar" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
As you can see, the Loggers section can contain a Logger tag, in which you can define the level for any package you want.
Try this
<logger name="com.myexample.firstmodule" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE-AUDIT" />
</logger>
<logger name="com.myexample.secondmodule" level="TRACE" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE-AUDIT" />
</logger>
in my config STDOUT stands for console logger and FILE-AUDIT for file logger.

Log4j2 : RollingFile appender does not write logs into file

My log4j2.xml is as follows :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Async name="ASYNC">
<AppenderRef ref="R"/>
</Async>
<RollingFile name="R" fileName="${sys:catalina.home}/logs/myServer.log" filePattern="${sys:catalina.home}/logs/myServer.log.%d{yyyy-MM-dd}">
<PatternLayout pattern="%d %-5p [%t] %C{2} - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.ps" level="WARN" additivity="false">
<AppenderRef ref="ASYNC"/>
</Logger>
<Root level="WARN">
<AppenderRef ref="ASYNC"/>
</Root>
</Loggers>
</Configuration>
This configuration only creates myServer.log file and does not roll file also does not write logs into it.
Log file is created with permissions :
ls -ltr my*
-rw-------. 1 root root 0 Feb 14 18:15 myServer.log
What I am doing wrong?
You should add reference to your Rolling file Appender to get logs written in your File.
<Root level="WARN">
<AppenderRef ref="ASYNC"/>
<AppenderRef ref="R"/>
</Root>
More information on configuring log4j2 can be found here
Add <AppenderRef ref="R"/> as well under <Root level="WARN"> tag and try again.

Apache Log4j2 package specific logging using log4j2.xml

I am using log4j2. But the problem that I am facing is that it logs all logs. I want to ... log from specific package to a specific file & other package to another file. I am using log4j2.xml for configuration.
Please can someone help?
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Loggers>
<Root level="DEBUG" additivity="false">
<AppenderRef level="DEBUG" ref="CONSOLE" />
<AppenderRef level="DEBUG" ref="fileAppender" />
</Root>
<Logger name="com.pkg.test.logging.method" level="DEBUG"
additivity="false">
<Appender-ref ref="fileAppender" level="DEBUG" />
</Logger>
</Loggers>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n%throwable}" />
</Console>
<RollingFile name="fileAppender" fileName="./log.log"
filePattern="./log-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
</Appenders>
Just answered the question.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Loggers>
<Root level="DEBUG" additivity="false">
<AppenderRef level="DEBUG" ref="CONSOLE" />
</Root>
<Logger name="com.pkg.test.logging.method" level="DEBUG"
additivity="false">
<Appender-ref ref="fileAppender" level="DEBUG" />
</Logger>
</Loggers>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n%throwable}" />
</Console>
<RollingFile name="fileAppender" fileName="./log.log"
filePattern="./log-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
</Appenders>
Removed the <AppenderRef level="DEBUG" ref="fileAppender" /> from root logger. Thus it started logging logs based on packages.
From Log4J Manual:
Adding a specific logger for a class: (you can refer to packages here too)
<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
<AppenderRef ref="File"/>
</Logger>
Adding a specific appender:
<Appender type="File" name="File" fileName="${filename}">
<Layout type="PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</Layout>
</Appender>
The <Loggers> section of the config looks correct. But, if all other logs not belonging to the expected package is also getting logged, it is the Root logger which is responsible for these logs, and not the package level logger. It may not be active because of the way you are creating the logger instance in your code. E.g: If one is using slf4j to create the logger instance, then this is how we would need to create the logger instance:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Logger logger = LoggerFactory.getLogger(Test.class);
If one gets the logger instance using - Logger logger = LoggerFactory.getLogger("Test") then, the config will not work as expected.
Also, another answer mentioned that the solution was to remove the fileAppender that the package logger was using from the root logger. This, in fact, is not a problem. You are free to use the same appenders in root and package level logger. My answer in context of log4j-api 2.14.1

Log4j 2 logging in the wrong appender

I'm working with log4j 2, version 2.4 on Jboss 6.4, my log4j2.xml is in WEB-INF/classes placed. I can deploy my war without errors about my log4j config, and I can log properly in my RollingFile name="myLog", but something I can't understand is why appear the info's logs for "myLog" in my console appender
private static final Logger loggerBatch = LogManager.getLogger("my.test");
loggerBatch.info("test log");
Config:
<?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>
<RollingFile name="myLog" fileName="C:\\Workarea\\my.log"
filePattern="C:\\Workarea\\myLog-%d{dd-MM-yyyy}-%i.log"
append="true">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5MB"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.test">
<AppenderRef ref="myLog" level ="info"/>
</Logger>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
In addition to Andrey's answer you have declared 3 root loggers. There can only be one. Which one of the definitions will win is undefined.
Check your additivity settings. This will fix the issue
<Logger name="my.test" additivity="false">

Categories