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
Related
I am new to log4j and was just wondering if someone could help me with a configuration that I am currently guessing. I'm trying to test it but it doesn't seem to be working.
<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="debugAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/usr/local/apache-tomcat-7.0.35/logs/sample.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMax" value="DEBUG" />
</filter>
</appender>
<logger name="com.webservice" additivity="false">
<priority value="DEBUG" />
<appender-ref ref="debugAppender" />
</logger>
<root>
<priority value="ERROR" />
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
What I am trying to do here is set the root logger to ERROR so that it does not pick up anything unless there is an error. But then I have a logger named com.webservice that I would like to have pick up DEBUG statements from my packages(and not other libraries) only for development purposes.
When I go to production I want to change the com.webservice priority back to INFO so that it does not pick up any logging.
Have I set this file up correctly?
thanks
Try to set debug attribut in log4j:configuration node to true.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
It prints out information as the configuration file is read and used to configure the log4j environment. You may be got more details to resolve your problem.
I havent worked on xml but I use log4j.properties which works perfectly fine. Just place this in your classpath and add jar for log4j. It should work. You can change log levels as you want.
Also there is no need to specify ERROR level as by default ERROR level gets logged.
Sample :
log4j.debug=TRUE
log4j.logger.com.XXX.RemoteCacheManager=FATAL, Logs
log4j.logger.com.XXX.RedisCacheImpl=FATAL, Logs
log4j.logger.com.XXX.utils=DEBUG, TimeItLogs
log4j.logger.com.XXX=DEBUG, Logs
log4j.logger.org.hibernate=INFO, Logs
log4j.logger.org.springframework=INFO, Logs
log4j.logger.org.apache.velocity=ERROR, Logs
log4j.logger.org.apache.commons=ERROR, Logs
log4j.logger.org.apache.tiles=ERROR, Logs
log4j.appender.Logs=org.apache.log4j.RollingFileAppender
log4j.appender.Logs.File=${catalina.base}/logs/XXX.log
log4j.appender.Logs.MaxFileSize=10MB
log4j.appender.Logs.MaxBackupIndex=50
log4j.appender.Logs.layout=org.apache.log4j.PatternLayout
log4j.appender.Logs.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c %m %n
I would like to know if there is possibility to set an attribute in log4j.xml by using property file. For example the log4j.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %l - %m%n "/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="\D:\ReadText_File\log4jlogss.txt"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}]- %l - %m%n"/>
</layout>
</appender>
<logger name="org.apache">
<level value="WARN"/>
</logger>
<root>
<level value="DEBUG"/>
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>
I wan to specify the path for the file using Properties file
Any idea how can we do it ????
about log4j, this type of configuration that XML configuration which embed the property file is not supported in current version.
for details, you can view the log4j's source code . the XML parse details is in class
DOMConfigurator.configure
You can use Log4j Lookups with PropertySubstitution:
See http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution and http://logging.apache.org/log4j/2.x/manual/lookups.html
Here is an example on StackOverflow: Log4J2 property substitution - default
(to make it short, you can use, as far as I know, the System Property using -Dkey=value)
You can also try to use the ${bundle:com.package.Messages:MyKey} syntax to use a properties file (using log4j 2.0-rc1 or greater), see https://stackoverflow.com/a/19303208/1149528.
I configured the logging in my Play Framework application. When I run application in prod or dev mode or run test by comand (play test) - everything works fine, but test can not executes when I run their 'play auto-test'. Please help!
in application.conf:
application.log=INFO
application.log.path=/log4j.xml
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${application.path}/logs/application.log"/>
<param name="MaxFileSize" value="1MB"/>
<param name="MaxBackupIndex" value="100"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p ~ %m %n"/>
</layout>
</appender>
<logger name="play">
<level value="error"/>
</logger>
<root>
<priority value="error"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
When I use the following log4j.xml file:
<?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="%m%n"/>
</layout>
</appender>
<logger name="play">
<level value="debug"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
tests running the command 'play auto-test' successfully executed. Please tell how configure logging in Play (output to file) that run and executes tests in 'play auto-test'!
Thi is pretty much a Play! bug. It's not caused by the fact that you've configured log4j with a separate file, though doing that will hide another error message: "play.tmp is null when it should be play.tmp=none" (something to this liking - you'll get this if you remove your custon log4j.xml file and re-do "play auto-test"). Problem is that even if you set that to play.tmp=none it will still not work. Besides, if you compare their online documentation to Play!'s console output you'll already realize that something's wrong:
The docs say:
"The 'auto-test' command do the same than the 'test' command, but it automatically launch a browser, run all the tests, and stop."
Console sais (when you do "play auto-test"):
ATTENTION: You're running Play! in DEV mode
~
~ Go to http://localhost:9000/#tests to run the tests
~
The answer was very simple
in application.conf:
%test.application.log=INFO
%test.application.log.path=/log4j.xml
application.log=INFO
application.log.path=/log4j.properties
application.log.system.out=off
log4j.xml:
<?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="%m%n"/>
</layout>
</appender>
<logger name="play">
<level value="debug"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
log4j.properties:
log4j.rootLogger=ERROR, Rolling
log4j.logger.play=INFO
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
log4j.appender.Rolling.File=${application.path}/logs/application.log
log4j.appender.Rolling.MaxFileSize=1MB
log4j.appender.Rolling.MaxBackupIndex=100
log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.Rolling.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m %n
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n
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?
Right now we are using a customized logger method for our application but now we have a stand alone code for which we need to write to a separate log file. We have little idea about log4j. I just want to know where to change properties if any so that i don't disturb the existing logger application as well as we write our logs into new log file.
First define a file appender:
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=/mylogfile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
Then point your package to use this appender:
log4j.logger.mypackage=LOGFILE
log4j.additivity.mypackage=false
The last line is important if you do not want your package to inherit the global appender. Doing so will result in the log messages from mypackage also printed at the default appender.
in log4j you can direct log entries to separate files based upon pacakge name.
log4j.logger.your.package1= LOG,STDOUT
log4j.additivity.your.package1=false
log4j.logger.your.package2= DEBUG, STDOUT
log4j.additivity.your.package2=false
I use XML configuration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="logfile" class="org.apache.log4j.FileAppender">
<param name="file" value="app.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d-%5p(%F:%L)-%m%n"/>
</layout>
</appender>
<appender name="myLogfile" class="org.apache.log4j.FileAppender">
<param name="file" value="myFile.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d-%5p(%F:%L)-%m%n"/>
</layout>
</appender>
<logger name="org.myApp">
<level value="INFO"/>
<appender-ref ref="myLogfile" />
</logger>
<root>
<level value="ERROR" />
<appender-ref ref="logfile" />
</root>
</log4j:configuration>
Just use FileAppender and specify a new filename in the constructor.