I'm attempting to programmatically override the levels of my log4j2 loggers on the fly, but it doesn't seem to be working. Can anyone point out what I might be doing wrong? This is the code that I am currently using to attempt to reset the levels.
final static Level REQUEST = Level.forName("REQUEST", 450);
errorLog.info("Showing Requests in logs");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig("console");
loggerConfig.setLevel(REQUEST);
LoggerConfig loggerConfigFile = config.getLoggerConfig("file-log");
loggerConfigFile.setLevel(REQUEST);
ctx.updateLoggers();
My configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<RollingFile name="file-log" fileName="${log-path}/DataAdapter.log"
filePattern="${log-path}/DataAdapter-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{MM-dd-yyyy HH:mm:ss} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{MM-dd-yyyy HH:mm:ss} %c{1} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="joy.com.DataAdapter" level="trace" additivity="false">
<appender-ref ref="file-log" level="info"/>
</Logger>
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
</Loggers>
</Configuration>
I finally figured it out. Having the level="" in each appender-ref
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
was overriding my programmatic settings. Removing them allowed me to set the level from the code.
<Root level="info" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="file-log"/>
</Root>
Related
Can someone give me pointers on below. I tought the child class level (trace) should override the root class level (warn), but I only see the warn and above logs.
I tought the child class level (trace) should override the root class level (warn). but I only see the warn and above logs.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="30">
<Appenders>
<Console name="StdOut" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %msg%n" />
</Console>
<File name="FileAppender" filename="LogOutput.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="FileAppender" />
</Root>
<!-- Uncomment the Logger definition below to have ONLY warn-level messages
and above be directed to the console -->
<Logger name="com.fdmgroup.logging.Demo" >
<AppenderRef ref="StdOut" level="trace"/>
</Logger>
<Logger name="com.fdmgroup.exercise.Runner" >
<AppenderRef ref="StdOut" level="trace"/>
<AppenderRef ref="FileAppender" level="trace"/>
</Logger>
</Loggers>
</Configuration>
You didn't set a TRACE threshold on the logger, but on the appender reference. Hence your "com.fdmgroup.logging.Demo" logger has a:
WARN threshold for the logger (inherited from the root logger),
a TRACE threshold for the "StdOut" appender,
no threshold for the "FileAppender" inherited from the root logger (the default value for additivity is `true).
A message is logged only if it not rejected by filter on the logger and the appender, hence only WARN messages are logged.
You probably wanted to set the level for loggers, not appender refs:
<Loggers>
<Root level="WARN">
<AppenderRef ref="FileAppender" />
</Root>
<Logger name="com.fdmgroup.logging.Demo" additivity="false" level="TRACE">
<AppenderRef ref="StdOut"/>
</Logger>
<Logger name="com.fdmgroup.exercise.Runner" level="TRACE">
<AppenderRef ref="StdOut"/>
</Logger>
</Loggers>
I added Log4j2 into my project
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
and added next properties:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx"/>
<!-- Крутое, но бесцветное логирование-->
<!-- <PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}.%M():%L - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/>-->
</Console>
<RollingFile name="File" fileName="C:/Users/Mi/output.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz" immediateFlush="true">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
<Logger name="org.apache.logging.log4j.filelogger" level="info" additivity="false">
<AppenderRef ref="File"/>
</Logger>
</Loggers>
</Configuration>
App write logs to console, but not in file. As I wait, app shall write logs into file by url C:/Users/Mi/output.log, it creates automatically, I can't delete it while app is wotking, but there no logs. Please advice, what to do.
You are only logging stuff from org.apache.logging.log4j.filelogger package into the "File" appender. The Logger name property is the package/class reference
You should do something like this to get logging to file instead of console:
<Root level="info">
<AppenderRef ref="File"/>
</Root>
If you want console and file do:
<Root level="info">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
I changed the path of log4j2.xml configuration file and now the log files will create but application doesn't log into the files. I get no error messages.
My order structur:
Log4j2.xml:
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5level ${hostName} --- [%15.15thread] %-100.100class : %message%n%exception
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="RootAppender" fileName="./log/log.log" filePattern="./log/log-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100MB" />
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
<RollingFile name="CurlAppender" fileName="./log/curl.log" filePattern="./log/curl-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500MB" />
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Root>
<Logger name="CurlLogger" level="trace" additivity="false">
<AppenderRef ref="CurlAppender"/>
</Logger>
</Loggers>
I've found the issue.
In the application.properties file there must be set the "logger.config" attribute.
After setting this attribute, the file logging works.
I think there is some issue in Loggers tag.
In the Logger name please add your package name and also change your AppenderRef in Logger.
Please change your xml code to below:
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Root>
<Logger name="com.example.springbackend.controller" level="trace" additivity="false">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Logger>
</Loggers>
Here <Logger name="com.example.springbackend.controller" level="trace" additivity="false"> Logger name is project package name.
I think the issue is in root level.
Please change the root level logger to debug or trace.
I have a java web app with log4j2.xml
I need to have a different level for each package
for example:
com.myexample.firstmodule.* this should be with INFO level
com.myexample.secondmodule.* this should be with TRACE level
I found some answers telling how to do that in .propreties file, but I couldn't understand how to do it with .xml
Anyone can help with that, please?
Thanks in advance
The log4j website contains detailed information about how to do this, but here's the basic example of an xml configuration they give:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.foo.Bar" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
As you can see, the Loggers section can contain a Logger tag, in which you can define the level for any package you want.
Try this
<logger name="com.myexample.firstmodule" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE-AUDIT" />
</logger>
<logger name="com.myexample.secondmodule" level="TRACE" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE-AUDIT" />
</logger>
in my config STDOUT stands for console logger and FILE-AUDIT for file logger.
I'm working with log4j 2, version 2.4 on Jboss 6.4, my log4j2.xml is in WEB-INF/classes placed. I can deploy my war without errors about my log4j config, and I can log properly in my RollingFile name="myLog", but something I can't understand is why appear the info's logs for "myLog" in my console appender
private static final Logger loggerBatch = LogManager.getLogger("my.test");
loggerBatch.info("test log");
Config:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="myLog" fileName="C:\\Workarea\\my.log"
filePattern="C:\\Workarea\\myLog-%d{dd-MM-yyyy}-%i.log"
append="true">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5MB"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.test">
<AppenderRef ref="myLog" level ="info"/>
</Logger>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
In addition to Andrey's answer you have declared 3 root loggers. There can only be one. Which one of the definitions will win is undefined.
Check your additivity settings. This will fix the issue
<Logger name="my.test" additivity="false">