Example2:(log
msgs are printed on console using ConsoleAppender class)
Write a sample program for using ConsoleAppender and PatternLayout
follow conversion pattern format.
log4j.properties
log4j.rootLogger=INFO,CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r
%d [%t] %C %-5p %m %n
|
Sample.java
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:
0 2014-02-14 15:09:10,020
[main] com.dustbin.test.Sample INFO info
message
16 2014-02-14 15:09:10,036
[main] com.dustbin.test.Sample WARN warn
message
16 2014-02-14 15:09:10,036
[main] com.dustbin.test.Sample ERROR error message
16 2014-02-14 15:09:10,036
[main] com.dustbin.test.Sample FATAL fatal message
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: c:\logs.log (A required
privilege is not held by the client)
0 comments:
Post a Comment