Log4j2.xml软件包外部的位置

时间:2019-05-06 10:47:11

标签: java spring-boot java-ee log4j

我需要将此文件log4j2-spring.xml放在包装之外。我在我的application.properties中配置以下行:

logging.config=C:\\example\\apache-tomcat-8.0.20\\webapps\\log4j2-spring.xml

我的log4j2.xml在“ C:\ example \ apache-tomcat-8.0.20 \ webapps \”中创建一个文件夹“ logs”。当我在资源中使用此文件时,效果很好,但是当我在外部使用时,则不起作用。

我该怎么做?

编辑:

<Appenders>

    <RollingFile name="TraceAppender" fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/Trace.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/Trace-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <Filters>
            <ThresholdFilter level="TRACE" />
            <ThresholdFilter level="DEBUG" onMatch="DENY"
                onMismatch="NEUTRAL" />
            <ThresholdFilter level="INFO" onMatch="DENY"
                onMismatch="NEUTRAL" />
            <ThresholdFilter level="WARN" onMatch="DENY"
                onMismatch="NEUTRAL" />
            <ThresholdFilter level="ERROR" onMatch="DENY"
                onMismatch="NEUTRAL" />
        </Filters>
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="Trace-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

    <RollingFile name="DebugAppender" fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/Debug.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/Debug-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <Filters>
            <ThresholdFilter level="DEBUG" />
            <ThresholdFilter level="INFO" onMatch="DENY"
                onMismatch="NEUTRAL" />
            <ThresholdFilter level="WARN" onMatch="DENY"
                onMismatch="NEUTRAL" />
            <ThresholdFilter level="ERROR" onMatch="DENY"
                onMismatch="NEUTRAL" />
        </Filters>
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="Debug-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

    <RollingFile name="DebugControllerAppender"
        fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/DebugController.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/DebugController-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <Filters>
            <ThresholdFilter level="DEBUG" />
            <ThresholdFilter level="INFO" onMatch="DENY"
                onMismatch="NEUTRAL" />
            <ThresholdFilter level="WARN" onMatch="DENY"
                onMismatch="NEUTRAL" />
            <ThresholdFilter level="ERROR" onMatch="DENY"
                onMismatch="NEUTRAL" />
        </Filters>
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="DebugController-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

    <RollingFile name="InfoAppender" fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/Info.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/Info-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <Filters>
            <ThresholdFilter level="INFO" />
            <ThresholdFilter level="WARN" onMatch="DENY"
                onMismatch="NEUTRAL" />
            <ThresholdFilter level="ERROR" onMatch="DENY"
                onMismatch="NEUTRAL" />
        </Filters>
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="Info-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

    <RollingFile name="WarnAppender" fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/Warn.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/Warn-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <Filters>
            <ThresholdFilter level="WARN" onMatch="ACCEPT"
                onMismatch="DENY" />
            <ThresholdFilter level="ERROR" onMatch="DENY"
                onMismatch="NEUTRAL" />
        </Filters>
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="Warn-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>

    </RollingFile>

    <RollingFile name="ErrorAppender" fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/Error.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/Error-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <ThresholdFilter level="ERROR" />
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="Error-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>

    </RollingFile>

    <RollingFile name="AllLogsAppender" fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/All.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/All-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <ThresholdFilter level="ALL" />
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="All-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

    <RollingFile name="HibernateAppender" fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/Hibernate.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/Hibernate-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <ThresholdFilter level="ERROR" onMatch="ACCEPT"
            onMismatch="DENY" />
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="Hibernate-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

    <RollingFile name="SpringAppender" fileName="C:/example/apache-tomcat-8.0.20/webapps/logs/Spring.log"
        filePattern="C:/example/apache-tomcat-8.0.20/webapps/logs/Spring-%d{yyyy-MM-dd}.log.gz"
        append="true">
        <ThresholdFilter level="ERROR" onMatch="ACCEPT"
            onMismatch="DENY" />
        <PatternLayout
            pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%C:%M:%L) - %m%n" />
        <TimeBasedTriggeringPolicy interval="1"
            modulate="true" />
        <DefaultRolloverStrategy>
            <Delete basePath="C:/example/apache-tomcat-8.0.20/webapps/logs/">
                <IfFileName glob="Spring-*.log.gz" />
                <IfLastModified age="P28D" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

    <Console name="Console">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
    </Console>

</Appenders>

<Loggers>

    <Logger name="org.springframework" level="WARN">
        <AppenderRef ref="TraceAppender" />
        <AppenderRef ref="ErrorAppender" />
        <AppenderRef ref="SpringAppender" />
    </Logger>

    <Logger name="org.hibernate" level="WARN">
        <AppenderRef ref="TraceAppender" />
        <AppenderRef ref="ErrorAppender" />
        <AppenderRef ref="HibernateAppender" />
    </Logger>

    <Logger name="es.example">
        <AppenderRef ref="ErrorAppender" />
        <AppenderRef ref="WarnAppender" />
        <AppenderRef ref="DebugAppender" />
        <AppenderRef ref="InfoAppender" />
    </Logger>

    <Logger name="es.example.glusilo.controller">
        <AppenderRef ref="DebugControllerAppender" />
    </Logger>

    <Logger name="com.mchange" level="WARN" />

    <Logger name="net.sf.ehcache" level="WARN" />

    <!-- All logs will be placed here (${log4j.logs.path}/All.log), from each 
        library, yours included -->
    <Root level="INFO">
        <AppenderRef ref="AllLogsAppender" />
        <AppenderRef ref="Console" />
    </Root>



</Loggers>

0 个答案:

没有答案