如何使用 vertx-default-jul-logging.properties 文件来控制文件的格式和数量?

How do I use of the vertx-default-jul-logging.properties file to control the format and number of files?

我在使用这个 Vertx 包时遇到了困难。 所有日志记录都将使用 Vertx (io.vertx.core.logging.Logger)

完成

我今天可以使用 vertx-default-jul-logging.properties 文件登出到一个文件。 我一直坚持的是这三件事。 (是的,我一直在网上阅读很多文档。我只需要看到一个可靠的工作属性文件,其中写出了内容) 1. 如何指定多个输出文件,以便一些转到 mmddyy class.method.log 和第二个文件到 mmddyy audit.log

  1. 如何控制内容的输出,使日志的格式为 {"yy-MM-dd"} [%-5level] %class{15}.%M :%L - %msg%n

  2. 您可以向我指出的任何可靠资源都有一些关于在这个 vertx-default-jul-logging.properties 文件中编写正确内容的教程,因为我花了我一天的时间这个,不要认为它应该花这么长时间才能得到。

感谢您的帮助。

您应该从阅读官方文档中的小章节 http://vertx.io/docs/vertx-core/java/#_logging 开始。重要的是要看到它解释说,即使 JUL 是默认设置,您也可以根据需要使用任何其他日志记录框架:

  • log4j
  • slf4j
  • log4j2

vert.x 记录器 class 给你的只是一个抽象,无论下面是什么都隐藏在你的代码中,如果你想改变它,你不需要改变你的代码。

如果您仍想使用 JUL,可以在配置文件中配置日志格式:

# here you specify who's going to handle the logs
# for this example I'll use only the console
handlers=java.util.logging.ConsoleHandler
# now you specify the format
java.util.logging.SimpleFormatter.format=%5$s %6$s\n
# and now you tell the console to use that format
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

这些是基本的配置,为了控制格式,这里指定语法http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

首先,如果你还想使用JUL,请放弃这个答案;否则,请继续阅读。

我正在使用 Vert.x 3.3.3 并重定向所有日志 through/to Log4J 2.x — 类似的方法适用于 Logback/SLF4J.

您首先需要 add/include 这些依赖项(我正在使用 Gradle;如果可用请更新版本):

compile 'com.lmax:disruptor:3.3.5'
compile 'org.apache.logging.log4j:log4j-1.2-api:2.7'
compile 'org.apache.logging.log4j:log4j-jcl:2.7'
compile 'org.apache.logging.log4j:log4j-jul:2.7'
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.7'

然后创建一个名为${projectRoot}/src/main/resources/log4j2.xml的文件,并在其中添加以下内容"content":

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Properties>
    <Property name="log-pattern">%d{MM-dd-yyyy HH:mm:ss.SSS} |- %highlight{%5p}{TRACE=blue, DEBUG=green, INFO=green, WARN=yellow, ERROR=red, FATAL=red} in %style{%C{1}:%L}{cyan} [%style{%t}{magenta}] - %m%n</Property>
  </Properties>

  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="${log-pattern}" />
    </Console>
  </Appenders>

  <!-- Logger levels: trace, debug, info, warn, error, fatal -->
  <Loggers>
    <AsyncLogger name="your.package.name" level="DEBUG" additivity="false" includeLocation="true">
      <AppenderRef ref="Console" />
    </AsyncLogger>

    <Root level="WARN">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

...最后,在您的 class(es) 中定义一个 LOGGER,以便实际 "do the logging".

...
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class Application extends AbstractVerticle {
  final Logger LOGGER = LogManager.getLogger();

  ...
}

那会成功 — 但使用 Log4J 2.x。请记住,Vert.x 本质上(或某种程度上)是异步的,并且此配置也使用异步记录器,因此需要(很棒的 IMO)LMAX Disruptor.

另请注意,您需要更改应用程序的包名称(请参阅 AsyncLogger 定义;我使用了 your.package.name),最终 log-pattern 用于您要查找的格式。