Example3:( printed log messages using FileAppender class into file)
Approach1:
develop a log messages properties file by using
FileAppender class
step 1: develop a .properties file
like
using property file externally.
log4j.properties
log4j.rootLogger=INFO,CA
log4j.appender.CA=org.apache.log4j.FileAppender
log4j.appender.CA=d:\logs.txt
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r %d [%t] %C
%-5p %m %n
step2: develop a java class
package
com.dustbin.test;
import
org.apache.log4j.Logger;
import
org.apache.log4j.PropertyConfigurator;
public
class
Sample {
static
Logger logger=Logger.getLogger(Sample.class);
public
static
void
main(String[] args) {
PropertyConfigurator.configure("C:/logs/log4j.properties");
logger.info("info
message");
logger.warn("warn
message");
logger.error("error
message");
logger.fatal("fatal
message");
logger.debug("debug
msg");
}
}
output:
logging messages are printed on file
location d:\logs.txt
0 2014-02-14
15:23:54,208 [main] com.dustbin.test.Sample INFO info message
0 2014-02-14
15:23:54,208 [main] com.dustbin.test.Sample WARN warn message
0 2014-02-14
15:23:54,208 [main] com.dustbin.test.Sample ERROR error message
0 2014-02-14
15:23:54,208 [main] com.dustbin.test.Sample FATAL fatal message
0 comments:
Post a Comment