本文介绍了日志文件旋转时应用程序未写入日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在使用spring log4j2,并使用slf4j api将日志写入单独的日志文件"application.log".

此应用程序与其他应用程序一起部署到了tomcat v8.所有应用程序都共享通用的log4j2配置,并写入通用日志文件'application.log'.

My app is using spring log4j2 and uses slf4j api to write log to separate log file "application.log".

This app gets deployed to tomcat v8 along with other apps. All app share common log4j2 configuration and writes to common logfile 'application.log'.

我们有250 mb的日志轮换策略,当日志文件轮换时,日志没有写入日志文件,只有一个令人惊讶所有应用程序中的app都可以写入日志文件.

We have a log rotation policy of 250 mb and when the log file rotates the logs are not written to the logfile surprising only one app among all the app is able to write to the log file.

另一个请在下面找到log4j2.xml配置.

The otherPlease find the log4j2.xml config below.

JAR版本

  • slf4j-api 1.7.21
  • log4j-slf4j-impl 2.5
  • log4j-api 2.5
  • log4j 核心 2.5
  • log4j-web 2.5

Log4j2.xml

Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="api-config" status="trace" monitorInterval="30">
  <Properties>
    <Property name="logdir">/Users/kramesan/microservices-config/logs</Property>
  </Properties>
  <Appenders>

    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%X{_requestId} %X{authToken} %X{urlEmployeeId} %X{urlCompanyId} [%X{authEmplIds}] [%X{authCompanyIds}] %d{yyy
y-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
    </Console>

    <RollingFile name="ApplicationLogRollingFile" fileName="${logdir}/application.log"
      filePattern="${logdir}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
      <JSONLayout locationInfo="true" complete="true" compact="true" eventEol="true" properties="true" />
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="250 MB" />
      </Policies>
      <DefaultRolloverStrategy max="20" />
    </RollingFile>

    <RollingFile name="AuditLogRollingFile" fileName="${logdir}/audit/api-audit.log"
      filePattern="${logdir}/audit/$${date:yyyy-MM}/api-audit-%d{MM-dd-yyyy}-%i.log.gz">
      <PatternLayout>
            <Pattern>%X{_requestId} %X{authToken} %X{urlEmployeeId} %X{urlCompanyId} [%X{authEmplIds}] [%X{authCompanyIds}] %m%n</Pattern>
      </PatternLayout>
      <!-- JSONLayout locationInfo="true" complete="true" compact="true" eventEol="true" properties="true" -->
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="250 MB" />
      </Policies>
      <DefaultRolloverStrategy max="20" />
    </RollingFile>
        <Async name="ApplicationLogAsync" bufferSize="262144">
         <AppenderRef ref="ApplicationLogRollingFile"/>
     </Async>

     <Async name="AuditLogAsync" bufferSize="262144">
         <AppenderRef ref="AuditLogRollingFile"/>
     </Async>

 </Appenders>
 <Loggers>
     <!--  All the 3rd Party frameworks -->
     <Logger name="org.springframework" level="warn" />

     <Logger name="org.hibernate" level="warn" />

<!-- common package name for all the business application level code -->
     <Logger name="com.trinet" level="info" />

     <!-- Audit Loggger This is used for spring aspect to log before and after execution -->
     <Logger name="AuditLogger" level="info">
         <AppenderRef ref="AuditLogAsync" />
     </Logger>

     <Root level="info">
         <AppenderRef ref="ApplicationLogAsync" />
     </Root>
 </Loggers>
</Configuration>

推荐答案

请编辑您的记录器名称.记录器名称属性需要包路径.

Please, edit your Logger name. Logger name property needs package path.

按现状

...
<Logger name="AuditLogger" level="info">
...

即将成为

...
<Logger name="com.foo.bar.AuditLogger" level="info" additivity="false">
...

...
<Logger name="com.foo.bar.*" level="info" additivity="false">
...

我认为您最好编写属性 additivity ,因为您的 Logger 工作两次 com.foo.bar.AuditLogger Logger 根记录器.因此您将 additivity 设置为 false ,然后每个都可以使用.

I think you'd better write property additivity Because your Logger works two times com.foo.bar.AuditLogger Logger and Root Logger. so you additivity to false then it works each.

参考链接:可加性

这篇关于日志文件旋转时应用程序未写入日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:39