I have a log4j2 config file I was handed and have no clue what half of it means due to that it doesn't follow what is stated on their site about their configuration file layout. Can anybody decipher what the config file is saying?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ERROR_FILE" fileName="${env:ADSSSDIR}/log/error.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="EVENT_FILE" fileName="${env:ADSSSDIR}/log/event.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="mikros.utils.MikrosLoggerTest" level="trace"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="mikros.adsss.logger.AdsssLogger" level="debug"
additivity="false">
<AppenderRef ref="ERROR_FILE" level="ERROR"/>
<AppenderRef ref="EVENT_FILE" level="INFO"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Config file looks fine to me. Are you looking at the right web site?
User manual links:
Status Logger (showing internal Log4j2 WARN-level messages)
monitorInterval
Console Appender
Pattern Layout
File Appender
Environment Variable Lookup
Loggers
additivity
... That should get you started.
Related
I'm using log4j2 for logging in my app. Basically it's logging uncatched exceptions (Error.log) and logging changes of my data at service layer (journal.log; journalJSON.log).
And here's the thing, when I'm starting service layer tests every log appears in the file and console, but when I'm using application deployed in tomcat they're only in console. What's happening?
<?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>
<File name="Error" fileName="logs/error.log"
immediateFlush="true" append="true">
<PatternLayout pattern=" \n\n %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<RandomAccessFile name="Journal" fileName="logs/journal.log" immediateFlush="true" append="true">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %msg%n"/>
</RandomAccessFile>
<File name="JournalJSON" fileName="logs/journalJSON.log" immediateFlush="true" append="false">
<JSONLayout complete="true" charset="UTF-8" compact="false" eventEol="false"/>
</File>
</appenders>
<Loggers>
<Logger name="com.user.controller" level="error" additivity="false">
<AppenderRef ref="Error" level="error"/>
<AppenderRef ref="Console" level="error"/>
</Logger>
<Logger name="com.user.service" level="info" additivity="false">
<appender-ref ref="JournalJSON" level="info"/>
<appender-ref ref="Journal" level="info"/>
<appender-ref ref="Console" level="info"/>
</Logger>
<Root level="warn">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
Well, there are a couple of possibilities I can think of:
Your configuration file isn't being found and it is using the default configuration.
The logger for your application isn't com.user.controller or com.user.service and the root logger is being used.
get a logger instance named "com.foo"
Logger logger = Logger.getLogger("com.foo");
Now set its level. Normally you do not need to set the level of a logger programmatically. This is usually done in configuration files.
logger.setLevel(Level.INFO);
this is an example of log4j2 configuration
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Appender type="File" name="File" path="location/to/logging/space/file.txt">
<Layout type="PatternLayout" pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Appender>
</Appenders>
<Loggers>
<Logger name="com.foo" level="trace" additivity="false">
<AppenderRef ref="File"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
I am trying out log4j2 configuration
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="info">
<appenders>
<Console name="console-log" target="SYSTEM_OUT" append="false">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36}:%L %M - %msg%xEx%n"/>
</Console>
<File name="debug-log" fileName="${catalina.base}/logs/debug.log" append="false">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS z} %-5level %class{36}:%L %M - %msg%xEx%n"/>
</File>
<File name="trace-log" fileName="${catalina.base}/logs/trace.log" append="false">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS z} %-5level %class{36}:%L %M - %msg%xEx%n"/>
</File>
<File name="error-log" fileName="${catalina.base}/logs/error.log" append="false">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS z} %-5level %class{36}:%L %M - %msg%xEx%n"/>
</File>
</appenders>
<loggers>
<root level="debug" additivity="false">
<appender-ref ref="console-log"/>
</root>
<Logger name="com.mypackage" level="debug" additivity="false">
<appender-ref ref="debug-log" level="debug"/>
<appender-ref ref="trace-log" level="info"/>
<appender-ref ref="error-log" level="error"/>
</Logger>
</loggers>
</configuration>
I am getting logging and data to catalina.out but the rest of the files are not getting created.
Couldn't figure out the silly mistake I might be doing here?
and How do I create one more file which can have the appended logs of debug-log,trace-log,error-log only.
You have an error in your config:
Console appenders shouldn't have an append attribute. Removing that should fix your config.
Additionally the additivity attribute on the Root logger is unnecessary. It does nothing, as there is no higher logger to add to.
You should definitely have seen a line like the following in your output, warning you about the bad config:
ERROR Console contains an invalid element or attribute "append"
I am new to unix. I have below log4j2 config file. When I use this file in unix environment, the file app.log is not created. Could any one tell me how to redirect the logs to file in unix environment
<?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} %method - %msg%n" />
</Console>
<File name="file" fileName="C:\Users\ppatrana\Desktop\app.log">
<!-- <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern> -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} %method - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="CONSOLE" />
</Root>
<Root level="trace">
<AppenderRef ref="file" />
</Root>
</Loggers>
</Configuration>
You have to change the filename :
<File name="file" fileName="/tmp/app.log">
More about linux filesystem : http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard
I think your real problem is that you have two root loggers. Try the following instead:
<Loggers>
<Root level="trace">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="file" />
</Root>
</Loggers>
You also need to fix the filename as mentioned by #ToYonos.
I am working with the log4j2 configuration file settings to try and make the config file use regular expressions so the it can use multiple classes in the logger property and name attribute. This is what I have tried so far that doesn't appear to work.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ERROR_FILE" fileName="../log/error.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="EVENT_FILE" fileName="../log/event.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="foo.test" level="trace"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="foo.*" level="debug"
additivity="false">
<AppenderRef ref="ERROR_FILE" level="ERROR"/>
<AppenderRef ref="EVENT_FILE" level="INFO"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Has anybody ever been able to successfully use regular expressions or even globs in the log4j2 config file?
I figured out I do not need to put regular expressions at all in the package name. For example if in my foo package i had foo.bar, foo.foo, and foo.test. I would put foo in the Logger property name attribute to log the ERROR and INFO level logs from any any of 3 packages that begins with foo as such.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ERROR_FILE" fileName="../log/error.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="EVENT_FILE" fileName="../log/event.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="foo.test" level="trace"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="foo" level="debug"
additivity="false">
<AppenderRef ref="ERROR_FILE" level="ERROR"/>
<AppenderRef ref="EVENT_FILE" level="INFO"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>