Example #3 : Here, we are writing logs to console as well as to a file "example.log".
Sample Code
/*
* LogWithFileConfigure.java
* Here, we are reading the configuration settings from
* a text file "MultiAppender.properties". Here, we are
* writing logs to the console as well to a file "example.log".
*
* By : Javapandit
*/
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LogWithFileConfigure {
static Logger logger = Logger.getLogger("xyz");
public static void main(String[] args) {
//reading the configuration from file "MultiAppender.properties"
PropertyConfigurator.configure("MultiAppender.properties");
logger.info("Entering application.");
logger.warn("Application is running.");
logger.info("Exiting application.");
}//main()
}
The confiuration file MultiAppender.properties is shown here -
Sample Code
MultiAppender.properties
------------------------
# Set root logger level to DEBUG and add 2 appenders -
# A1 : for writing to the console.
# A2 : for writing to the file.
log4j.rootLogger=debug, A1, A2
# define the settings for appender "A1".
# We want to write logs to the console. Hence, A1 is set
# to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# Set the layout of A1 (uses PatternLayout).
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#define the settings for appender "A2".
#A2 is for writing to the file.
log4j.appender.A2=org.apache.log4j.RollingFileAppender
#set the log file name.
log4j.appender.A2.File=example.log
#set the maximum file size.
log4j.appender.A2.MaxFileSize=100KB
# Keep one backup file
log4j.appender.A2.MaxBackupIndex=1
#set the layout of A2.
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%p %t %c - %m%n
Sample Code
/*
* LogWithFileConfigure.java
* Here, we are reading the configuration settings from
* a text file "MultiAppender.properties". Here, we are
* writing logs to the console as well to a file "example.log".
*
* By : Javapandit
*/
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LogWithFileConfigure {
static Logger logger = Logger.getLogger("xyz");
public static void main(String[] args) {
//reading the configuration from file "MultiAppender.properties"
PropertyConfigurator.configure("MultiAppender.properties");
logger.info("Entering application.");
logger.warn("Application is running.");
logger.info("Exiting application.");
}//main()
}
The confiuration file MultiAppender.properties is shown here -
Sample Code
MultiAppender.properties
------------------------
# Set root logger level to DEBUG and add 2 appenders -
# A1 : for writing to the console.
# A2 : for writing to the file.
log4j.rootLogger=debug, A1, A2
# define the settings for appender "A1".
# We want to write logs to the console. Hence, A1 is set
# to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# Set the layout of A1 (uses PatternLayout).
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#define the settings for appender "A2".
#A2 is for writing to the file.
log4j.appender.A2=org.apache.log4j.RollingFileAppender
#set the log file name.
log4j.appender.A2.File=example.log
#set the maximum file size.
log4j.appender.A2.MaxFileSize=100KB
# Keep one backup file
log4j.appender.A2.MaxBackupIndex=1
#set the layout of A2.
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%p %t %c - %m%n