1.服务端配置
1)服务端配置文件log4j-server.properties
#Define a narrow log category. A category like debug will produce some extra logs also from server itself
log4j.rootLogger=ERROR, file
#Define how the socket server should store the log events
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=application-error.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] [%t] [%m]%n
2)服务端启动监听
java -classpath c:log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer
c:log4j-server.properties
注意,log4j-1.2.17.jar和配置文件都放在C盘,具体指定好位置即可,4712是监听的端口号。
2.客户端配置
1)客户端配置文件log4j.properties
#Define the log4j configuration for local application
log4j.rootLogger=ERROR, server
#We will use socket appender
log4j.appender.server=org.apache.log4j.net.SocketAppender
#Port where socket server will be listening for the log events
log4j.appender.server.Port=4712
#Host name or IP address of socket server
log4j.appender.server.RemoteHost=localhost
#Define any connection delay before attempting to reconnect
log4j.appender.server.ReconnectionDelay=10000
2)代码中使用
private static final Logger logger = LoggerFactory.getLogger(xx.class);
logger.error("test name:{}",guolei);
3.其他注意点
由于我们在项目中并不是要将所有的日志全部发送到远程服务器,而只是发送部分类中的,需要做特殊配置。如下:
log4j.logger.com.wanmei.marketing.RuleShowAction=INFO,server