log4j2 DailyRollingFileAppende separate log files on roll based pid in actual log - java

I've implemented a plugin that adds to log information about pid.
appender.DRFA.layout.pattern = %d{ISO8601} %5p **[%pid]** [%t] %c{2}: %m%n
Also there is another plugin for FilePattern conversion that adds pid to file when rolling takes place
appender.DRFA.filePattern = ${sys:some.log.dir}/${sys:some.log.file}.%d{yyyy-MM-dd}.%pid
Trying to understand how to make next:
at the end of the day take all rows in log files that have specific pid and roll them to file with the same pid defined by pattern
appender.DRFA.filePattern = ${sys:some.log.dir}/${sys:some.log.file}.%d{yyyy-MM-dd}.%pid

Log4j2 rollover cannot combine multiple log files into a single zip file. Rollover can rename and/or compress a single log file. What you can do is move all log files with the same pid info the same directory:
appender.DRFA.filePattern = ${sys:some.log.dir}.%pid/${sys:some.log.file}.%d{yyyy-MM-dd}
If you need to compress multiple files together, or append these files together into a single text file, the built-in rollover cannot currently do that.
Take a look at the custom delete action that can be triggered on rollover.
You may be able to extend that into what you have in mind. Please raise a JIRA ticket if you need Log4j2 to make API changes for your custom rollover actions.

Related

Where on the client to place the log4j.properties file?

A Java application makes use of the Apache logging but am unable to turn it on.
The application is a Java Web Start application which uses a signed Jar file so I am unable to make any changes to the Jar file or the code.
A log4j.properties file with the following content was created and place in all the Java_Home/bin directories on my machine to ensure it will be picked up.
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=c:/MyLoggingDir/application6.log
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
log4j.appender.rollingFile.MaxFileSize=10MB
log4j.appender.rollingFile.MaxBackupIndex=5
log4j.appender.rollingFile.append=true
log4j.rootCategory=ALL, rollingFile
When the application starts no output is visible in the Java Console and no files are created in the c:/MyLoggingDir directory.
Is there another requirement missing that needs to be set in order to obtain output?
From the documentation of log4j:
Log4j will inspect the "log4j.configurationFile" system property and,
if set, will attempt to load the configuration using the
ConfigurationFactory that matches the file extension.
So, for example, if you put the property file in C:\Configuration\log4j.properties, you should run your application with the flag -Dlog4j.configuration="file:C:\Configuration\log4j.properties"
See also this other answer.

Why log4j renames filename?

I use the bellow codes.When I run project create MyLogFile.log file.
Next day I run project and log4j renames MyLogFile to
MyLogFile.log_Yesterday.log and create new MyLogFile file and start to write this file.
Why It doesn't crate MyLogFile.log_ToDay.log file?
Why it renames fileName?
Thank in advance
log4j.rootLogger=DEBUG, stdout
log4j.rootLogger=DEBUG, RollingAppender
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=d:/Logs/MyLogFile.log
log4j.appender.RollingAppender.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
The appender being used is org.apache.log4j.DailyRolling‌​FileAppender which takes a back up of the current log file whenever the date changes.
2017-08-29, The log file will be created with the name MyLogFile.log and logs will be written to it.
2017-08-30, Whenever the code encounters something to be logged, the appender will rename the file created on the previous day to MyLogFile_2017-08-29.log and create a fresh MyLogFile.log for the current day.
Please refer to the Javadoc for DailyRollingFileAppender for more details on how it works.

Seperate logs for different jars in war file

I have a war file which is deployed on jboss server. This war contains some jar files.
What i need is to create seperate logs for different jars.
I am using log4j for logging purpose and also intergrated log4j properties file in each jar.
My log4j for every jar have different paths.
Still the logs are generated in server.log of jboss.
I need help to generate seperate logs for every jar.
If you want to make different log file for different jar, you will have to configure it in your log4j.properties file related to every jar(I assume that every jar have different package naming convention).
Here is an example like if you want to log message in different file whose package name starts with org.jar1 and org.jar2.
log4j.debug=true
log4j.logger.org.jar1=DEBUG,jar1logger
log4j.logger.org.jar2=DEBUG,jar2logger
log4j.additivity.org.jar1=false
log4j.additivity.org.jar2=false
log4j.appender.jar1logger=org.apache.log4j.RollingFileAppender
log4j.appender.jar1logger.layout=org.apache.log4j.PatternLayout
log4j.appender.jar1logger.layout.ConversionPattern=%d [%t]<%-5p> %c -> %m%n
log4j.appender.jar1logger.File=/var/log/mylog/jar1.log
log4j.appender.jar1logger.Append=true
log4j.appender.jar1logger.MaxFileSize=1024KB
log4j.appender.jar1logger.MaxBackupIndex=5
log4j.appender.jar2logger=org.apache.log4j.RollingFileAppender
log4j.appender.jar2logger.layout=org.apache.log4j.PatternLayout
log4j.appender.jar2logger.layout.ConversionPattern=%d [%t]<%-5p> %c -> %m%n
log4j.appender.jar2logger.File=/var/log/mylog/jar2.log
log4j.appender.jar2logger.Append=true
log4j.appender.jar2logger.MaxFileSize=1024KB
log4j.appender.jar2logger.MaxBackupIndex=5
Based on your requirement whether you want to create log on daily basis or on size basis you have to change your configuration.
Keep in mind if any jar whose package name in any jars start with org.jar1 then all logs will be written into jar1.log file.

log4j - compress log file to gz on fly

Is it possible to setup log4j to create gziped log files without creating intermediate .log files? There are several rolling strategies (rolling by date, etc), but they creates unzipped files first.
You can create you own Appender extending org.apache.log4j.RollingFileAppender and overriding
the current logic by implementing your own optimized implementation version. Zip current file and discard rolling it to another log file (default implementation of RollingFileAppender). Example:
log4j.appender.{name}=br.com.sample.MyZipRollingFileAppender
You can "google" and search implementation examples using java.util.zip.ZipOutputStream or java.util.zip.GZIPOutputStream to zip the current file.
It is possible to solve this task using this Writer: http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/coyote/http11/filters/FlushableGZIPOutputStream.html
and a code like
Writer writer = new OutputStreamWriter(new FlushableGZIPOutputStream(newFileOutputStream(logFileName), LINES_TO_FLUSH));
appender.setWriter(writer);
It works but there are some drawbacks: compression is lower and it is not always possible to unzip this file. So I returned back to rotation.
You may try org.apache.log4j.rolling.TimeBasedRollingPolicy:
<appender...>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="/wombat/log.%d{yyyy-MM}**.gz**"/>
</rollingPolicy>
</appender>

Reduce logs of log4j in catalina.out

I am using the log4j API for logging in my Java based Tomcat application and using version 1.2.14 (log4j-1.2.14.jar). The problem is that it creates more logs in the catalina.out log file, which are very micro level logs and not required for me. I am facing the problem of memory in my machine, and I want to reduce the logging of the log4j logger. Is there a way to reduce logs such that I can save my memory?
Setting a properties or a XML configuration file will allow you to control log4j output as stated in the log4j manual.
An example of the properties file would be like this
# Root logger option
log4j.rootLogger=**ERROR**, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\mylogging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
Note the rootLogger parameter, as it controls how much information you get, whether you need only error messages or everything down to information messages.
In order to set it properly, look in Log4j Configuration Using Properties File for extra information as you can also use it in the XML version (see Multiple Appenders Using XML File). Tip: place it in src/ or in project root.
You will have to look at CATALINA_HOME/conf folder for log4j.properties and raise the logging level to say, warn or error depending upon your needs

Categories