简体   繁体   English

我的自定义Log4j2 XML配置文件Java中的记录时间不正确

[英]Logging time incorrect in my custom Log4j2 XML configuration file Java

I am new to log4j2. 我是log4j2的新手。 Previously I am using log4j. 以前我正在使用log4j。 The reason I am migrating into part 2 is for Asynchronous logging. 我迁移到第2部分的原因是为了进行异步日志记录。 After searching Internet I am able to write a configuration file that actually creates two log files "Errors.log" and "Messages.log". 搜索Internet之后,我可以编写一个配置文件,该配置文件实际上创建两个日志文件“ Errors.log”和“ Messages.log”。 Now the problem is : I would be communicating with Servers that are kept far away from me. 现在的问题是:我将与远离我的服务器通信。 I wrote a client that communicates with the server and sends a request and in back the Server sends me a response. 我编写了一个与服务器通信并发送请求的客户端,然后服务器又向我发送了响应。 In any situation it takes at least 10 milli-seconds for the request to reach the Server and get back the response from it. 在任何情况下,请求都至少需要10毫秒才能到达服务器并从中获取响应。 But in my log files it is showing that the request sent to the Server and receive from the Server is at same time (Same milli-second). 但是在我的日志文件中,它显示发送到服务器和从服务器接收的请求是同时的(同一毫秒)。 I am using the Asynchronous logging. 我正在使用异步日志记录。 Is this causing the wrong timestamp? 这会导致错误的时间戳吗? or else the policies which I have used are creating these issues? 还是我所使用的政策造成了这些问题?

Below is my Log4j2 XML CONFIG file: 以下是我的Log4j2 XML CONFIG文件:

<?xml version="1.0" encoding="UTF-8"?> 
<Configuration status="warn">
  <Appenders>
  <File name="my_file_appender" fileName="LOG4j_LOGS/Errors.log" immediateFlush="false" append="false">
      <PatternLayout>
       <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n</Pattern>
      </PatternLayout>
    </File>
    <Async name="async_appender">
    <AppenderRef ref="my_file_appender" />
   </Async>
     <!-- file appender -->
     <RollingFile name="Error-log" fileName="LOG4j_LOGS/Messages.log"
                 filePattern="LOG4j_LOGS/Messages-%d{yyyy-MM-dd}.log">
      <!-- log pattern -->   
         <PatternLayout>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n</Pattern>
        </PatternLayout>
        <!-- set file size policy -->
        <Policies>
           <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="100 MB" />
        </Policies>
        <DefaultRolloverStrategy max="25"/>
    </RollingFile>   
  </Appenders>   
  <Loggers>
      <Logger name="Error-log" level="info" additivity="false">
       <appender-ref ref="Error-log" level="debug"/>
       </Logger>
       <Root level="info" includeLocation="false">
            <AppenderRef ref="async_appender"/>
      </Root>
  </Loggers>
</Configuration>

Can anyone please check my CONFIG file. 任何人都可以检查我的CONFIG文件。 All I want is to create two separate log files, one for storing info messages and other for storing errors. 我只想创建两个单独的日志文件,一个用于存储信息消息,另一个用于存储错误。 And this should create a new file every time I run my application and it should not delete the previous logs. 而且,这应该在我每次运行应用程序时创建一个新文件,并且不应删除以前的日志。 The size of the logs can be anything. 日志的大小可以是任何值。 If the size has exceeded it should create a new file and write the data into it. 如果超出大小,则应创建一个新文件并将数据写入其中。 No matter how many days I run the application the daily logs needs to be stored and the entire process has to be done in Asynchronously. 无论我运行该应用程序多少天,都需要存储每日日志,并且整个过程必须异步进行。

I am also using the below VM options for logging asynchronously : 我还使用以下VM选项进行异步记录:

 -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector

When logging is done asynchronously you log messages got onto a separate queue. 异步完成日志记录时,您会将消息记录到单独的队列中。 They are timestamped at the moment of processing by background thread that writes logs to disk (since timestamp is part of you Appender pattern). 它们在处理时由后台线程加时间戳,该后台线程将日志写入磁盘(因为时间戳是Appender模式的一部分)。 So only the order is preserved. 因此,仅保留订单。 Timestamps may be slightly different. 时间戳可能略有不同。

See https://logging.apache.org/log4j/2.x/manual/async.html for more info. 有关更多信息,请参见https://logging.apache.org/log4j/2.x/manual/async.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM