Home >> Miscellaneous
Using Log4j How to write different types of log output to different files,
suppose normal debug information stored in a seperate file and more specific
process related audit information stored in a separate log file?
Most probable answer (Open for discussion by Reply) :
There can be many ways of doing this, one of the solution is by defining
log4j.properties in such a way so as to have two different Appenders,
with two different log files.
This is statically defined configuration, that is defining two different
Loggers as well, as follows:Here I am using Apache Log4J version 1.2.x.
1. Define rootLogger=DEBUG as
log4j.rootLogger=DEBUG
2. As two different types of loggers, here I define
these as DEFAULT_LOGGER and PROCESS_LOGGER,
|
|  |
|
log4j.logger.DEFAULT_LOGGER=INHERIT, A1
log4j.logger.PROCESS_LOGGER=INHERIT, A2
These two custome loggers are to be inherit from the root logger, and
two appenders A1 and A2 are attached to each of these loggers.
3. Now these two appenders A1 and A2 are to be set with respective
properties such as type of layout, patern and file name etc.
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=app.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.A1.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=audit_process.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Here default logger has daily rolling file appender, that means with
every system date change, it is going to create a separate log file with
complete date with timestamp.
And process logger has a simple file appender, with a specific pattern.
In order to test this log4j properties file, I have some sample Java code
as follows:
TestService.java
import org.apache.log4j.Logger;
public class TestService {
Logger logger1 = Logger.getLogger("DEFAULT_LOGGER");
Logger logger2 = Logger.getLogger("PROCESS_LOGGER");
public void testMethod() {
logger1.debug("Default Logger");
logger2.debug("Process Logger");
}
}
and the test client for calling this demo service as follows:
TestMultiLogger.java
public class TestMultiLogger {
public TestMultiLogger() {
new TestService().testMethod();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestMultiLogger();
}
}
As depicted in the TestService class file, here we have to call
appropriate logger for logging either default or process logger.
One question could arise that generally we pass class name
as the argument to the Logger.getLogger method?? but the concern class
name and method can be printed in the log file with appropriate
settings in log4j.properties file as follows:
....
....
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p
%C-%M %c - %m%n
....
....
Hope this helps, If you want to share some more suggestions or ways of
doing this, please write to us by replying to this write up.
After review and required changes made, if any, it will be published
in this page.
If anything missed out , please let me know at
techienjoy at yahoo . com
|
|
|
|
|
|
| Android Tab View Example : |
Example on Android Tab View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android ListView
explained with a very simple scenario
whereby showing folder and files with
structure and appropriate screens
captured and shown.
|
|
|
| Google GWT Example : |
Example using GWT and some design patterns and various
ways of implementing this example.
|
|
| Android Sensors Example : |
Example on Android Sensors Listed and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android ListView and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
| Android ListView Example : |
Example on Android List View
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
|
| Android Examples : |
List of ANDROid examples
with source code and output
screens captured and shown.
|
|
| Android Gallery Example : |
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
|
|
|