如何配置使用 Log4j
最近在用Java 写一个FTP上传下载文件工具,正好需要产生操作日志。
利用Log4j.jar可以轻松解决。下面介绍一下Log4j的使用,具体概念到官网查阅:
-----------------------------------------------------------------------------
1、下载:
http://archive.apache.org/dist/logging/log4j/
2、添加jar到java项目:
3、创建:Log4j.Properties文件,放在src文件夹下即可:
3.1 输出到console端:
# Root logger option log4j.rootLogger=INFO, stdout # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n3.2 输出到日志文件:
# Root logger option log4j.rootLogger=INFO, file # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\loging.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n3.3 输出到console同时输出到日志文件:
# Root logger option log4j.rootLogger=INFO, file, stdout # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\loging.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n3.4 按天roll over(翻滚,转存)日志:
# Root logger option log4j.rootLogger=INFO,console,dailyFile ##Daily log: log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.dailyFile.File=e:/ftplog/autoftp.log log4j.appender.dailyFile.Append=true log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout log4j.appender.dailyFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n log4j.appender.dailyFile.Threshold=INFO log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd # Direct log messages to console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Threshold=INFO log4j.appender.console.ImmediateFlush=true log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%nPS:
Debug Level: 日志记录等级
We have used DEBUG with both the appenders. All the possible options are:
-
TRACE
-
DEBUG
-
INFO
-
WARN
-
ERROR
-
FATAL
-
ALL
Daily Log File Generation: 每日文件生成模式
DatePattern | Description |
---|---|
'.' yyyy-MM | Roll over at the end of each month and the beginning of the next month. |
'.' yyyy-MM-dd | This is the default value and roll over at midnight each day. |
'.' yyyy-MM-dd-a | Roll over at midday and midnight of each day. |
'.' yyyy-MM-dd-HH | Roll over at the top of every hour. |
'.' yyyy-MM-dd-HH-mm | Roll over every minute. |
'.' yyyy-ww | Roll over on the first day of each week depending upon the locale. |
4、测试代码如下:
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class Hello {
static Logger logger = Logger.getLogger(Hello.class);
public
static
void main(String argv[]) {
BasicConfigurator.configure();
logger.debug("Hello world.");
logger.info("What a beatiful day.");
}
}
Output:
2013-12-14 13:02:06 ERROR Hello:12 - Hello world.
2013-12-14 13:02:06 INFO Hello:13 - What a beatiful day.
----------------------------------------
Dylan presents.