public class ClassLog
{
static Logger logger = Logger.getLogger(ClassLog2.class.getName());
public ClassLog()
{
}
public static void main(String[] args)
{
PropertyConfigurator.configure
("C:\\eclipse\\workspace\\mytest\\log4jtest\\log4j.properties");
logger.debug("Start of the main() in classlog");
logger.info("Just testing a log message with priority set to INFO");
logger.warn("Just testing a log message with priority set to WARN");
logger.error("Just testing a log message with priority set to ERROR");
logger.fatal("Just testing a log message with priority set to FATAL");
logger.log(Priority.WARN,"Testing a log message use a alternate form");
logger.debug(ClassLog2.class.getName());
ClassLog2 cl2 = new ClassLog2();
cl2.log();
}
}
public class ClassLog2
{
static Logger logger = Logger.getLogger(ClassLog2.class.getName());
public ClassLog2()
{
}
public void log()
{
PropertyConfigurator.configure("C:\\eclipse\\workspace\\mytest\\log4jtest\\log4j.properties");
logger.debug("Start of the main() in classlog2");
logger.info("Just testing a log message with priority set to INFO");
logger.warn("Just testing a log message with priority set to WARN");
logger.error("Just testing a log message with priority set to ERROR");
logger.fatal("Just testing a log message with priority set to FATAL");
logger.log(Priority.WARN,"Testing a log message use a alternate form");
logger.debug(ClassLog2.class.getName());
}
}
#2区
#Print only messages of priority WARN or higher for your category
log4j.logger.log4jtest.ClassLog=WARN,stdout,R
#log4j.logger.log4jtest.ClassLog=WARN
log4j.logger.log4jtest.ClassLog2=DEBUG,S
#log4j.logger.log4jtest.ClassLog2=DEBUG
#3区
#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#4区
#### Second appender writes to a file
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=c:\\example.log2
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
log4j.appender.stdout2=org.apache.log4j.ConsoleAppender
log4j.appender.stdout2.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout2.layout.ConversionPattern=[%t] (%F:%L) - %m%n
log4j.appender.S=org.apache.log4j.RollingFileAppender
log4j.appender.S.File=c:\\example.log
# Control the maximum log file size
log4j.appender.S.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.S.MaxBackupIndex=1
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n