Log4j2 - 当前日期文件追加不起作用

时间:2018-03-19 16:11:33

标签: java log4j2

我正在尝试使用附加的当前日期将当前日志保存到catalina日志中。

Log4j2.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
    <RollingFile name="CATALINA"
                 fileName   ="${sys:catalina.base}/logs/catalina.log"
                 filePattern="${sys:catalina.base}/logs/catalina.%d{yyyy-MM-dd}-%i.log">
        <PatternLayout pattern="[TID=%tid] %d %-5p %c- %m%n"/>
        <TimeBasedTriggeringPolicy modulate="true" />
    </RollingFile>
</Appenders>
<Loggers>
    <Root level="DEBUG" >
        <AppenderRef ref="CATALINA" />
    </Root>
</Loggers>
</Configuration>

但保存为catalina.log,没有当前日期。

我已经从log4j迁移到了我正在做这项工作的属性:

log4j.appender.CATALINA=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.CATALINA.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.CATALINA.RollingPolicy.FileNamePattern=${catalina.base}/logs/catalina.%d{yyyy-MM-dd}.log

由于代码使用JDK6,我使用的是log4j2版本2.3。

根据Vasan的建议,我已经转到catalina.properties并从log4j*.jar属性中移除了jarsToSkip,但当前日期并未附加到当前的catalina日志中。

Tomcat启动日志:

Mar 19, 2018 3:00:31 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 619 ms
Mar 19, 2018 3:00:31 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 19, 2018 3:00:31 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.22
Mar 19, 2018 3:00:31 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive monitor.war
Mar 19, 2018 3:00:31 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/local//apache-tomcat-7.0.22/webapps/msg-monitor/WEB-INF/lib/javax.servlet-3.0.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Mar 19, 2018 3:00:31 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/local/apache-tomcat-7.0.22/webapps/msg-monitor/WEB-INF/lib/servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Mar 19, 2018 3:00:31 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Mar 19, 2018 3:00:31 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Mar 19, 2018 3:00:31 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.web.context.support.XmlWebApplicationContext@5e1558dc: display name [Root WebApplicationContext]; startup date [Mon Mar 19 15:00:31 EDT 2018]; root of context hierarchy
Mar 19, 2018 3:00:31 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Mar 19, 2018 3:00:32 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@5e1558dc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2e7cf883
Mar 19, 2018 3:00:32 PM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from class path resource [properties/monitor.properties]
Mar 19, 2018 3:00:32 PM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#718ddc48' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Mar 19, 2018 3:00:32 PM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'dataSource' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Mar 19, 2018 3:00:32 PM org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory
INFO: Building JPA container EntityManagerFactory for persistence unit 'direct-msg-monitor-store'
Mar 19, 2018 3:00:33 PM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'entityManagerFactory' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Mar 19, 2018 3:00:33 PM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Mar 19, 2018 3:00:33 PM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'org.springframework.transaction.config.internalTransactionAdvisor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Mar 19, 2018 3:00:33 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2e7cf883: defining beans [org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,txJSONProvider,healthCheckResource,txsResource,aggregationDAOImpl,notificationDuplicationDAOImpl,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,dataSource,transactionManager,notificationDuplicationDAO,duplicationStateManager,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,generalTimeoutCondition,reliableTimeoutCondition,varaiableTimeoutCondition,generalCompletionCondition,reliableCompletionCondition,variableCompletionCondition,aggregationStrategy,msgIdCorrelator,textAssemblerFactory,textBodyGenerator,dsnGenerator,dsnMessageProcessor,smtpClientFactory,dsnSender,aggregationDAO,directMonitoringRepo,msgMonitoringProducerTemplate,consumerTemplate,camel-1:beanPostProcessor,camel-1]; root of factory hierarchy
Mar 19, 2018 3:00:34 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 2804 ms
Mar 19, 2018 3:00:35 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [1,265] milliseconds.
Mar 19, 2018 3:00:36 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet getContext
INFO: Using default applicationContext
Mar 19, 2018 3:00:36 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.6 03/25/2011 01:14 PM'
Mar 19, 2018 3:00:36 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive service.war

有什么线索我设置错了?

2 个答案:

答案 0 :(得分:2)

尝试使用以下配置在日志文件名中打印当前日期 -

     <RollingFile name="CATALINA"
                 fileName   ="${sys:catalina.base}/logs/catalina.${date:yyyy-MM-dd}.log"
                 filePattern="${sys:catalina.base}/logs/catalina.%d{yyyy-MM-dd}.log">
        <PatternLayout pattern="[TID=%tid] %d %-5p %c- %m%n"/>
        <TimeBasedTriggeringPolicy modulate="true" />
    </RollingFile>

答案 1 :(得分:1)

此版本的tomcat会跳过扫描与模式log4j * .jar匹配的任何JAR。该模式在与tomcat捆绑在一起的catalina.properties(在tomcat.util.scan.DefaultJarScanner.jarsToSkip属性中)中静态配置。

由于这个原因,它没有读取log4j2 ServletContainerInitializer,它为web应用程序设置了log4j2。因此,您需要更改属性文件以删除log4j *模式。

Reference