首先需求slf4j-api-1.5.8.jar和slf4j-log4j12-1.5.8.jar包
然后在src下创建log4j.properties文件,添加
# # XXX GeeData Logging Configuration # log4j.rootLogger=info, stdout, R #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=%5p %d{yy-MM-dd HH:mm:ss} %c:%L - %m%n #file log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=logs/mylog.log #指定日志输出的路径 log4j.appender.R.Threshold=INFO log4j.appender.R.MaxFileSize=100MB log4j.appender.R.MaxBackupIndex=10 log4j.appender.R.layout=org.apache.log4j.PatternLayout # [u673Au5668ip|u65E5u5FD7u7EA7u522B|u65F6u95F4] [u7EBFu7A0Bu540D|u7C7Bu540D|u65B9u6CD5u540D|u884Cu53F7] [u4EA7u54C1id|u7248u672Cu53F7] message log4j.appender.R.layout.ConversionPattern=[%X{ip}|%p|%d{yyyy-MM-dd HH:mm:ss}] [%t|%C|%M|%L] [%X{productname}|%X{productversion}] - %m%n
在代码里面直接引用slf4j包,就可以用了
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CrTemplateAction extends BaseAction implements ModelDriven<CrTemplate>{ private static final Logger log = LoggerFactory.getLogger(CrTemplateAction.class); log.info("解码‘网站名称’出错。"); }