配置log4j

如何配置使用Log4j

分类:java2013-12-1414:473519人阅读评论(2)收藏举报

javalog4j

目录(?)[+]

最近在用Java写一个FTP上传下载文件工具,正好需要产生操作日志。

利用Log4j.jar可以轻松解决。下面介绍一下Log4j的使用,具体概念到官网查阅:

-----------------------------------------------------------------------------

1、下载:

http://archive.apache.org/dist/logging/log4j/

2、添加jar到java项目:

3、创建:Log4j.Properties文件,放在src文件夹下即可:

3.1输出到console端:

#Rootloggeroption

log4j.rootLogger=INFO,stdout

#Directlogmessagestostdout

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-ddHH:mm:ss}%-5p%c{1}:%L-%m%n

3.2输出到日志文件:

#Rootloggeroption

log4j.rootLogger=INFO,file

#Directlogmessagestoalogfile

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-ddHH:mm:ss}%-5p%c{1}:%L-%m%n

3.3输出到console同时输出到日志文件:

#Rootloggeroption

log4j.rootLogger=INFO,file,stdout

#Directlogmessagestoalogfile

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-ddHH:mm:ss}%-5p%c{1}:%L-%m%n

#Directlogmessagestostdout

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-ddHH:mm:ss}%-5p%c{1}:%L-%m%n

3.4按天rollover(翻滚,转存)日志:

#Rootloggeroption

log4j.rootLogger=INFO,console,dailyFile

##Dailylog:

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-ddHH:mm:ss}%-5p-%m%n

log4j.appender.dailyFile.Threshold=INFO

log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd

#Directlogmessagestoconsole

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-ddHH:mm:ss}%-5p%c{1}:%L-%m%n

PS:

DebugLevel:日志记录等级

WehaveusedDEBUGwithboththeappenders.Allthepossibleoptionsare:

TRACE

DEBUG

INFO

WARN

ERROR

FATAL

ALL

DailyLogFileGeneration:每日文件生成模式

DatePatternDescription

'.'yyyy-MMRolloverattheendofeachmonthandthebeginningofthenextmonth.

'.'yyyy-MM-ddThisisthedefaultvalueandrolloveratmidnighteachday.

'.'yyyy-MM-dd-aRolloveratmiddayandmidnightofeachday.

'.'yyyy-MM-dd-HHRolloveratthetopofeveryhour.

'.'yyyy-MM-dd-HH-mmRollovereveryminute.

'.'yyyy-wwRolloveronthefirstdayofeachweekdependinguponthelocale.

4、测试代码如下:

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importorg.apache.log4j.Logger;

importorg.apache.log4j.BasicConfigurator;

publicclassHello{

staticLoggerlogger=Logger.getLogger(Hello.class);

public

static

voidmain(Stringargv[]){

BasicConfigurator.configure();

logger.debug("Helloworld.");

logger.info("Whatabeatifulday.");

}

}

Output:

2013-12-1413:02:06ERRORHello:12-Helloworld.

2013-12-1413:02:06INFOHello:13-Whatabeatifulday.

----------------------------------------

Dylanpresents.

感谢http://blog.csdn.net/indexman/article/details/17318817

相关推荐