• log4j中将SocketAppender将日志内容发送到远程服务器


    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
  • 相关阅读:
    jQuery取得select选中的值
    超大文件上传和断点续传的组件
    局域网实现文件上传下载功能实例解析
    B/S实现文件上传下载功能实例解析
    网页实现文件上传下载功能实例解析
    VUE实现文件上传下载功能实例解析
    HTML5实现文件上传下载功能实例解析
    JavaScript实现文件上传下载功能实例解析
    JS实现文件上传下载功能实例解析
    HTML实现文件上传下载功能实例解析
  • 原文地址:https://www.cnblogs.com/JAYIT/p/6404348.html
Copyright © 2020-2023  润新知