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>
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 have an standalone java application with following config 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="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="1" />
<param name="File"
value=".\\myComp.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yy HH\:mm\:ss.SSS} %5p %c{1}:%L - %m%n " />
</layout>
</appender>
<category name="com.mycomp.project.starter">
<priority value="${project.client.log.level.starter}" />
</category>
<category name="com.security">
<priority value="${project.client.log.level.security}" />
</category>
<root>
<level value="ERROR" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
When I start the Application I can set the log level through a ini file which can contain:
-Dproject.client.log.level.starter=INFO
-Dproject.client.log.level.security=DEBUG
What I would like to archive is, if the -Dproject.client.log.level.security=DEBUG is not set it should use ERROR.
How could I achieve this? I appreciate any help.
Okay, I got a solution, unfortunately it involved switching to log4j2.
I found out that log4j allows Property Substitution allows to use system properties with prefix sys:. In combination with this finding (where I can set default values with a simple -), I changed my config to:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<RollingFile name="file" fileName=".\\myComp.log" append="true" filePattern=".\\myComp-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{dd.MM.yy HH:mm:ss.SSS} %5p %c{1}:%L - %m%n "/>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- setting log level per default to INFO if not set through system properties -->
<Logger name="com.myComp.starter" level="${sys:project.client.log.level.starter:-INFO}" />
<!-- setting log level per default to ERROR if not set through system properties -->
<Logger name="com.security" level="${sys:project.client.log.level.security:-ERROR}" />
<Root level="ERROR">
<AppenderRef ref="file"/>
</Root>
</Loggers>
</Configuration>
Now I am able to set it within a ini file or leave it out if not needed and still have a defined log level.
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>
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?