log4j2 external library logs to separate files - java

I'm using log4j2 xml configuration with a Routing Appender and a wrapper. I'm passing different appenders to log to different files for different parts of the project. My configuration looks something like this.
<Configuration status="WARN">
<Appenders>
<Routing name="RoutingAppender">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}"
fileName="logs/${ctx:logFileName}"
filePattern="logs/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</RollingFile>
</Route>
<!-- This route is chosen if ThreadContext has no value for key logFileName} -->
<Route key="$${ctx:logFileName}">
<RollingFile name="Rolling-default" fileName="logs/WITHOOUT-THREAD-CONTEXT.log"
filePattern="./logs/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
The problem is, I'm getting external library logs(those without appenders) together with my project logs in the second route, although I want only external logs.

I would suggest to use configuration something like below -
<Configuration status="WARN">
<Appenders>
<RollingFile name="RollingFile"
fileName="logs/${ctx:logFileName}"
filePattern="logs/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</RollingFile>
<RollingFile name="Rolling-default" fileName="logs/WITHOOUT-THREAD-CONTEXT.log"
filePattern="./logs/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="application.package" level="info" additivity="false">
<appender-ref ref="RollingFile" />
</Logger>
<Root level="info" additivity="false">
<appender-ref ref="Rolling-default" />
</Root>
</Loggers>
</Configuration>
In this configuration, there are 2 loggers. One logger is for your application having root package name - application.package. This package should contain all sub-packages and classes of your application. It will use RollingFile Appender for logging.
Another is Root logger which can be used for logging external libraries i.e. classes which are not in application.package. It will use Rolling-default Appender.
In above configuration, you are setting fileName through context lookup so you have to set context value in the beginning otherwise you will get error.

Related

log4j2 writing method and class

I'm using log4j2 in a java program..
this is the line of code where it is initialized
private static final Logger logger = LogManager.getLogger("application-
log");
and this is the configuration file where the format is decided
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
<Properties>
<Property name="logApplicativo">../logs/logApplicativo.log</Property>
<Property name="logCdr">../logs/logCdr.log</Property>
</Properties>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%m MDC%X%n"/>
</Console>
<RollingRandomAccessFile name="fileLogApplicativo"
fileName="${logApplicativo}" filePattern="${log-Applicativo}-%d{yyyy-MM-
dd}-%i.log" immediateFlush="false" append="true"
ignoreExceptions="false" >
<PatternLayout>
<pattern>%d [%-6p] %C.%M(%F:%L) - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile >
<RollingRandomAccessFile name="fileLogCdr" fileName="${logCdr}"
filePattern="${log-Cdr}-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout>
<pattern>%d %-5p %c{2} - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncRoot level="error">
<AppenderRef ref="STDOUT"/>
</AsyncRoot>
<AsyncLogger name="application-log" level="debug" additivity="false">
<AppenderRef ref="fileLogApplicativo" level="debug"/>>
</AsyncLogger>
<AsyncLogger name="cdr-log" level="debug" additivity="false">
<appender-ref ref="fileLogCdr" level="debug"/>
</AsyncLogger>
</Loggers>
I do not understand why, but class name, code name, line code arent printed. Probably the problem is trivial but I'm going crazy from one day.
Thank you all
If you're using asynchronous loggers or asynchronous appenders try adding includeLocation="true" to your logger or appender configuration.
You can read more about it in the log4j2 manual

log4j: How can I direct logs into different files for a cron and for a webservice?

I have a Cron and a Webservice, both implemented using spring. The cron and the webservice use a set of classes A, B and C to achieve their objective.
In each class, I use log4j 2 as the logging mechanism as so:
Logger log = LogManager.getLogger(A.class.getName());
In the log4j.xml, I have a single RollingAppender which logs to a file.
Now, I would like the Cron to log to a different file i.e. use a different appender. But if I set the category for the cron to use a different appender, that still doesn't cause the logs from A, B and C to go into that appender.
Update: log4j configuration:
<Configuration status="warn" name="mylogger" packages="">
<Properties>
<Property name="baseDir">/var/log/tomcat</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}.log.gz">
<PatternLayout><Pattern>%5p %d{ISO8601} [%t][%x] %c - %m%n</Pattern></PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
You can use below mentioned configuration if you want to log into different files using same class.
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="rollingFileAppender"
fileName="/data/abc.log"
filePattern="/data/abc-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
<RollingFile name="rollingFilesAppender"
fileName="/data/cde.log"
filePattern="/data/fgh-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE" />
</Root>
<Logger name="rollingFilesLogger" additivity="false" level="WARN">
<AppenderRef ref="rollingFilesAppender" />
</Logger>
<Logger name="com.log4jtest" additivity="false" level="INFO">
<AppenderRef ref="rollingFileAppender" />
</Logger>
</Loggers>
In Java file you can use like :
private static final Logger LOGGER = LogManager.getLogger(Hello.class);
private static final Logger SECOND_LOGGER = LogManager.getLogger("rollingFilesLogger");
Using this you will be able to send logs in two different files.
Ref : https://github.com/ragnar-lothbrok/log4j2-example

