Quarkus - 日志配置

本指南说明日志(logging)及如何配置。

运行时配置

application.properties 文件中配置运行时日志记录, 例如将 Hibernate 之外的都设置为 INFO 级日志:

quarkus.log.level=INFO
quarkus.log.category."org.hibernate".level=DEBUG

所有配置属性参考 the logging configuration reference.

如果要通过命令行添加这些属性,请确保 " 已转义。 如 -Dquarkus.log.category.\"org.hibernate\".level=DEBUG

日志分类

记录是按类别进行的。每个类别均可独立配置。 适用于类别的配置也将适用于该类别的所有子类别,除非存在更具体的匹配子类别配置。 对于每个类别,将应用在 ( console / file / syslog ) 上配置的相同设置。 也可以通过将一个或多个 named handlers 附加到类别来覆盖它们。请参阅 Named handlers attached to a category 中的示例

属性名 默认值 描述

quarkus.log.category."<category-name>".level

INFO [1]

The level to use to configure the category named <category-name>. The quotes are necessary.

quarkus.log.category."<category-name>".useParentHandlers

true

Specify whether or not this logger should send its output to its parent logger.

quarkus.log.category."<category-name>".handlers=[<handler>]

empty [2]

The names of the handlers that you want to attach to a specific category.

属性名称中显示的引号是必需的,因为类别通常包含 '.' 必须转义。 如 配置成用文件记录 TRACE 级别日志 中例子。

Root logger 配置

root logger category 是单独处理的, 它通过下列属性配置:

属性名 默认值 描述

quarkus.log.level

INFO

每个日志类别的默认最小日志级别。

格式字符串

日志记录格式字符串支持以下符号:

符号 简介 描述

%%

%

表示一个 % 字符.

%c

Category

表示分类名称.

%C

Source class

Renders the source class name.[3]

%d{xxx}

Date

给定的的格式日期字符串,该字符串使用 java.text.SimpleDateFormat 定义的语法 .

%e

Exception

异常信息, 如果有的话.

%F

Source file

文件名.[3]

%h

Host name

系统主机名.

%H

Qualified host name

Renders the system’s fully qualified host name, which may be the same as the simple host name, depending on OS configuration.

%i

Process ID

Render the current process PID.

%l

Source location

Renders the source location information, which includes source file name, line number, class name, and method name.[3]

%L

Source line

Renders the source line number.[3]

%m

Full Message

Renders the log message plus exception (if any).

%M

Source method

Renders the source method name.[3]

%n

Newline

Renders the platform-specific line separator string.

%N

Process name

Render the name of the current process.

%p

Level

Render the log level of the message.

%r

Relative time

Render the time in milliseconds since the start of the application log.

%s

Simple message

Renders just the log message, with no exception trace.

%t

Thread name

Render the thread name.

%t{id}

Thread ID

Render the thread ID.

%z{<zone name>}

Time zone

Set the time zone of the output to <zone name>.

%X{<MDC property name>}

Mapped Diagnostics Context Value

Renders the value from Mapped Diagnostics Context

%X

Mapped Diagnostics Context Values

Renders all the values from Mapped Diagnostics Context in format {property.key=property.value}

%x

Nested Diagnostics context values

Renders all the values from Nested Diagnostics Context in format {value1.value2}

其它控制台日志记录格式

可以更改控制台日志的输出格​​式。 这对于一些需要收集 Quarkus 程序日志的服务非常有用,比如处理和存储日志信息以供后续分析。

JSON 格式日志

为了配置 JSON 格式日志,需要使用 quarkus-logging-json 扩展。 如下代码所示,将此扩展添加到您的应用程序 POM 中。

修改 POM 文件添加 JSON 日志扩展
  <dependencies>
    <!-- ... your other dependencies are here ... -->
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-logging-json</artifactId>
    </dependency>
  </dependencies>

默认情况下,引入此扩展会替换掉控制台的输出格式配置。 这意味着格式字符串和颜色设置(如果有)将被忽略。 其他控制台配置项(包括那些控制异步日志记录和日志级别)将继续生效。

对于某些人来说,在 dev 模式下使用人类可读(非结构化)的日志记录而在生产模式下使用JSON日志记录(结构化)是有意义的。 可以使用不同的配置文件来实现,如以下配置所示。

在 application.properties 中为 dev和 test 模式禁用 JSON 日志格式
%dev.quarkus.log.console.json=false
%test.quarkus.log.console.json=false
配置

JSON 日志扩展可以通过多种方式进行配置。支持以下属性:

Configuration property fixed at build time - All other configuration properties are overridable at runtime

Configuration property

Type

Default

Determine whether to enable the JSON console formatting extension, which disables "normal" console formatting.

boolean

true

Enable "pretty printing" of the JSON record. Note that some JSON parsers will fail to read pretty printed output.

boolean

false

The date format to use. The special string "default" indicates that the default format should be used.

string

default

The special end-of-record delimiter to be used. By default, no delimiter is used.

string

The zone ID to use. The special string "default" indicates that the default zone should be used.

string

default

The exception output type to specify.

detailed, formatted, detailed-and-formatted

detailed

