Configure log4j to write logs to local filesystem – FileAppender
Three main components that needs to be configured in log4j.properties file are:
1. logger => log messages.
2. appender => specifies the output destination like console or a file.
3. layout => specify the format in which the log messages should be logged.
When you configure using the BasicConfigurator.configure() method by default it uses the ConsoleAppender and PatternLayout for all the loggers.
The following configuration creates the same result as the BasicConfigurator.configure() method.
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n
In order to configure File logger, one needs to update log4j.properties file as follow:
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.FileAppender
log4j.appender.FILE.File=./log/log.out
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n
Other possible appenders (in addition to FileAppender) are:
AppenderSkeleton, AsyncAppender, ConsoleAppender, DailyRollingFileAppender, ExternallyRolledFileAppender, FileAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender, NullAppender, RollingFileAppender, SMTPAppender, SocketAppender, SocketHubAppender, SyslogAppender, TelnetAppender, WriterAppender
0 comments