log4j转换模式的时间戳与微秒数[英] log4j ConversionPattern timestamp with microseconds

本文是小编为大家收集整理的关于log4j转换模式的时间戳与微秒数的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想在使用log4j生成的日志文件的每个条目的时间戳中添加微秒?

是否可能?

我已经在官方文档中搜索过,但是在毫秒下方没有提及单位.

现在我有一个转换模式,如下:

%d{dd/MM/yyyy HH\:mm\:ss,SSS} %-5p [%t] - %m%n

日期转换模式(%d )中,我想在毫秒后添加 microseconds ( sss ),是否有一种方式去做吧 ?

推荐答案

如果要显示微秒,则需要自己添加.这可以使用自定义格式化器(而不是pattern formatter)

来完成

其他推荐答案

从 Java 9 和 log4j 2.11.0 开始,即使使用纳米秒,也可以获得时间戳.

以下是特殊的预定义模式,允许使用微秒或纳秒的时间或时间:

  • 模式:%d{ABSOLUTE_MICROS}输出的示例:14:34:02,123456
  • 模式:%d{ABSOLUTE_NANOS}输出的示例:14:34:02,123456789
  • 模式:%d{DEFAULT_MICROS}输出的示例:2012-11-02 14:34:02,123456
  • 模式:%d{DEFAULT_NANOS}输出的示例:2012-11-02 14:34:02,123456789

如果您需要更具体的模式,请使用" nano of-of-em>"图案字母n而不是" second "图案字母S例如%d{dd MMM yyyy HH:mm:ss,nnnn} to %d{dd MMM yyyy HH:mm:ss,nnnnnnnnn}将给出02 Nov 2012 14:34:02,1234 to 02 Nov 2012 14:34:02,123456789.

在您的情况下,由于您需要毫秒和微秒,您的模式可能是%d{dd/MM/yyyy HH:mm:ss,nnnnnn}

log4j 2.11增加了对时间戳的有限支持,比 在Java 9.

上跑步时毫秒

有关更多详细信息,请参阅 https://logging.apache.apache. org/log4j/2.x/Manual/layouts.html

其他推荐答案

它基于 a>不支持子毫秒格式,因此无法报告微秒.

本文地址:https://www.itbaoku.cn/post/1574803.html

问题描述

I want to add microseconds in the timestamp of each entry of my log files generated with log4j, is it possible ?

I have searched in the official documentation but there are no mention of unit below the milliseconds.

Now i have a conversion pattern like the following :

%d{dd/MM/yyyy HH\:mm\:ss,SSS} %-5p [%t] - %m%n

in the date conversion pattern(%d) i want to add microseconds after the milliseconds value(SSS), is there a way to do it ?

推荐答案

If you want to display micro-seconds, you need to add this yourself. This could be done with a custom Formatter (instead of PatternFormatter)

其他推荐答案

Starting from Java 9 and log4j 2.11.0, it is possible to get the timestamp even with nanoseconds.

Here are the special predefined patterns allowing to get a date time or a time with microseconds or nanoseconds:

  • Pattern: %d{ABSOLUTE_MICROS} Example of output: 14:34:02,123456
  • Pattern: %d{ABSOLUTE_NANOS} Example of output: 14:34:02,123456789
  • Pattern: %d{DEFAULT_MICROS} Example of output: 2012-11-02 14:34:02,123456
  • Pattern: %d{DEFAULT_NANOS} Example of output: 2012-11-02 14:34:02,123456789

If you need a more specific pattern use the "nano-of-second" pattern letter n instead of the "fraction-of-second" pattern letter S for example %d{dd MMM yyyy HH:mm:ss,nnnn} to %d{dd MMM yyyy HH:mm:ss,nnnnnnnnn} will give 02 Nov 2012 14:34:02,1234 to 02 Nov 2012 14:34:02,123456789.

In your case, as you need milliseconds and microseconds, your pattern could be something like %d{dd/MM/yyyy HH:mm:ss,nnnnnn}

Log4j 2.11 adds limited support for timestamps more precise than milliseconds when running on Java 9.

For more details please refer to https://logging.apache.org/log4j/2.x/manual/layouts.html

其他推荐答案

it is based on SimpleDateFormat and that does not support sub-millisecond formats, so microseconds can not be reported.