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 中的示例
属性名 | 默认值 | 描述 |
---|---|---|
|
|
The level to use to configure the category named |
|
|
Specify whether or not this logger should send its output to its parent logger. |
|
|
The names of the handlers that you want to attach to a specific category. |
属性名称中显示的引号是必需的,因为类别通常包含 '.' 必须转义。 如 配置成用文件记录 TRACE 级别日志 中例子。 |
格式字符串
日志记录格式字符串支持以下符号:
符号 | 简介 | 描述 |
---|---|---|
|
|
表示一个 |
|
Category |
表示分类名称. |
|
Source class |
Renders the source class name.[3] |
|
Date |
给定的的格式日期字符串,该字符串使用 |
|
Exception |
异常信息, 如果有的话. |
|
Source file |
文件名.[3] |
|
Host name |
系统主机名. |
|
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. |
|
Process ID |
Render the current process PID. |
|
Source location |
Renders the source location information, which includes source file name, line number, class name, and method name.[3] |
|
Source line |
Renders the source line number.[3] |
|
Full Message |
Renders the log message plus exception (if any). |
|
Source method |
Renders the source method name.[3] |
|
Newline |
Renders the platform-specific line separator string. |
|
Process name |
Render the name of the current process. |
|
Level |
Render the log level of the message. |
|
Relative time |
Render the time in milliseconds since the start of the application log. |
|
Simple message |
Renders just the log message, with no exception trace. |
|
Thread name |
Render the thread name. |
|
Thread ID |
Render the thread ID. |
|
Time zone |
Set the time zone of the output to |
|
Mapped Diagnostics Context Value |
Renders the value from Mapped Diagnostics Context |
|
Mapped Diagnostics Context Values |
Renders all the values from Mapped Diagnostics Context in format {property.key=property.value} |
|
Nested Diagnostics context values |
Renders all the values from Nested Diagnostics Context in format {value1.value2} |
其它控制台日志记录格式
可以更改控制台日志的输出格式。 这对于一些需要收集 Quarkus 程序日志的服务非常有用,比如处理和存储日志信息以供后续分析。
JSON 格式日志
为了配置 JSON 格式日志,需要使用 quarkus-logging-json
扩展。
如下代码所示,将此扩展添加到您的应用程序 POM 中。
<dependencies>
<!-- ... your other dependencies are here ... -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-logging-json</artifactId>
</dependency>
</dependencies>
默认情况下,引入此扩展会替换掉控制台的输出格式配置。 这意味着格式字符串和颜色设置(如果有)将被忽略。 其他控制台配置项(包括那些控制异步日志记录和日志级别)将继续生效。
对于某些人来说,在 dev 模式下使用人类可读(非结构化)的日志记录而在生产模式下使用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
Type |
Default |
|
---|---|---|
Determine whether to enable the JSON console formatting extension, which disables "normal" console formatting. |
boolean |
|
Enable "pretty printing" of the JSON record. Note that some JSON parsers will fail to read pretty printed output. |
boolean |
|
The date format to use. The special string "default" indicates that the default format should be used. |
string |
|
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 |
|
The exception output type to specify. |
|
|
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 |
|
启用 pretty printing 可能会导致某些处理器和 JSON 解析器失败。 |
由于从 caller 检索值,因此打印详细信息可能会很比较耗时。 详细信息包括源类名称,源文件名,源方法名称和源行号。 |
示例
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
|
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
# 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
集中式日志管理
如果要将日志发送到 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
Type |
Default |
|
---|---|---|
The log level of the root category, which is used as the default log level for all categories. |
|
|
The default minimum log level |
|
|
Type |
Default |
|
The log level level for this category |
InheritableLevel |
|
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 |
|
Type |
Default |
|
If console logging should be enabled |
boolean |
|
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 |
|
The console log level. |
|
|
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 |
|
Indicates whether to log asynchronously |
boolean |
|
The queue length to use before flushing writing |
int |
|
Determine whether to block the publisher (rather than drop the message) when the queue is full |
|
|
Type |
Default |
|
If file logging should be enabled |
boolean |
|
The log format |
string |
|
The level of logs to be written into the file. |
|
|
The name of the file in which logs will be written. |
|
|
Indicates whether to log asynchronously |
boolean |
|
The queue length to use before flushing writing |
int |
|
Determine whether to block the publisher (rather than drop the message) when the queue is full |
|
|
The maximum file size of the log file after which a rotation is executed. |
||
The maximum number of backups to keep. |
int |
|
File handler rotation file suffix. Example fileSuffix: .yyyy-MM-dd |
string |
|
Indicates whether to rotate log files on server initialization. |
boolean |
|
Type |
Default |
|
If syslog logging should be enabled |
boolean |
|
The IP address and port of the syslog server |
host:port |
|
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 |
|
|
Set the |
|
|
Sets the protocol used to connect to the syslog server |
|
|
Set to |
boolean |
|
Set to |
boolean |
|
Enables or disables blocking when attempting to reconnect a |
boolean |
|
The log message format |
string |
|
The log level specifying, which message levels will be logged by syslog logger |
|
|
Indicates whether to log asynchronously |
boolean |
|
The queue length to use before flushing writing |
int |
|
Determine whether to block the publisher (rather than drop the message) when the queue is full |
|
|
Type |
Default |
|
If console logging should be enabled |
boolean |
|
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 |
|
The console log level. |
|
|
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 |
|
Indicates whether to log asynchronously |
boolean |
|
The queue length to use before flushing writing |
int |
|
Determine whether to block the publisher (rather than drop the message) when the queue is full |
|
|
Type |
Default |
|
If file logging should be enabled |
boolean |
|
The log format |
string |
|
The level of logs to be written into the file. |
|
|
The name of the file in which logs will be written. |
|
|
Indicates whether to log asynchronously |
boolean |
|
The queue length to use before flushing writing |
int |
|
Determine whether to block the publisher (rather than drop the message) when the queue is full |
|
|
The maximum file size of the log file after which a rotation is executed. |
||
The maximum number of backups to keep. |
int |
|
File handler rotation file suffix. Example fileSuffix: .yyyy-MM-dd |
string |
|
Indicates whether to rotate log files on server initialization. |
boolean |
|
Type |
Default |
|
If syslog logging should be enabled |
boolean |
|
The IP address and port of the syslog server |
host:port |
|
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 |
|
|
Set the |
|
|
Sets the protocol used to connect to the syslog server |
|
|
Set to |
boolean |
|
Set to |
boolean |
|
Enables or disables blocking when attempting to reconnect a |
boolean |
|
The log message format |
string |
|
The log level specifying, which message levels will be logged by syslog logger |
|
|
Indicates whether to log asynchronously |
boolean |
|
The queue length to use before flushing writing |
int |
|
Determine whether to block the publisher (rather than drop the message) when the queue is full |
|
|
Type |
Default |
|
The message starts to match |
list of string |
|
About the MemorySize format
A size configuration option recognises string in this format (shown as a regular expression): |