Migrating from log4j 1.2 to log4j2 - java

I am in the process of migrating my application from log4j 1.2 to log4j2-2.8.1 version.
Following is the existing 1.x configuration in log4j.properties file.
log4j.appender.JSERRORFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.JSERRORFILE.File=${log4j.loglocation}/jserror.log
log4j.appender.JSERRORFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.JSERRORFILE.layout.ConversionPattern=%d %-5p %c - %m%n
log4j.logger.com.app.JavascriptLogger=ERROR,JSERRORFILE
log4j.additivity.com.app.JavascriptLogger=false
Converted this to equivalent xml configuration log4j2.xml :
<RollingFile name="JSERRORFILE" fileName="${log-path}/jserror.log">
<PatternLayout pattern="%d %-5p %c - %m%n" />
</RollingFile>
<Logger name="com.app.JavascriptLogger" level="ERROR" additivity="false">
<AppenderRef ref="JSERRORFILE"/>
</Logger>
After conversion,I keep getting the following error :
org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element RollingFile are invalid
Any help would be appreciated.

You need to tell the RollingFile appender when to rollover (trigger policy) and what the result of a rollover should look like.
If you want to rollover at some regular interval (TimeBasedTriggeringPolicy or CronTriggeringPolicy) you need to specify a filePattern containing a SimpleDateFormat-like string. If you want to rollover to prevent large files (SizeBasedTriggeringPolicy), you need to specify a filePattern containing %i.
The filePattern is the relative or absolute path of the location you want the old (rolled over) file to be moved to.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
The cron expression above fires once a day.
For details and more examples see the RollingFile appender section of the user manual.

Related

Log4j does not print to console nor write to file

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class AzulMain {
private static final Logger LOGGER = LogManager.getLogger(AzulMain.class.getName());
public static void main(String[] args){
LOGGER.info("Maybe my first Logger works?");
}
}
The imports work fine. I use these jar-files:
log4j-1.2-api-2.17.2.jar
log4j-api.2.17.2.jar
log4j-core-2.17.2.jar
And this is how my XML-file (log4j2.xml) looks. It is in the same folder as my AzulMain:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">log/${date:yyyy-MM-dd HH-mm-ss-SSS}</Property>
<Property name="archive">${log-path}/archive</Property>
</Properties>
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n
</pattern>
</PatternLayout>
</Console>
<File name="File-Appender-AzulMain" fileName="${log-path}/Azul.log">
<PatternLayout>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n
</pattern>
</PatternLayout>
</File>
<RollingFile name="RollingFile-Appender"
fileName="${log-path}/rollingfile.log"
filePattern="${archive}/rollingfile.log.%d{yyyy-MM-dd#HH-mm}.gz">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="30 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="AzulMain" level="trace" additivity="false">
<AppenderRef ref="File-Appender-AzulMain" level="all"/>
<AppenderRef ref="Console-Appender" level="info"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="Console-Appender"/>
</Root>
</Loggers>
</Configuration>
When I use JavaUtilLogger everything works quite fine so far, I can make it create a file and print to console, however, with log4j nothing works.
I tested deleting the XML file and adding BasicConfigurator.configure() into my main-method, but it still didn't work. If I start my main-method all I get is:
Process finished with exit code 0
What is strange to me is that when I use the command java -Dlog4j.debug -cp AzulMain, it does not show me my configuration as I would expect it, but just what seems to be a very generic help message.
It is my first time, I am using a logger. Does anyone know what the problem might be?
Update:
This helped me as a first step:
BasicConfigurator replacement in log4j2
I deleted the XML-file and used the new
Configurator.initialize(new DefaultConfiguration());
Configurator.setRootLevel(Level.INFO);
And now it works at least in so far as it prints to the console. However, I am still not able to make it use the log4j2.xml file. I tried naming it log4j2-test.xml, too. (Source) It did not make a difference.
Now it works. This is how I did it.
I set up a new project, with just a main-class and added a new xml-file with a file logger only at first, then added a logger to the console, too. I deleted cache in IntelliJ and deleted the Configurator-lines in the code.
This is the new XML-file log4j2.xml that made it work:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="basePath">log/${date:yyyy-MM-dd HH-mm-ss-SSS}</Property>
</Properties>
<!-- File Logger -->
<Appenders>
<!-- Console appender configuration -->
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n
</pattern>>
</PatternLayout>
</Console>
<RollingFile name="fileLogger"
fileName="${basePath}/Azul.log"
filePattern="${basePath}/app-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<!-- Max 10 files will be created everyday -->
<DefaultRolloverStrategy max="10">
<Delete basePath="${basePath}" maxDepth="10">
<!-- Delete all files older than 30 days -->
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<appender-ref ref="fileLogger" />
<appender-ref ref="Console-Appender" level="info"/>
</Root>
</Loggers>
</Configuration>
It seems to me to have been a mistake somewhere in the xml-file that I just couldn't figure out where it is.