Log4j2 rc 1 DefaultRolloverStrategy overwrites after 7 files

I am using lo4j2 rc1 with RollingFile holding TimeBasedTriggeringPolicy, SizeBasedTriggeringPolicy and DefaultRolloverStrategy having Max files as 50. But the rolled back files are overwritten after 7 files.
Below is my configuration
<Appenders>
<Routing name="ServerLogs">
<Routes pattern="$${ctx:logRouter}/">
<Route>
<RollingFile name="ServerLogs" immediateFlush="false" append="false"
fileName="${loghome}/${ctx:logRouter}/ServerLogs.log"
filePattern="${loghome}/${ctx:logRouter}/%d{dd-MM-yyyy}-ServerLogs-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %-40C{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="4 MB" />
<DefaultRolloverStrategy max="50"/>
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<AsyncLogger name="AsyncServerLogs" level="TRACE" includeLocation="false">
<AppenderRef ref="ServerLogs"/>
</AsyncLogger>
<Root level="DEBUG" includeLocation="false">
<AppenderRef ref="ServerLogs" level="TRACE" />
</Root>
</Loggers>
Am i missing any configuration?
Your <DefaultRolloverStrategy max="50"/> element is in the wrong place.
It's not a policy, so it doesn't belong in the <Policies> element. In fact, you may even see the following in your logs:
ERROR Policies has no parameter that matches element DefaultRolloverStrategy
Move it up a level so that it's a direct child of the <RollingFile> element.

issue with my log4j2 setup

I was using log4j2 and it was logging statements for me with no issues. I might have made some changes (moved from info to debug and back) but its quite possibly I might have messed up the config in some other fashion. I am copying my config file below (I have not moved any log4j2 and slf4j jar files from my project). Any thoughts?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="TestApp" packages="">
<Appenders>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="logs/test.log" immediateFlush="false" append="false"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="FATAL_LOGGER" level="fatal" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<Root level="debug" includeLocation="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
I tested the config in a test project and it works without any issues. Ensure that you do not have any locks on the file and have correct read/write privileges on the file/directory.

How to write different logs in different files with log4j2 (MDC in xml)?

Now I'm using structure like this:
Appender:
<RollingFile name="user.log" append="true" fileName="users/%MDC{USERNAME}.txt"
filePattern="users/archive/%MDC{USERNAME}-%d{MM-dd-yyyy}-%i.txt.gz">
<PatternLayout pattern="%-5p %d{MMMM-dd HH:mm:ss} %X: %c - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
</RollingFile>
Logger:
<appender-ref ref="user.log">
<ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY" operator="or">
<KeyValuePair key="USERNAME" value="%X{USERNAME}"/>
<KeyValuePair key="IP" value="%X{IP}"/>
</ThreadContextMapFilter>
</appender-ref>
But it doesn't work with MDC keys. How could I use MDC keys in xml to config RollingFileAppender?
Take a look at RoutingAppender. Maybe this can get you started:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG" name="MyApp" packages="">
<appenders>
<Routing name="Routing">
<Routes pattern="$${ctx:USERNAME}">
<Route>
<RollingFile name="user.log" append="true" fileName="users/${ctx:USERNAME}.txt"
filePattern="users/archive/${ctx:USERNAME}-%d{MM-dd-yyyy}-%i.txt.gz">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
</appenders>
<loggers>
<root level="TRACE">
<appender-ref ref="Routing" level="DEBUG" />
</root>
</loggers>
</configuration>

Categories

Resources