tomcat8/9集成log4j2

1、下载tomcat8.0.53 中的插件tomcat-juli.jar和tomcat-juli-adapters.jar两个jar把,经过测试,这两个jar适用于tomcat8.0、8.5、9.0
2、下载log4j-1.2-api-2.11.1.jar、log4j-api-2.11.1.jar、log4j-core-2.11.1.jar三个jar包,此处注意log4j2有bug,
笔者第一次使用2.8.2三个jar包,启动tomcat后写入文件的日志正常,但是控制台输出一直报错,错误内容如下:
2019-01-24 14:05:00,504 main ERROR Unable to inject fields into builder class for plugin type class org.apache.logging.log4j.core.appender.ConsoleAppender, element Console. java.nio.charset.UnsupportedCharsetException: cp65001
        at java.nio.charset.Charset.forName(Charset.java:531)
        at org.apache.logging.log4j.util.PropertiesUtil.getCharsetProperty(PropertiesUtil.java:146)
        at org.apache.logging.log4j.util.PropertiesUtil.getCharsetProperty(PropertiesUtil.java:134)
        at org.apache.logging.log4j.core.appender.ConsoleAppender$Target.getCharset(ConsoleAppender.java:85)
        at org.apache.logging.log4j.core.appender.ConsoleAppender$Target$1.getDefaultCharset(ConsoleAppender.java:71)
        at org.apache.logging.log4j.core.appender.ConsoleAppender$Builder.build(ConsoleAppender.java:218)
        at org.apache.logging.log4j.core.appender.ConsoleAppender$Builder.build(ConsoleAppender.java:185)
        at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:952)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:892)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:884)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:508)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:232)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:244)
        at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:545)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
        at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
        at org.apache.logging.log4j.LogManager.getContext(LogManager.java:307)
        at org.apache.log4j.Logger$PrivateManager.getContext(Logger.java:59)
        at org.apache.log4j.Logger.getLogger(Logger.java:37)
        at org.apache.juli.logging.impl.Log4JLogger.getLogger(Log4JLogger.java:262)
        at org.apache.juli.logging.impl.Log4JLogger.<init>(Log4JLogger.java:108)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.apache.juli.logging.impl.LogFactoryImpl.createLogFromClass(LogFactoryImpl.java:1025)
        at org.apache.juli.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:844)
        at org.apache.juli.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:541)
        at org.apache.juli.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:292)
        at org.apache.juli.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:269)
        at org.apache.juli.logging.LogFactory.getLog(LogFactory.java:657)
        at org.apache.catalina.startup.Catalina.<clinit>(Catalina.java:824)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.apache.catalina.startup.Bootstrap.init(Bootstrap.java:264)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:461)

2019-01-24 14:05:00,520 main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console. java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.ConsoleAppender
        at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:224)
        at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:130)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:952)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:892)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:884)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:508)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:232)
        at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:244)
        at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:545)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
        at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
        at org.apache.logging.log4j.LogManager.getContext(LogManager.java:307)
        at org.apache.log4j.Logger$PrivateManager.getContext(Logger.java:59)
        at org.apache.log4j.Logger.getLogger(Logger.java:37)
        at org.apache.juli.logging.impl.Log4JLogger.getLogger(Log4JLogger.java:262)
        at org.apache.juli.logging.impl.Log4JLogger.<init>(Log4JLogger.java:108)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.apache.juli.logging.impl.LogFactoryImpl.createLogFromClass(LogFactoryImpl.java:1025)
        at org.apache.juli.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:844)
        at org.apache.juli.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:541)
        at org.apache.juli.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:292)
        at org.apache.juli.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:269)
        at org.apache.juli.logging.LogFactory.getLog(LogFactory.java:657)
        at org.apache.catalina.startup.Catalina.<clinit>(Catalina.java:824)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.apache.catalina.startup.Bootstrap.init(Bootstrap.java:264)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:461)

2019-01-24 14:05:00,566 main ERROR Null object returned for Console in Appenders.
2019-01-24 14:05:00,576 main ERROR Unable to locate appender "CONSOLE" for logger config "root"
 经在百度查找发现是log4j2的bug,该bug在2.8.2以后修改,提示升级到最新版的log4j2,因此笔者升级到2.11.1后就正常了。
3、将这5个jar拷贝到tomcat/lib目录下
4、将tomcat-juli.jar覆盖到tomcat/bin目录下
5、删除tomcat/conf下logging.properties文件
6、在tomcat/lib目录下创建log4j.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">

    <Properties>
        <Property name="CATALINA_BASE_LOGPATH">../logs</Property>         
    </Properties>

    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <patternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}#%p#%t#%c#%C{1}#%F#%M#%L#%r#%m%n" />
        </Console>
		
        <RollingFile name="CATALINA" fileName="${CATALINA_BASE_LOGPATH}/catalina.log"
            filePattern="${CATALINA_BASE_LOGPATH}/catalina.%date{yyyy-MM-dd}.%i.log" append="true" immediateFlush="true">
            <patternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}#%p#%t#%c#%C{1}#%F#%M#%L#%r#%m%n" />
            <policies>
                <timeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </policies>
            <DefaultRolloverStrategy fileIndex="min" max="20"/>
        </RollingFile>

        <RollingFile name="LOCALHOST" fileName="${CATALINA_BASE_LOGPATH}/localhost.log"
            filePattern="${CATALINA_BASE_LOGPATH}/localhost.%date{yyyy-MM-dd}.%i.log" append="true" immediateFlush="true">
            <patternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}#%p#%t#%c#%C{1}#%F#%M#%L#%r#%m%n" />
            <policies>
                <timeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </policies>
            <DefaultRolloverStrategy fileIndex="min" max="20"/>
        </RollingFile>

        <RollingFile name="MANAGER" fileName="${CATALINA_BASE_LOGPATH}/manager.log"
            filePattern="${CATALINA_BASE_LOGPATH}/manager.%date{yyyy-MM-dd}.%i.log" append="true" immediateFlush="true">
            <patternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}#%p#%t#%c#%C{1}#%F#%M#%L#%r#%m%n" />
            <policies>
                <timeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </policies>
            <DefaultRolloverStrategy fileIndex="min" max="20"/>
        </RollingFile>

        <RollingFile name="HOST-MANAGER" fileName="${CATALINA_BASE_LOGPATH}/host-manager.log"
            filePattern="${CATALINA_BASE_LOGPATH}/host-manager.%date{yyyy-MM-dd}.%i.log" append="true" immediateFlush="true">              
            <patternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}#%p#%t#%c#%C{1}#%F#%M#%L#%r#%m%n" />
            <policies>
                <timeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </policies>
            <DefaultRolloverStrategy fileIndex="min" max="20"/>
        </RollingFile>             

    </Appenders>

    <Loggers>
        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="INFO" additivity="false">
            <appender-ref ref="LOCALHOST" />     
        </Logger>

        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]" level="INFO" additivity="false"> 
            <appender-ref ref="MANAGER" />
        </Logger>

        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]" level="INFO"  additivity="false">
			<appender-ref ref="HOST-MANAGER" />   
        </Logger>

        <Root level="INFO" additivity="false">
			<AppenderRef ref="CONSOLE" level="INFO"/>
            <AppenderRef ref="CATALINA" level="INFO"/>
        </Root>
    </Loggers>

</Configuration>