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?
Related
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" />
I am working on a Spring-MVC application in which I am using Jetty as our application server. I want to debug log a specific java class, but even after enabling log4j or slf4j, and adding files it's not working.
Finally, I created a war file, added to start log4j with this command java -jar start.jar --add-to-start=logging-log4j, which created a log4j.xml file in resources directory. In that log4j.xml, I added the class I want to debug, but no [DEBUG] entries are getting added.
Enabled modules :
log4j2-slf4j.mod
slf4j-log4j2.mod
slf4j-api.mod
logging-slf4j.mod
I have also tried their combinations and different loggings as documentation suggested.
log4j.xml present in resources directory of both IDE and Jetty.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<!-- console appender -->
<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>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
<logger name="com.zaxxer.hikari" additivity="false">
<level value="DEBUG" />
<appender-ref ref="console" />
</logger>
<root>
<priority value="ERROR" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
What am I doing wrong?
It seems that the problem is that you have only bridges and not log4j itself in the classpath.
As requested a simple logback.xml config file:
<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} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="test" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
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>
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>
Merged with Log4j not finding custom appender using a property file.
I'm working on an Eclipse RCP project comprised from multiple plugin projects, and having trouble load a custom appender using the following property file in log4j:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="eclipseErrorView" class="com.mypackage.EclipseLoggingAppender">
</appender>
<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 ="warn" />
<appender-ref ref="eclipseErrorView" />
</root>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
But when coming to load this property file I get the following error:
log4j:ERROR Could not create an Appender. Reported error follows.
java.lang.ClassNotFoundException: com.mypackage.EclipseLoggingAppender
Where should I include this package in order for log4j to recoginze it? Note that I'm working on multiple plugin projects, all of them use log4j for logging purposes...
Thanks