I am trying to setup a log4j2 xml config and am not getting the results I desire. I want my console to get level info and above, and the rolling file appender (standard) to get level debug and above. I then want to restrict 3 classes in the console appender to only receive warn and above. However, when I add these logger entries my standard appender also stops receiving the info and debug levels for these 3 classes. What should I do to only restrict console and not standard?
<?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"/>
<param name="threshold" value="info" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c [%t] %X{ctx} ~ %m%n" />
</layout>
</appender>
<appender name="standard" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="logs/broadcast.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} [%t] %X{ctx} ~ %m%n" />
</layout>
</appender>
<appender name="stderr" class="org.apache.log4j.FileAppender">
<param name="file" value="logs/broadcast.err" />
<param name="threshold" value="error" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} [%t] %X{ctx} ~ %m%n" />
</layout>
</appender>
<appender name="jlv" class="org.apache.log4j.net.SocketAppender">
<param name="remoteHost" value="localhost"/>
<param name="port" value="4445"/>
<param name="locationInfo" value="true"/>
</appender>
<logger name="com.mydetv.broadcast.BroadcastLoop">
<level value="warn"/>
<appender-ref ref="console" />
</logger>
<logger name="com.mydetv.broadcast.BroadcastUpdate22">
<level value="warn"/>
<appender-ref ref="console" />
</logger>
<logger name="com.mydetv.broadcast.vlc.VlcRequest22">
<level value="warn"/>
<appender-ref ref="console" />
</logger>
<root>
<priority value ="debug" />
<appender-ref ref="standard" />
<appender-ref ref="stderr"/>
<appender-ref ref="console" />
<appender-ref ref="jlv"/>
</root>
</log4j:configuration>
Enumeration appenders = logger. getAllAppenders(); . . . fileBackupIndex = rollingFileAppender. getMaxBackupIndex();
Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.
You can set a logger's level with the class Configurator from Log4j Core. BUT be aware that the Configurator class is not part of the public API. If you wish to change the root logger level, do something like this : LoggerContext ctx = (LoggerContext) LogManager.
Log4j2 ConsoleAppender ConfigurationThe target poperty specifies the target of the logging messages i.e. SYSTEM_OUT or SYSTEM_ERR . The follow attribute tells whether the appender should honor the reassignments of System. out or System. err made after the logging configuration has been initialized.
First, it looks like your config is for log4j-1.x, not log4j2. You'll need to convert this if you want to use log4j2. (The manual has many examples for the new syntax.)
It originally thought you might need to use the log4j2 ThresholdFilter (http://logging.apache.org/log4j/2.x/manual/filters.html#ThresholdFilter ), but this may be a simpler way:
(BTW, the layout patterns in the example config below still need some work to match your original config.)
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT" ignoreExceptions="false">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.mysetv.broadcast.BroadcastLoop" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="warn" />
<AppenderRef ref="RollingFile"/>
</Logger>
... (add other named loggers here)...
<Root level="trace">
<AppenderRef ref="STDOUT" level="info" />
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
If by any chance you want to restrict to use appender to specific LogLevel. Probably the right answer is to use filters. Kindly take a look at specific logger for levels
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With