【Log 4 j 2】Log 4 jプロパティファイル構成の詳細

10782 ワード

以下にlog 4 jを示す.propertiesの構成
 
log4j.rootCategory=INFO, stdout , R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=/home/tom/logs/logs.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

log4j.logger.com.tom=INFO, tom
log4j.appender.tom=org.apache.log4j.DailyRollingFileAppender
log4j.appender.tom.File=/home/tom/logs/tom.log
log4j.appender.tom.layout=org.apache.log4j.PatternLayout
log4j.appender.tom.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n


log4j.logger.pck=INFO, pck
log4j.appender.pck=org.apache.log4j.DailyRollingFileAppender
log4j.appender.pck.File=/home/tom/logs/pck.log
log4j.appender.pck.layout=org.apache.log4j.PatternLayout
log4j.appender.pck.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

 
このプロファイルにはroot,comの3つのloggerが定義.tomおよびpck、ログファイルはそれぞれlogsに印刷される.log,tom.logおよびpck.log.
 
次の3つのテストクラスを定義します.
package com.tom.log4j;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

public class Log4jTest {

    @org.junit.Test
    public void test1() {
        Log log = LogFactory.getLog("tom");
        log.info("Hello,test1");//   logs.log
    }

    @org.junit.Test
    public void test2() {
        Logger logger = Logger.getLogger(Log4jTest.class);
        logger.info("Hello, test2");//   logs.log  tom.log
    }

    @org.junit.Test
    public void test3() {
        Logger logger = Logger.getLogger("pck"); //   logs.log  pck.log
        logger.info("Hello, test3");
    }
}

 
test 1結果分析
  • root loggerはすべてのログに作用するので、root loggerに対応するログファイルlogsに印刷されます.log
  • 構成にはtomというログ名のloggerが構成されていないため、root loggerのみがこのログ
  • を記録する.
  • 以下に定義するログ名はtomではなくcomである.tom、だからgetLogger(「tom」)を通じて、このログは選択されず、tomに印刷されません.log

  •  
    log4j.logger.com.tom=INFO, tom
     
     
    test 2結果分析
  • root loggerはすべてのログに作用するので、root loggerに対応するログファイルlogsに印刷されます.log
  • Logger.getLogger(Log 4 jTest.class)は、Log 4 jTestクラスファイルの全限定名(com.tom.log 4 j.Log 4 jTest)でログを検索します.ログプロファイルにはcomという名前が定義されていないためです.tom.log4j.Log 4 jTestのlogger、それはどのようにtomに書きます.ロゴファイルの中は?これがLog 4 jのloggerの継承関係、例えばLogger.getLogger(「X.Y.Z」)では、機能するloggerには、X.Y.Z、X.Y、X、rootが含まれます.下から上へ見つけて停止するのではなく、それ自体からrootまでのすべてのloggerを探し出し、各loggerの構成に基づいてログ印刷を行います.
  • test 2でロガーgetLogger(「com.tom.log 4 j.Log 4 jTest」)はcomを見つけます.tomとrootの2つのloggerなので、それぞれtomに印刷する.logとlogs.log

  • test 3結果分析
  • root loggerはすべてのログに作用するので、root loggerに対応するログファイルlogsに印刷されます.log
  • は、構成中にpckという名前のloggerがあるため、このloggerも選択するのでpckに印刷される.log中
  • まとめ
    test 2に示すloggerの継承特性は非常に有用であり、Spring,Strutsなどの第三者のフレームワークをプロジェクトに使用する場合、Spring,Strutsのログを記録するためにlog 4 jでよい.propeties 2行追加
     
    org.apache.struts=ERROR
    org.springframework=WARN

     
    別のファイルに記録することもできます.たとえば、次のようにします.
    log4j.logger.org.apache.struts=INFO, struts
    log4j.appender.struts=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.struts.File=/home/tom/logs/struts.log
    log4j.appender.struts.layout=org.apache.log4j.PatternLayout
    log4j.appender.struts.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

     
    义齿
     
    Conversion Character Effect
    c
    Used to output the category of the logging event. The category conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the category name will be printed. By default the category name is printed in full. For example, for the category name "a.b.c"the pattern %c{2} will output "b.c".
    C
    Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form. For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass". WARNING Generating the caller class information is slow. Thus, use should be avoided unless execution speed is not an issue.
    d
    Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. If no date format specifier is given then ISO8601 format is assumed. The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat . Although part of the standard JDK, the performance of SimpleDateFormat is quite poor. For better results it is recommended to use the log4j date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE"and "ISO8601"for specifying AbsoluteTimeDateFormat , DateTimeDateFormat and respectively ISO8601DateFormat . For example, %d{ISO8601} or %d{ABSOLUTE}. These dedicated date formatters perform significantly better than SimpleDateFormat .
    F
    Used to output the file name where the logging request was issued. WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
    l
    Used to output location information of the caller which generated the logging event. The location information depends on the JVM implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses. The location information can be very useful. However, its generation is extremely slow and should be avoided unless execution speed is not an issue.
    L
    Used to output the line number from where the logging request was issued. WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
    m
    Used to output the application supplied message associated with the logging event.
    M
    Used to output the method name where the logging request was issued. WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
    n
    Outputs the platform dependent line separator character or characters. This conversion character offers practically the same performance as using non-portable line separator strings such as "", or "\r". Thus, it is the preferred way of specifying a line separator.
    p
    Used to output the priority of the logging event.
    r
    Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.
    t
    Used to output the name of the thread that generated the logging event.
    x
    Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.
    X
    Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by the key for the map placed between braces, as in %X{clientNumber} where clientNumber is the key. The value in the MDC corresponding to the key will be output. See MDC class for more details.
    %
    The sequence %% outputs a single percent sign.
     
     
    log 4 jのmaven依存
     
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.2</version>
            </dependency>
    
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>

     
    slf 4 jのコードを使用する:
     
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    private final Logger LOG = LoggerFactory.getLogger("mylogger");

     
     
    最初の行の最初のhc 4はloggerの名前で、2番目のhc 4はloggerの構成別名で、appender.hc 4はいずれも代の2番目のhc 4.を指す.
    この書き方にはメリットがあります.
    log4j.logger.org.apache.http=INFO,httpはhttpでorgを指すことができる.apache.httpというloggerは後の構成を行います
    log4j.logger.hc4=INFO,hc4
    log4j.appender.hc4=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.hc4.Threshold=DEBUG
    log4j.appender.hc4.file=C:/http.client.logs/hc4.log
    log4j.appender.hc4.DatePattern='.'yyyy-MM-dd
    log4j.appender.hc4.layout=org.apache.log4j.PatternLayout
    log4j.appender.hc4.layout.ConversionPattern=[%-5p] [%d{yyyy-MM-dd HH:mm:ss}] [%C{1}:%M:%L] %m%n
    log4j.additivity.hc4=false