log4j2自定义配置文件位置和文件名(附log4j2.xml配置实例)

前言

我们使用log4j2一般做法是将log4j2.xml文件放在资源文件夹根目录。对于有强迫症的开发者来说,我更喜欢在资源文件夹下新建包或文件夹,然后把配置文件放在里面。本博客将介绍如何自定义log4j2.xml文件的位置和文件名。

web.xml配置

    
    <listener>
        <listener-class>edu.example.holder.system.Log4j2ConfigListenerlistener-class>
    listener>
    <context-param>
        <description>日志配置文件的路径description>
        <param-name>log4j.configurationFileparam-name>
        <param-value>log4j/log4j2.xmlparam-value>
    context-param>

Log4j2ConfigListener类是自定义的类,实现ServletContextListener接口,这样tomcat启动时可以更改日志配置文件的默认路径和文件名。

生效配置文件

package edu.example.holder.system;

import java.util.Enumeration;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.logging.log4j.core.config.Configurator;

public class Log4j2ConfigListener implements ServletContextListener
{
    private static final String KEY = "log4j.configurationFile";

    @Override
    public void contextDestroyed(ServletContextEvent arg0)
    {
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0)
    {
        String fileName = getContextParam(arg0);
        Configurator.initialize("Log4j2", "classpath:" + fileName);
    }

    @SuppressWarnings("unchecked")
    private String getContextParam(ServletContextEvent event) {
        Enumeration names = event.getServletContext().getInitParameterNames();
        while (names.hasMoreElements())
        {
            String name = names.nextElement();
            String value = event.getServletContext().getInitParameter(name);
            if(name.trim().equals(KEY))
            {
                return value;
            }
        }
        return null;
    }
}

日志配置文件实例




<Configuration status="WARN" monitorInterval="600">

    <Properties>
        
        <Property name="LOG_HOME">${sys:catalina.home}/WebAppLogs/SSHExampleProperty>
    Properties>

    <Appenders>

        
        <Console name="console_out_appender" target="SYSTEM_OUT">
            
            <ThresholdFilter level="warn" onMatch="DENY" onMismatch="ACCEPT"/>  
            
            <PatternLayout pattern="%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%n"/>
        Console>
        
        <Console name="console_err_appender" target="SYSTEM_ERR">
            
            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            
            <PatternLayout pattern="%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%n"/>
        Console>

        
        
        <RollingRandomAccessFile name="trace_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/trace.log"
                                 filePattern="${LOG_HOME}/trace/trace - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>

                
                <SizeBasedTriggeringPolicy size="2MB"/>

            Policies>
            <Filters>
                
                <ThresholdFilter level="debug" onMatch="DENY" onMismatch="NEUTRAL"/>  
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/> 
            Filters>
        RollingRandomAccessFile>

        
        
        <RollingRandomAccessFile name="debug_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/debug.log"
                                 filePattern="${LOG_HOME}/debug/debug - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>

                
                <SizeBasedTriggeringPolicy size="2MB"/>

                


            Policies>
            <Filters>
                
                <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>  
                <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/> 
            Filters>
        RollingRandomAccessFile>

        
        <RollingRandomAccessFile name="info_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/info.log"
                                 filePattern="${LOG_HOME}/info/info - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="2MB"/>
            Policies>
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>  
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> 
            Filters>
        RollingRandomAccessFile>

        
        <RollingRandomAccessFile name="warn_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/warn.log"
                                 filePattern="${LOG_HOME}/warn/warn - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="2MB"/>
            Policies>
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>  
                <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/> 
              Filters>
        RollingRandomAccessFile>

        
        <RollingRandomAccessFile name="error_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/error.log"
                                 filePattern="${LOG_HOME}/error/error - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="2MB"/>
            Policies>
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
              Filters>
        RollingRandomAccessFile>
    Appenders>

    <Loggers>
        
        <root level="trace">
            <appender-ref ref="console_out_appender"/>
            <appender-ref ref="console_err_appender"/>
            <appender-ref ref="trace_appender"/>
            <appender-ref ref="debug_appender"/>
            <appender-ref ref="info_appender"/>
            <appender-ref ref="warn_appender"/>
            <appender-ref ref="error_appender"/>
        root>

        
        <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.jboss.netty" level="warn"/>
        <logger name="org.apache.http" level="warn"/>

    Loggers>

Configuration>

结束语

本文章追求简洁时尚,不赘述。转载请注明出处,谢谢。

你可能感兴趣的:(Logger)