PropertyConfigurator:
If we
create a properties file (.properties), then we should use Property Configurator class for loading that file.
You need to use the PropertyConfigurator.configure() method
to configure log4j using a properties file.Log4j should be configured only once
for the entire application.
Note:- properties file contains only key and value.
Example 1:-
Program
for using ConsoleAppender and SimpleLayout from DEBUG level.
log4j.properties
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.SimpleLayout
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:
INFO - info message
WARN - warn message
ERROR - error message
FATAL - fatal message
DEBUG - debug msg
Some
times we get below type exception ....
log4j:ERROR Could not read configuration file
[C:/log/log4j.properties].
java.io.FileNotFoundException: C:\log\log4j.properties
(The system cannot find the path specified)
Note:-specify absolute path of the log4j.properties
file
0 comments:
Post a Comment