简体   繁体   English

如何使用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?

I'm struggling with this Vertx package that I am told to use. 我正在努力使用该Vertx软件包。 All logging is to be done with Vertx (io.vertx.core.logging.Logger) 所有记录都将通过Vertx(io.vertx.core.logging.Logger)完成

I am able to get logging out to a file today with the use of the vertx-default-jul-logging.properties file. 今天,我可以使用vertx-default-jul-logging.properties文件注销到文件。 What I have been stuck on are these 3 things. 我一直坚持的是这三件事。 (And yes i've been reading lots of documents online. I just need to see a solid working properties file with the stuff written out) 1. How to have multiple output files specified so some go to mmddyy class.method.log and a second file to mmddyy audit.log (是的,我一直在线阅读大量文档。我只需要看到一个可靠的工作属性文件,上面写出的内容就可以了。)1.如何指定多个输出文件,因此一些文件转到mmddyy class.method.log和mmddyy audit.log的第二个文件

  1. How do I control the output of the content so the logs have the format of {"yy-MM-dd"} [%-5level] %class{15}.%M:%L - %msg%n 如何控制内容的输出,使日志的格式为{“ yy-MM-dd”} [%-5level]%class {15}。%M:%L-%msg%n

  2. Any solid resource that you can point me to that has some tutorial on writing the proper stuff in this vertx-default-jul-logging.properties file as I've spent my day on this and don't think it should take this long to get. 您可以向我指向的任何可靠资源都提供了一些有关在此vertx-default-jul-logging.properties文件中编写适当内容的教程,因为我花了很多时间在此上并且不认为应该花这么长时间得到。

Appreciate your help. 感谢您的帮助。

You should start by reading the small chapter http://vertx.io/docs/vertx-core/java/#_logging from the official documentation. 您应该先阅读官方文档中的一小章http://vertx.io/docs/vertx-core/java/#_logging It is important to see that it explains that even though JUL is the default you can use any other logging framework if you so desire: 重要的是要看到它可以说明,即使JUL是默认设置,如果您愿意,也可以使用任何其他日志记录框架:

  • log4j log4j
  • slf4j slf4j
  • log4j2 log4j2

What the vert.x logger class gives you is just an abstraction, that no matter what is beneath is hidden from your code and if you desire to change it you don't need to change your code. vert.x logger类提供的只是一个抽象,无论代码中隐藏了什么内容,如果希望对其进行更改,则无需更改代码。

If you still want to use JUL the formatting of your logs are configurable on the config: 如果您仍想使用JUL,则可以在config上配置日志格式:

# 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

These are the basic of the configuration, in order to control the format the syntax is specified here http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax 这些是配置的基础,为了控制格式,在此处指定了语法http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

First of all, if you still want to use JUL discard this answer; 首先,如果您仍然想使用JUL,请放弃此答案; otherwise, keep reading. 否则,请继续阅读。

I'm using Vert.x 3.3.3 and redirecting all the logs through/to Log4J 2.x — a similar approach applies for Logback/SLF4J. 我正在使用Vert.x 3.3.3 ,并将所有日志重定向通过/重定向到Log4J 2.x-类似的方法适用于Logback / SLF4J。

You first need to add/include these dependencies (I'm using Gradle; update the versions if available): 您首先需要添加/包括这些依赖项(我正在使用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'

Then make a file named ${projectRoot}/src/main/resources/log4j2.xml , and add the following "content" into it: 然后制作一个名为${projectRoot}/src/main/resources/log4j2.xml的文件,并在其中添加以下“内容”:

<?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>

...finally, define a LOGGER in your class(es) in order to actually "do the logging". ...最后,在您的课程中定义一个LOGGER ,以便实际“进行日志记录”。

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

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

  ...
}

That will do the trick — but using Log4J 2.x. 可以解决问题-但使用Log4J2.x。 Keep in mind, Vert.x is asynchronous by nature (or sort of) and this configuration also uses asynchronous logger(s), hence the need for the (awesome IMO) LMAX Disruptor . 请记住,Vert.x本质上是异步的(或某种程度上),并且此配置还使用异步记录器,因此需要(很棒的IMO) LMAX Disruptor

Also notice that you need to change the package name(s) for your application (see the AsyncLogger definition; I used your.package.name ) and eventually, the log-pattern for the format you are looking for. 还要注意,您需要更改应用程序的软件包名称(请参阅AsyncLogger定义;我使用了your.package.name ),最后your.package.name了所要查找格式的log-pattern

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

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