1.先看log4j的配置文件:log4j.rootLogger=INFO,R,Clientlog4j.appender.R=org.apache.log4j.RollingFileAppenderlog4j.appender.R.File=./log/server.loglog4j.appender.R.MaxFileSize=5MBlog4j.appender.R.MaxBackupIndex=10log4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=[%5p] %d [%X{userName}] [%X{remoteIp}] %m%nlog4j.logger.Client=INFO,Clientlog4j.additivity.Client = falselog4j.appender.Client=org.apache.log4j.RollingFileAppenderlog4j.appender.Client.File=./log/client.loglog4j.appender.Client.MaxFileSize=5MBlog4j.appender.Client.MaxBackupIndex=10log4j.appender.Client.layout=org.apache.log4j.PatternLayoutlog4j.appender.Client.layout.ConversionPattern=[%5p] %d [%X{userName}] [%X{remoteIp}] %m%n此日志文件分为两个日志文件.一个是给服务器程序记录日志,另一个是给客户端程序记录日志的.红色部分是必须要加的.log4j.additivity.Client = false 确保写入client.log文件信息不重复出现到server.log文件中.log4j.logger.Client=INFO,Client 确保只对log4j的info信息感兴趣.2.再看代码的初使化://加入log4j支持static Logger logger =Logger.getLogger("Client");3.最后运行要记录的类.呵呵,轻松搞定.
时间: 2024-12-22 01:32:04