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>
Related
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.
I was wondering if there is a way to define the appenders (file, console etc) on a different file than the one defining the actual logging properties.
The idea came up from a system I am developing and we have the following requirement:
Different versions of the system will be deployed on the same server. So in order not to maintain different log4j properties file, that will all set the same properties and differ on the file appenders (so as to know which log was recorded from which version of the system).
Thank you in advance
You can use DOMConfigurator or PropertyConfigurator to load your log4j settings from an external file. You can invoke this API multiple times under a run to load the settings from different sources.
In your case, you can load the Appender details alone dynamically from another property file based on the version.Just like suffixing some version id to the file name and loading it from your code in a generic way.
If each version running on a different VM process (on different ports), you can add an argument to the virtual machine. e.g.:
-Dmysystem.version=1.0.1
If you are using the XML configuration:
<param name="file" value="/logs/system.v${mysystem.version}.log" />
Of if you are using the properties:
log4j.appender.ROLL.File=/logs/system.v${mysystem.version}.log
In both cases, the generated file might be:
/logs/system.v1.0.1.log
In this way, you can maintain a single configuration file and dynamic filenames.
I am using log4j for my logs. My application has a simple XML configuration file and I need to have in my config file the path for the log file. At the moment, it's another XML configuration file for log4j that contains this:
<log4j:configuration>
<appender name="file" class="org.apache.log4j.FileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" />
<param name="file" value="log.out" />
(..........)
(log.out is the default log which is in the project's directory.) How can I move this configuration into my application's configuration file?
There are basically three options:
During the build, create a config for log4j which contains an absolute path for the file parameter. Least flexible.
Use a System property. While this gives you some flexibility, this causes problems when you run in a container (J2EE server) and you have several applications which all use log4j.
The second option comes in two flavors:
You can specify the path for the log file using ${logFile} in the XML and use -DlogFile= on the command line to specify the path.
You can keep the XML config in a different place and tell log4j to load it when it starts using -Dlog4j.configuration=/absolute/path/to/log4j.xml
Related articles:
http://www.jroller.com/rmcmahon/entry/system_properties_in_log4j_properties
Log4J change File path dynamically
Log4j: How to configure simplest possible file logging?
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/FileAppender.html
I have issue in executing the logs. I have two projects: one is a servlet and the other one is a simple java file. private static Logger logger=Logger.getLogger(myServiceServlet.class); This was working fine. Its writing log wen i start running the Tomcat server. But the same thing is not achieve in my simple java file, it converted into WS and try to deploy in axis/Tomcat server
Servlet is directly running in Tomcat server.
But java file is converted into Webservice.aar inside the Axis2--->Tomcat server.
Log Properites
# Log levels
log4j.rootLogger=INFO,CONSOLE,R
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=C:/res backup/apache-tomcat-6.0.35/webapps/mylog/logs/servicelog.log
log4j.appender.R.MaxFileSize=2048KB
# Number of backup files
log4j.appender.R.MaxBackupIndex=50
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n`
The .aar files are not running in the same context as the JARs in WEB-INF/lib.
They share some kind of context as you can load references to the WEB-INF/lib classes, however its not 100% consistent at this point.
I would recommend you build some kind of wrapper in your .aar files that do nothing else then forwarding the calls into methods of JAR files that are situated in WEB-INF/lib.
You just need to make sure you load the reference from the .aar to the methods in WEB-INF/lib context correctly.
To throw errors in the .aar classes I would recommend you catch them and then throw an "AxisFault".
throw new AxisFault(err.getMessage());
http://axis.apache.org/axis2/java/core/api/org/apache/axis2/AxisFault.html
That way we have have been able to solve almost all our issue with the context of .aar files.
Sebastian
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