关于服务器项目日志的一个便捷处理

来源:互联网 发布:如何不越狱下载软件 编辑:程序博客网 时间:2024/05/20 14:27
        发现运维同事的项目日志处理比较便利,值得借鉴。即使在开发环境中,这样处理也是不错的。
        比如项目 rtp 部署在了 /home/www/rtp 目录下。
        新建 /usr/userfile/logs 目录专门用来存放日志。
        新建 /home/www/defonds-config/rtp 目录专门用来存放配置文件(如 log4j.properties 等)。

        编辑 %tomcat%/conf 目录下的 catalina.properties,在结尾增加以下两行:

defonds.rtp.config.path=/home/www/defonds-config/rtpdefonds.rtp.log4j.path=/home/www/defonds-config/rtp

        编辑 rtp 项目的 web.xml:

<context-param><param-name>log4jConfigLocation</param-name><param-value>file:${defonds.rtp.log4j.path}/log4j.properties</param-value></context-param>

        如果有其他文件,如 config.properties,编辑 Spring 的 applicationContext.xml 如下:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations" value="file:${defonds.rtp.config.path}/config.properties"/></bean> 

        log4j.properties 内容如下:

log4j.rootLogger=info,RollingFilelog4j.appender.RollingFile=org.apache.log4j.RollingFileAppenderlog4j.appender.RollingFile.layout=org.apache.log4j.PatternLayoutlog4j.appender.RollingFile.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}][%p] - %m%nlog4j.appender.RollingFile.File=/usr/userfile/logs/rtp.loglog4j.appender.RollingFile.MaxFileSize=5MBlog4j.appender.RollingFile.MaxBackupIndex=20#rtplog4j.logger.com.defonds.rtp.test.service=DEBUG

        File 指定了日志输出到 /usr/userfile/logs/rtp.log,MaxFileSize 指定了当文件大小到达指定尺寸的时候产生新文件 rtp.log.1,MaxBackupIndex 指定了最大备份数。
        就是说,rtp.log 最大为 5MB,超过这个大小,老的数据放进备份文件 rtp.log.1,rtp.log.1 超出 5MB,其老的数据放进 备份文件 rtp.log.2……依次类推,最多有 20 个备份文件,即 rtp.log.20 产生的老数据丢弃。
        这样子,rtp.log 保存的永远是最新信息,rtp.log.1 保存的是旧一点的信息……rtp.log.20 保存的是最老的信息。
        log4j.logger.com.defonds.rtp.test.service 指定了 rtp com.defonds.rtp.test.service 包里的日志级别是 DEBUG,DEBUG 及以上优先级的日志将被输出。
        将 log4j.properties、config.properties 等配置文件放在 /home/www/defonds-config/rtp 目录。以后要修改这些文件就要来这里编辑了。
        重启 tomcat,日志处理生效。可以去 /usr/userfile/logs 目录下查看 rtp.log 的输出信息,当然也可以用 "tail -f rtp.log" 实时监测日志。
        这么基本做到了一劳永逸。不管你项目怎么重复部署,我的日志管理及日志查看不会受任何影响。
原创粉丝点击