Log4j displays messages with incorrect level - java

I am looking for a logger configuration that outputs everything from some package to file and WARN and higher messages to console. To implement this i wrote following log4j.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<appender name="DIAGAPPDENDER" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${rpt.trace}/diagnostic.log"/>
</appender>
<appender name="ROOTAPPENDER" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
</appender>
<logger name="rpt.diagnostic">
<level value ="ALL" />
<appender-ref ref="DIAGAPPDENDER"/>
</logger>
<root>
<priority value ="WARN" />
<appender-ref ref="ROOTAPPENDER" />
</root>
However, log messages of all levels appears not only in file but also in console which is not what i need. What i am missing?
Thanks. Paul.

Its easy . You can add param threshold to your console appender and set any level where the message of level value equal or above that will be displayed .
<?xml version="1.0" encoding="UTF-8" ?>
<appender name="DIAGAPPDENDER" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${rpt.trace}/diagnostic.log"/>
</appender>
<appender name="ROOTAPPENDER" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Threshold" value="WARN" />
</appender>
<logger name="rpt.diagnostic">
<level value ="ALL" />
<appender-ref ref="DIAGAPPDENDER"/>
</logger>
<root>
<priority value ="WARN" />
<appender-ref ref="ROOTAPPENDER" />
</root>

Related

log4j.xml configuration is not storing logs to file

I have the following log4j.xml configuration file, but don't see the log data stored in the file specified (in my documents folder: Documents/debug.log).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">
<appender name="applicationLogFile" class="org.apache.log4j.RollingFileAppender">
<param name="DatePattern" value="'.'yyyyMMdd'.log'" />
<param name="File" value="Documents/debug.log" />
<param name="threshold" value="TRACE" />
<param name="MaxNumberOfDays" value="90"/>
<param name="CompressBackups" value="TRUE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c] %m%n" />
</layout>
</appender>
<logger name="APP_VERSION_LOGGER">
<appender-ref ref="applicationVersionFile" />
</logger>
<logger name="org.springframework">
<level value="WARN" />
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="applicationLogFile" />
<appender-ref ref="bmcAlertFile" />
</root>
</log4j:configuration>
How an I get my log data sent to my Documents directory on my computer? I'm using log4j-core version 2.13.3. And yes, I've looke at the docs, but I still don't see where my log data is going.
Your line here says:
<param name="File" value="Documents/debug.log" />
It will make a folder with name Documents on your project root level and logs will be there.
I f you want to put them in to specific location on your machine then you need to provide full pathname.
ex.
<param name="File" value="/Users/myUser/Documents/debug.log" />

How to log specific class with different levels to different appenders with LOG4J

I have a situation where I have 2 appenders, one writing to console, another to file. I'd like to log everything with DEBUG level except for one class.
For this class I want to log DEBUG to console and ERROR to file.
I figured out that this kind of filtering is done by category tag, however I haven't managed to split different levels to different appenders on a specific class.
is there a way to do it?
Current configuration is the following :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
</layout>
</appender>
<appender name="file-bl" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="append" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="log/server-bl2.log.%d{yyyy-MM-dd}.gz"/>
<param name="ActiveFileName" value="log/server-bl2.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
</layout>
</appender>
<logger name="org">
<level value="DEBUG"/>
<appender-ref ref="console"/>
<appender-ref ref="file-bl"/>
</logger>
<logger name="org.springframework" additivity="false">
<level value="ERROR"/>
<appender-ref ref="file-bl"/>
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="console" />
<appender-ref ref="file-bl" />
</root>
</log4j:configuration>
Ok, so in the end I came up with a solution like this :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
</layout>
</appender>
<appender name="file-bl" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="append" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="log/server-bl.log.%d{yyyy-MM-dd}.gz"/>
<param name="ActiveFileName" value="log/server-bl.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
</layout>
</appender>
<logger name="org.springframework" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="console"/>
</logger>
<logger name="org.mybatis" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="console"/>
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="console" />
<appender-ref ref="file-bl"/>
</root>
</log4j:configuration>
The root level of logging is DEBUG and there a 2 appenders : console and file-bl. Then, there 2 exceptions : org.springframework and org.mybatis.
For them only 1 appender applies - console.
Though, it's not an exact solution to the problem I described in the beginning since I don't log org.springframework and org.mybatis at all.