In Log4j2, Is it possible to filter out certain key value pairs from the logger based on the key?

Can Log4j2 be configured in such a way that the filters or some other components can filter out certain values from getting printed in the log? (but should allow other fields in the same line to pass through)
Say the following lines appear in the log
[operation=DONE, userName=junitUser, tenant=Tenant [tenantID=default], needDetails=1, message=BaseMsg [version=1.0, sdk=AppSDK [version=1.3, protocols=[4aac81ca, 393ae7a0]], device=Device [id=12345, type=Pompom, info=Dot's Device]]], channel=null
[operation=DONE, userName=junitUser224, tenant=Tenant [tenantID=default], needDetails=1, message=BaseMsg [version=1.0, sdk=AppSDK [version=1.3, protocols=[4aac81ca,393ae7a0]], device=Device [id=123456, type=Mamamia, info=tom's Device]]], channel=null
Now can I filter out the "userName" field in such a way that the log lines now do not contain it as shown below?
[operation=DONE, tenant=Tenant [tenantID=default], needDetails=1, message=BaseMsg [version=1.0, sdk=AppSDK [version=1.3, protocols=[4aac81ca, 393ae7a0]], device=Device [id=12345, type=Pompom, info=Dot's Device]]], channel=null
[operation=DONE, tenant=Tenant [tenantID=default], needDetails=1, message=BaseMsg [version=1.0, sdk=AppSDK [version=1.3, protocols=[4aac81ca,393ae7a0]], device=Device [id=123456, type=Mamamia, info=tom's Device]]], channel=null
Here is my log4j2.xml
<?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="RollingFile" fileName="/Users/dunston/logs/app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".* zinger_log .*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
This can be accomplished with a RewriteAppender.
You may need to write a custom RewritePolicy that inspects the LogEvent's Message and replaces the Message with another instance if the formatted message contains a regular expression that you want to filter out.
Your custom RewitePolicy can be configured in the configuration like any other standard Log4j2 plugin.

how to roll a new log file in routing appender everyday

My routing log4j2.xml should role log file based on user login.
Assume a user logs in the application for consecutive 3 days and do his stuff for around half an hour and then logs out. So as per requirement 3 log files should be created for the logged user (one file per day basis in separate file)
like e.g.
john-20-11-2015.log,
john-21-11-2015.log,
john-22-11-2015.log
below is the Log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN" name="MySite" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
</PatternLayout>
</Console>
<RollingFile name="error-log"
fileName="D:/myLogs/error [${date:yyyy-MM-dd}].log" filePattern="D:/myLogs/error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="trace-log"
fileName="D:/myLogs/trace [${date:yyyy-MM-dd}].log" filePattern="D:/myLogs/trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Routing name="RoutingAppender">
<Routes pattern="$${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}" append="true"
fileName="D:/myLogs/${ctx:logFileName}~${date:yyyy-MM-dd}.log"
filePattern="D:/myLogs/%d{yyyy-MM-dd}-%i.log">
<PatternLayout
pattern="SERVER-LOG: [%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Route>
<Route ref="Console" key="$${ctx:logFileName}" />
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="trace" additivity="false">
<Appender-Ref ref="Console"/>
<appender-ref ref="error-log" level="error"/>
<appender-ref ref="trace-log" level="trace"/>
<Appender-Ref ref="RoutingAppender" />
</Root>
</Loggers>
</Configuration>
I am calling
ThreadContext.put("logFileName", userName);
to append the log in the routing appender, the log were appending correctly in the respective logs
and I am clearing the threadcontext during logout method
ThreadContext.remove("logFileName");
ThreadContext.clearAll();
I hosted my application in tomcat. log files were generated based on logged user for every user based on the file name pattern, but log is not generated on daily basis, instead of that its appended the current day log of users in previous day log
eg: john-20-11-2015.log contains the log of of 21st and 22nd
its roles a new log file only when tomcat is stop started .
guys help me out.. is there anything wrong
I think you want DailyRollingFileAppender here is an example
Also please look at the similar question
example:
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
...
<param name="DatePattern" value="'.'yyyy-MM-dd" />
...
</appender>
The filename attribute is only evaluated when the appender is created. It does not change on each rollover. You should get a new file on each rollover that does have the correct date in it.
Is that not what you are seeing?

log4j2 - how to change file dynamically?

I've implemented async logging with log4j 2, but now I need to change log filename every hour, eg 2015-11-19/log-12.00.log, 2015-11-19/log-13.00, etc. (Soulutions I've found didn't work, may be I did something wrong).
I have following log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Don't forget to set system property
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
to make all loggers asynchronous. -->
<Configuration status="WARN">
<Appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<RandomAccessFile name="RandomAccessFile" fileName="async.log" immediateFlush="false" append="true">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
</Appenders>
<Loggers>
<Root level="info" includeLocation="false">
<AppenderRef ref="RandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
How to achive this?
You should have a look at TimeBasedTriggeringPolicy. Basically it causes a rollover once the date/time pattern no longer applies to the active file.
Have't tried this but this should work for you.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" >
<appenders>
<Async name="Async">
<AppenderRef ref="logfile" />
</Async>
<RollingRandomAccessFile name="logfile" fileName="async.log" filePattern="log-%d{HH}.00.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="24"/>
</RollingRandomAccessFile>
</appenders>
<loggers>
<root level="INFO" includeLocation="false">
<AppenderRef ref="Async"/>
</root>
</loggers>
</configuration>
The TimeBasedTriggeringPolicy interval attribute controls how often a rollover should occur based on the most specific time unit in the date pattern. In you case it is hours that is %d{HH} from file pattern.

Log4j2 - Asynclogger Rolling File Appender not rolling daily per hour

This is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
<Appenders>
<!-- Generate STDOUT in console -->
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
<!-- Generate rolling log for router with per hour interval policy -->
<RollingFile name="RouterRollingFile" fileName="/apps/bea/mb-logs/router.log"
immediateFlush="false" filePattern="/apps/bea/mb-logs/router.%d{yyyy-MM-dd-HH}-%i.log">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %5p [%t] (%F:%L) - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<!-- <DefaultRolloverStrategy fileIndex="max" max="24" /> -->
</RollingFile>
</Appenders>
<Loggers>
<AsyncLogger name="com.tritronik.mb.router" level="info"
additivity="false" includeLocation="true">
<AppenderRef ref="RouterRollingFile" />
</AsyncLogger>
<!-- <Root level="info">
<appender-ref ref="CONSOLE" />
</Root> -->
</Loggers>
I want to achieve daily rolling file with per hour rolling, so far I haven't been able to produce the log with proper format, and as I remember, the interval parameter seems like to increment by day not hour.
I want to achieve this:
router.log --> currently written file
router.log.2014-06-20-00
router.log.2014-06-20-01
...
router.log.2014-06-20-23
router.log.2014-06-21-00
...
Instead I achieve this:
router.log
router.log.2014-06-20-1 --> One day worth of logs
I've been able to do this using usual log4j, but the io performance goes down and force me to use log4j2, but I stumble into this problem.
Where do I have it wrong? Or is it true that log4j2 doesn't support this yet?
Thank you
You may have found a bug.
Is this only happening with Async Loggers or also when you configure a plain (synchronous) logger?
Also, have you tried using a filePattern that looks like this:
filePattern="/apps/bea/mb-logs/$${date:yyyy-MM-dd}/router.%d{yyyy-MM-dd-HH}.log"?
I have a (admittedly vague) suspicion that the $${date:...} part might be related.
If neither of the above makes any difference, could you please file a Jira ticket on the log4j2 issue tracker? https://issues.apache.org/jira/browse/LOG4J2

Categories