Enable printing of more details in the log. Printing the details can be expensive as the values are retrieved from the caller. The details include the source class name, source file name, source method name and source line number.

boolean

false

启用 pretty printing 可能会导致某些处理器和 JSON 解析器失败。
由于从 caller 检索值,因此打印详细信息可能会很比较耗时。 详细信息包括源类名称,源文件名,源方法名称和源行号。

示例

控制台用 DEBUG 级别,无颜色,短时间,缩短类别前缀
quarkus.log.console.enable=true
quarkus.log.console.format=%d{HH:mm:ss} %-5p [%c{2.}] (%t) %s%e%n
quarkus.log.console.level=DEBUG
quarkus.log.console.color=false

quarkus.log.category."io.quarkus".level=DEBUG
如果要通过命令行添加这些属性,请确保 " 已转义。 如 -Dquarkus.log.category.\"io.quarkus\".level=DEBUG
配置成用文件记录 TRACE 级别日志
quarkus.log.file.enable=true
# 输出日志到 /tmp 目录下的 trace.log 文件
quarkus.log.file.path=/tmp/trace.log
quarkus.log.file.level=TRACE
quarkus.log.file.format=%d{HH:mm:ss} %-5p [%c{2.}] (%t) %s%e%n
# 设置2类日志为 (io.quarkus.smallrye.jwt, io.undertow.request.security) TRACE 级别
quarkus.log.category."io.quarkus.smallrye.jwt".level=TRACE
quarkus.log.category."io.undertow.request.security".level=TRACE
Named handlers attached to a category
# Send output to the console
quarkus.log.file.path=/tmp/trace.log
quarkus.log.console.format=%d{HH:mm:ss} %-5p [%c{2.}] (%t) %s%e%n
# Configure a named handler that logs to console
quarkus.log.handler.console."STRUCTURED_LOGGING".format=%e%n
# Configure a named handler that logs to file
quarkus.log.handler.file."STRUCTURED_LOGGING_FILE".enable=true
quarkus.log.handler.file."STRUCTURED_LOGGING_FILE".format=%e%n
# Configure the category and link the two named handlers to it
quarkus.log.category."io.quarkus.category".level=INFO
quarkus.log.category."io.quarkus.category".handlers=STRUCTURED_LOGGING,STRUCTURED_LOGGING_FILE

支持的日志 API

应用程序和组件可以使用以下任何API进行日志记录,并且日志将被合并:

集中式日志管理

如果要将日志发送到 Graylog,Logstash或Fluentd 等日志收集工具,可以参考 Centralized log management guide

如何配置 @QuarkusTest 日志

如果要为 @QuarkusTest 配置日志,不要忘记进行相应的设置 maven-surefire-plugin 。 特别是需要用 java.util.logging.manager 设置 LogManager

配置示例
<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>${surefire-plugin.version}</version>
      <configuration>
        <systemProperties>
          <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> (1)
          <quarkus.log.level>DEBUG</quarkus.log.level>  (2)
        </systemProperties>
      </configuration>
    </plugin>
  </plugins>
</build>
1 确保使用 org.jboss.logmanager.LogManager.
2 所有日志都用 debug 级别.

日志配置参考

Configuration property fixed at build time - All other configuration properties are overridable at runtime

Configuration property

Type

Default

The log level of the root category, which is used as the default log level for all categories.

Level

INFO

The default minimum log level

Level

INFO

Logging categories

Type

Default

The log level level for this category

InheritableLevel

inherit

The names of the handlers to link to this category.

list of string

Specify whether or not this logger should send its output to its parent Logger

boolean

true

Console handlers

Type

Default

If console logging should be enabled

boolean

true

The log format. Note that this value will be ignored if an extension is present that takes control of console formatting (e.g. an XML or JSON-format extension).

string

%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n

Level

ALL

If the console logging should be in color. If undefined quarkus takes best guess based on operating system and environment. Note that this value will be ignored if an extension is present that takes control of console formatting (e.g. an XML or JSON-format extension).

boolean

Specify how much the colors should be darkened. Note that this value will be ignored if an extension is present that takes control of console formatting (e.g. an XML or JSON-format extension).

int

0

Indicates whether to log asynchronously

boolean

false

The queue length to use before flushing writing

int

512

Determine whether to block the publisher (rather than drop the message) when the queue is full

block, discard

block

File handlers

Type

Default

If file logging should be enabled

boolean

false

string

%d{yyyy-MM-dd HH:mm:ss,SSS} %h %N[%i] %-5p [%c{3.}] (%t) %s%e%n

The level of logs to be written into the file.

Level

ALL

The name of the file in which logs will be written.

File

quarkus.log

Indicates whether to log asynchronously

boolean

false

The queue length to use before flushing writing

int

512

Determine whether to block the publisher (rather than drop the message) when the queue is full

block, discard

block

The maximum file size of the log file after which a rotation is executed.

MemorySize

int

1

File handler rotation file suffix. Example fileSuffix: .yyyy-MM-dd

string

Indicates whether to rotate log files on server initialization.

boolean

true

Syslog handlers

Type

Default

If syslog logging should be enabled

boolean

false

The IP address and port of the syslog server