Turn off Console Messages docx4j

Docx4j gives me a bunch of messages like this
[AWT-EventQueue-0] INFO org.docx4j.model.listnumbering.Emulator -
How to turn that off?
The following log configuration gets loaded, but doesnt turns the logging off.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value ="OFF" />
</root>
</log4j:configuration>
Add the following line to your configuration:
<logger name="org.docx4j.model.listnumbering.Emulator">
<level value="ERROR" />
<appender-ref ref="console"/>
</logger>
This will configure log4j to log only ERROR messages originating from logger org.docx4j.model.listnumbering.Emulator. If you want to turn off INFO messages from all classes/packages under org.docx4j, use the following:
<logger name="org.docx4j" >
<level value="ERROR" />
<appender-ref ref="console"/>
</logger>
More information here : http://www.javabeat.net/baisc-steps-to-configure-log4j-using-xml-and-properties-file/
I'm using docx4j 8.3.2, logged by slf4j 1.7.30 + logback 1.2.3.
The annoying log messages disappeared after adding a logback.xml under src/main/resources. The logback.xml content:
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %line - %msg%n
</pattern>
</encoder>
</appender>
<!-- docx4j loggers -->
<logger name="org.docx4j" level="ERROR" />
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>

“log4j:configuration” must match “(renderer….loggerFactory)?)”

getting error, when deploying my war file to tomcat:
log4j:WARN The content of element type "log4j:configuration" must match "(renderer*,throwableRenderer?,appender*,plugin*,(category|logger)*,root?,(categoryFactory|loggerFactory)?)"
I googled around and found out that ordering of my log4j.xml could be wrong, but it should be correct.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d %c %x - %m%n"/>
</layout>
</appender>
<root>
<priority value ="error" />
<appender-ref ref="console" />
</root>
<category name="org.springframework" additivity="false">
<priority value="info" />
<appender-ref ref="console" />
</category>
</log4j:configuration>
Any suggestions?
According to the error message, the DTD expects all category elements to be ahead of the root element. You have them the wrong way round.
Follow this order:
<renderer></renderer>
<appender></appender>
<plugin></plugin>
<logger></logger>
<category></category>
<root></root>
<loggerfactory></loggerfactory>
<categoryfactory></categoryfactory>

Log4j not finding custom appender using a property file

I'm trying to configure log4j in an Eclipse plugin project using the following XML property file, that includes a custom appender called EclipseLoggingAppender:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<appender name="eclipseErrorView" class="com.lior.ibd.utils.logging.EclipseLoggingAppender"/>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
<logger name="com.lior">
<level value ="warn" />
<appender-ref ref="eclipseErrorView" />
</logger>
</log4j:configuration>
I pass this property file to the following statement in the code:
DOMConfigurator.configure(filename);
But when loading the application I get the following error message:
log4j:ERROR Could not create an Appender. Reported error follows.
java.lang.ClassNotFoundException: com.lior.ibd.utils.logging.EclipseLoggingAppender
Anyone knows what's the deal? could be a classpath issue?..
Yes, this is a classpath issue. Log4j is looking for class com.lior.ibd.utils.logging.EclipseLoggingAppender.
(probably appender that wrote someone in your organisation?)
If you remove lines:
<appender name="eclipseErrorView" class="com.lior.ibd.utils.logging.EclipseLoggingAppender"/>
and
<logger name="com.lior">
<level value ="warn" />
<appender-ref ref="eclipseErrorView" />
</logger>
log4j should handle it.
Or add EclipseLoggingAppender to classpath by locating a appropriate jar file and add it to the classpath. I.e. run
java -cp appender.jar com.mypackage.MyClass
for starters you can only have one <root> element. You want something more like
<appender name="eclipseErrorView" class="com.mypackage.EclipseLoggingAppender">
<filter class="org.apache.log4j.varia.LevelRangeFilter Source code of org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
</filter>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
<appender-ref ref="eclipseErrorView" />
</root>
How have you added your custom logger to the classpath?

Categories

Resources