Spring log功能的加强

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:672491466; mso-list-type:hybrid; mso-list-template-ids:-1013438282 558681142 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1、; mso-level-tab-stop:39.0pt; mso-level-number-position:left; margin-left:39.0pt; text-indent:-18.0pt;} @list l1 {mso-list-id:752504879; mso-list-type:hybrid; mso-list-template-ids:-1005126670 -1949146052 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l1:level1 {mso-level-text:%1); mso-level-tab-stop:39.0pt; mso-level-number-position:left; margin-left:39.0pt; text-indent:-18.0pt;} @list l2 {mso-list-id:1601720335; mso-list-type:hybrid; mso-list-template-ids:427562604 -1949146052 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l2:level1 {mso-level-text:%1); mso-level-tab-stop:39.0pt; mso-level-number-position:left; margin-left:39.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

       Spring log4j 提供了很好的支持,并提供了 Log4jConfigListener 用于 log4j 标准配置文件的初始化和 2 个比较有意义的自身功能加强实现( log4j 配置信息定时读取并更新,日志文件路径动态配置)。

1、 具体配置

1) web.xml 文件添加下述内容

    <!— 设置 web 应用的 key ,通常为应用名称,在同一应用服务器下若部署了多个应用,那么此配置必须唯一 -->

< context-param >

       < param-name > webAppRootKey </ param-name >

       < param-value > web-app </ param-value >

    </ context-param >

    <!— 设置 log4j.properties 的路径 -->

    < context-param >

       < param-name > log4jConfigLocation </ param-name >

       < param-value > classpath:log4j.properties </ param-value >

    </ context-param >

    <!— 设置刷新时间间隔 -->

    < context-param >

       < param-name > log4jRefreshInterval </ param-name >

       < param-value > 10000 </ param-value >

    </ context-param >

 

2) log4j.properties 配置

log4j.rootLogger= debug,stdout

 

log4j.logger.org= info,orgLog

#log4j.appender.stdout config

log4j.appender.stdout= org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout= org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - %m%n

 

#tomcatLogFile config

log4j.appender.orgLog = org.apache.log4j.DailyRollingFileAppender

log4j.appender.orgLog.File= ${web-app} /logs/org_

log4j.appender.orgLog.DatePattern= yyyy-MM-dd'.log'

log4j.appender.orgLog.layout= org.apache.log4j.PatternLayout

log4j.appender.orgLog.layout.ConversionPattern= %d{yyyy-MM-dd HH : mm:ss} %p [%c] - %m%n

注意此处的 log4j.appender.tomcatLog.File= ${web-app} /logs/org_, 系统会将所有 org 开头的 class 的日志输出到当前 web 应用的 logs 目录下,文件名为 org_, 每天产生一个日志文件。

2、  日志功能加强

通常日志写入分两类:操作日志和错误日志,比如操作日志文件为 process_ 开头,错误日志文件以 error_ 开头,此时就需要我们写个 LogManager 加以处理。

1)  LogManager

package com.marcus.common;

import org.apache.log4j.Logger;

 

/**

  *

  * <p> Logger 的封装,定义了 ErrorLogger,ProcessLogger </p>

  * @author marcus

  * @version $Id: LogManager.java,v 0.1 2010- 5- 28 下午 04:57:38 Exp $

  */

public class LogManager {

    /**

      * 获取错误 Logger

      * @param logUrl

      * @return Logger

      */

    public static Logger getErrorLog(Object logUrl ){

       //logUrl is class

       if (logUrl instanceof Class ){

           return Logger.getLogger ( "error." +((Class )logUrl ).getName());

       }

       else if (logUrl instanceof String)

       {

           return Logger.getLogger ( "error." +logUrl );

       }

        else

           return Logger.getRootLogger ();

    }

   

    /**

      * 获取操作 Logger

      * @param logUrl

      * @return Logger

      */

    public static Logger getProcessLog(Object logUrl){

       //logUrl is class

       if (logUrl instanceof Class ){

           return Logger.getLogger ( "process." +((Class )logUrl).getName());

       }

       else if (logUrl instanceof String)

       {

           return Logger.getLogger ( "process." +logUrl);

       }

       else

           return Logger.getRootLogger ();

    }

}

 

2)  log4j.properties 配置修改,见红色部分

log4j.rootLogger= debug,stdout

 

log4j.logger.org= info,orgLog

log4j.logger.error= info,errorLog

log4j.logger.process= info,processLog

#log4j.appender.stdout config

log4j.appender.stdout= org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout= org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - %m%n

 

#tomcatLogFile config

log4j.appender.orgLog = org.apache.log4j.DailyRollingFileAppender

log4j.appender.orgLog.File= ${web-app} /logs/org_

log4j.appender.orgLog.DatePattern= yyyy-MM-dd'.log'

log4j.appender.orgLog.layout= org.apache.log4j.PatternLayout

log4j.appender.orgLog.layout.ConversionPattern= %d{yyyy-MM-dd HH : mm:ss} %p [%c] - %m%n

#error log config

log4j.appender.errorLog= org.apache.log4j.DailyRollingFileAppender

log4j.appender.errorLog.File= ${web-app} /logs/error_

log4j.appender.errorLog.DatePattern= yyyy-MM-dd'.log'

log4j.appender.errorLog.layout= org.apache.log4j.PatternLayout

log4j.appender.errorLog.layout.ConversionPattern= %d{yyyy-MM-dd HH : mm:ss} %p [%c] - %m%n

 

#process log config

log4j.appender.processLog= org.apache.log4j.DailyRollingFileAppender

log4j.appender.processLog.File= ${web-app} /logs/process_

log4j.appender.processLog.DatePattern= yyyy-MM-dd'.log'

log4j.appender.processLog.layout= org.apache.log4j.PatternLayout

log4j.appender.processLog.layout.ConversionPattern= %d{yyyy-MM-dd HH : mm:ss} %p [%c] - %m%n

3)  添加 process error 日志处理

package com.marcus.common;

 

import org.apache.log4j.Logger;

import com.whhercp.common.LogManager;

 

public class LoggerDemo {

    private Logger processLog = LogManager.getProcessLog (LesDemoServiceImpl. class );

    private Logger errorLog = LogManager.getErrorLog (LesDemoServiceImpl. class );

    public void writeLog(){

       try {

           processLog .info( "do action ..." );//write process log

           //write your action code here

       } catch (Exception e){

           errorLog .error(e.getMessage()); //write error log

       }

    }

}

阅读更多

扫码向博主提问

老马历写记

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 系统架构
  • 数据库
  • java
  • js前端
  • 全栈开发
去开通我的Chat快问
个人分类: Java
所属专栏: spring必知必会
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