host:port

localhost:514

The app name used when formatting the message in RFC5424 format

string

The name of the host the messages are being sent from

string

Sets the facility used when calculating the priority of the message as defined by RFC-5424 and RFC-3164

kernel, user-level, mail-system, system-daemons, security, syslogd, line-printer, network-news, uucp, clock-daemon, security2, ftp-daemon, ntp, log-audit, log-alert, clock-daemon2, local-use-0, local-use-1, local-use-2, local-use-3, local-use-4, local-use-5, local-use-6, local-use-7

user-level

Set the SyslogType syslog type this handler should use to format the message sent

rfc5424, rfc3164

rfc5424

Sets the protocol used to connect to the syslog server

tcp, udp, ssl-tcp

tcp

Set to true if the message being sent should be prefixed with the size of the message

boolean

false

Set to true if the message should be truncated

boolean

true

Enables or disables blocking when attempting to reconnect a org.jboss.logmanager.handlers.SyslogHandler.Protocol#TCP TCP or org.jboss.logmanager.handlers.SyslogHandler.Protocol#SSL_TCP SSL TCP protocol

boolean

false

string

%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n

The log level specifying, which message levels will be logged by syslog logger

Level

ALL

Indicates whether to log asynchronously

boolean

false

The queue length to use before flushing writing

int

512

Determine whether to block the publisher (rather than drop the message) when the queue is full

block, discard

block

Console logging

Type

Default

If console logging should be enabled

boolean

true

The log format. Note that this value will be ignored if an extension is present that takes control of console formatting (e.g. an XML or JSON-format extension).

string

%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n

The console log level.

Level

ALL

If the console logging should be in color. If undefined quarkus takes best guess based on operating system and environment. Note that this value will be ignored if an extension is present that takes control of console formatting (e.g. an XML or JSON-format extension).

boolean

Specify how much the colors should be darkened. Note that this value will be ignored if an extension is present that takes control of console formatting (e.g. an XML or JSON-format extension).

int

0

Indicates whether to log asynchronously

boolean

false

The queue length to use before flushing writing

int

512

Determine whether to block the publisher (rather than drop the message) when the queue is full

block, discard

block

File logging

Type

Default

If file logging should be enabled

boolean

false

The log format

string

%d{yyyy-MM-dd HH:mm:ss,SSS} %h %N[%i] %-5p [%c{3.}] (%t) %s%e%n

The level of logs to be written into the file.

Level

ALL

The name of the file in which logs will be written.

File

quarkus.log

Indicates whether to log asynchronously

boolean

false

The queue length to use before flushing writing

int

512

Determine whether to block the publisher (rather than drop the message) when the queue is full

block, discard

block

The maximum file size of the log file after which a rotation is executed.

MemorySize

The maximum number of backups to keep.

int

1

File handler rotation file suffix. Example fileSuffix: .yyyy-MM-dd

string

Indicates whether to rotate log files on server initialization.

boolean

true

Syslog logging

Type

Default

If syslog logging should be enabled

boolean

false

The IP address and port of the syslog server

host:port

localhost:514

The app name used when formatting the message in RFC5424 format

string

The name of the host the messages are being sent from

string

Sets the facility used when calculating the priority of the message as defined by RFC-5424 and RFC-3164

kernel, user-level, mail-system, system-daemons, security, syslogd, line-printer, network-news, uucp, clock-daemon, security2, ftp-daemon, ntp, log-audit, log-alert, clock-daemon2, local-use-0, local-use-1, local-use-2, local-use-3, local-use-4, local-use-5, local-use-6, local-use-7

user-level

Set the SyslogType syslog type this handler should use to format the message sent

rfc5424, rfc3164

rfc5424

Sets the protocol used to connect to the syslog server

tcp, udp, ssl-tcp

tcp

Set to true if the message being sent should be prefixed with the size of the message

boolean

false

Set to true if the message should be truncated

boolean

true

Enables or disables blocking when attempting to reconnect a org.jboss.logmanager.handlers.SyslogHandler.Protocol#TCP TCP or org.jboss.logmanager.handlers.SyslogHandler.Protocol#SSL_TCP SSL TCP protocol

boolean

false

The log message format

string

%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n

The log level specifying, which message levels will be logged by syslog logger

Level

ALL

Indicates whether to log asynchronously

boolean

false

The queue length to use before flushing writing

int

512

Determine whether to block the publisher (rather than drop the message) when the queue is full

block, discard

block

Log cleanup filters - internal use

Type

Default

The message starts to match

list of string

inherit

About the MemorySize format

A size configuration option recognises string in this format (shown as a regular expression): [0-9]+[KkMmGgTtPpEeZzYy]?. If no suffix is given, assume bytes.


1. Some extensions may define customized default log levels for certain categories, in order to reduce log noise by default. Setting the log level in configuration will override any extension-defined log levels.
2. By default the configured category gets the same handlers attached as the one on the root logger.
3. Format sequences which examine caller information may affect performance
quarkus.pro 是基于 quarkus.io 的非官方中文翻译站 ,最后更新 2020/04 。
沪ICP备19006215号-8
QQ交流群:1055930959
微信群: