When running my application I see on the stdout the following log4j initialization infos:
log4j: Threshold ="null".
log4j: Level value for root is [OFF].
log4j: root level set to OFF
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].
This is probably coming from external libraries using log4j and slf4j.
I am trying to suppress these initialization infos, but even setting the root logger level to OFF does not seem to have any effect.
This is the log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="OFF" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Any suggestion?
Solved setting debug to false in log4j.xml:
<log4j:configuration debug="false"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="OFF" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Related
I have a log4j configuration file as below, what I want to do is output the TRACE in the console and output the ERROR message to the file (fileerror). But it doesn't work, both the console and file is at the TRACE level.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="fileerror" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="1024MB" />
<param name="maxBackupIndex" value="2" />
<param name="file" value="ExonImpact.error.log.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="ccbb.hrbeu" additivity="true">
<level value="TRACE"/>
<appender-ref ref="console" />
</logger>
<root>
<level value="ERROR"/>
<appender-ref ref="fileerror" />
</root>
</log4j:configuration>
Below is my log4j debug information.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [ccbb.hrbeu] additivity to [true].
log4j: Level value for ccbb.hrbeu is [TRACE].
log4j: ccbb.hrbeu level set to TRACE
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [ccbb.hrbeu].
log4j: Level value for root is [ERROR].
log4j: root level set to ERROR
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [append] to [false].
log4j: Setting property [maxFileSize] to [1024MB].
log4j: Setting property [maxBackupIndex] to [2].
log4j: Setting property [file] to [ExonImpact.error.log.txt].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: setFile called: ExonImpact.error.log.txt, false
log4j: setFile ended
log4j: Adding appender named [fileerror] to category [root].
By default, all messages caught by a child logger are propagated to the root logger. That's why both of your loggers log the same message. You can set your additivity parameter to false (additivity="false") to prevent your logger from passing messages to the root logger. Or you can add an additional "threshold" parameter to your file appender:
<param name="Threshold" value="ERROR" />
then it will ignore all messages below Error level.
I have almost tried everything but log4j seems to not compress the log file. I am using 1.2.17 jars for log4j and log4j extras. Cant seem to understand whats wrong . it simply creates a gz file but thats a normal file without compression
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="file" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy name="file" class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName" value="${catalina.home}/logs/broker.log" />
<param name="FileNamePattern" value="=${catalina.home}/logs/broker-.%d{yyyyMMdd-HH}.log.gz" />
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="10000"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%"/>
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
Answer to my own question this is how I made it work
#Worked with 2.17 version
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.file.rollingPolicy.maxIndex=5
log4j.appender.file.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.triggeringPolicy.MaxFileSize=10000
log4j.appender.file.rollingPolicy.FileNamePattern=${catalina.home}/logs/broker-%i.log.gz
log4j.appender.file.rollingPolicy.ActiveFileName=${catalina.home}/logs/broker.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
How I can remove all my Tomcat INFO from my log file, but just from my log file, I want to see that INFO only in console output
Here is what you can do. Hope it helps
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
<!-- Redirect the logging to console. -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
<!--
Redirect the logging to a file.. -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="10KB" />
<param name="maxBackupIndex" value="5" />
<!-- For Tomcat -->
<param name="file" value="${catalina.home}/logs/myStruts1App.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
I have a log4j configuration file and it uses a ConsoleAppender with a specific layout.
I use
logger.info("some text");
in several places in my java code.
Is it possible to create another appender with a different layout in the configuration file and use the specific appender wherever I choose in the code?
Is it possible to create another appender with a different layout in the configuration file and use the specific appender wherever I choose in the code?
Yes you can create different appender with a different layout and create different categories for each appender.
use the category name to get the logger instead of appender name.
Sample: (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="stdout1" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%.4t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<appender name="stdout2" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%.4t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<category name="category1">
<priority value="INFO" />
<appender-ref ref="stdout1" />
</category>
<category name="category2">
<priority value="DEBUG" />
<appender-ref ref="stdout2" />
</category>
</log4j:configuration>
Java Code: (get the logger based on category name)
org.apache.log4j.Logger.getLogger("category1").warn("debug msg for stdout1 appender");
org.apache.log4j.Logger.getLogger("category2").info("info msg for stdout2 appender");
Output:
[main] WARN category1 - warn msg for stdout1 appender
2014-05-09 02:46:29,216 [main] INFO category2 - info msg for stdout2 appender
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.