log 4 j 2アプリケーション

17468 ワード

1.依存性の追加(pom.xml)
        
        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-apiartifactId>
            <version>2.11.0version>
        dependency>
        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-coreartifactId>
            <version>2.11.0version>
        dependency>
        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-1.2-apiartifactId>
            <version>2.11.0version>
        dependency>
        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-webartifactId>
            <version>2.11.0version>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.12version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-simpleartifactId>
            <version>1.7.25version>
        dependency>
        <dependency >
            <groupId >javax.mail groupId >
            <artifactId >mail artifactId >
            <version >1.4.5 version >
        dependency >
        <dependency >
            <groupId >com.sun.mail groupId >
            <artifactId >javax.mail artifactId >
            <version >1.5.4 version >
        dependency >

2.ログポリシーの構成(log 4 j 2.xml)

<Configuration status="WARN">
    <properties>
        <property name="logPath">/opt/tomcat7/logs/creditplatform/${sys:logName}property>
        <property name="errorLogPath">/opt/tomcat7/logs/creditplatform/error/${sys:logName}property>
    properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout charset="UTF-8" pattern="[%5p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n" />
        Console>
        <RollingFile name="debugFile" fileName="${logPath}" filePattern="${logPath}-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout charset="UTF-8" pattern="[%5p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="200 MB" />
            Policies>
            <DefaultRolloverStrategy max="20">
                <Delete basePath="${logPath}" maxDepth="2">
                    <IfFileName glob="*/app*.log.gz" />
                    <IfLastModified age="30D" />
                Delete>
            DefaultRolloverStrategy>
        RollingFile>
        
        <RollingFile name="errorFile" fileName="${errorLogPath}" filePattern="${errorLogPath}-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout charset="UTF-8" pattern="[%5p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="50MB"/>
            Policies>
        RollingFile>

        <SMTP name="Mail" subject="Error Log" to="[email protected]" from="[email protected]"
              smtpHost="smtp.mxhichina.com" smtpUsername="...." smtpDebug="true"
              smtpPassword="......" smtpPort="25" bufferSize="50">
        SMTP>
    Appenders>

    <Loggers>
        
        <logger name="org.springframework.core" level="info"/>
        <logger name="org.springframework.beans" level="info"/>
        <logger name="org.springframework.context" level="info"/>
        <logger name="org.springframework.web" level="info"/>
        <logger name="org.apache.http" level="warn"/>
        <logger name="org.apache.zookeeper" level="ERROR" additivity="false">logger>
        <Root level="DEBUG">
            <AppenderRef ref="debugFile" />
            <AppenderRef level="ERROR" ref="errorFile" />
            <AppenderRef ref="Console" />
            
        Root>
    Loggers>
Configuration>

3.ログ・ファイル名の動的変更(listenterでの構成のカスタマイズ)
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            //log4j2    
            System.setProperty("logName", InetAddress.getLocalHost().getHostName()+".log");
            File file = new File(this.getClass().getClassLoader().getResource("").getPath()+"log4j2.xml");
            LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
            ctx.setConfigLocation(file.toURI());
            ctx.reconfigure();           
        } catch (Exception e) {
           Log.error(e.getMessage(),e);
        }       
    }