第 20 章 Log
Apache Karaf 提供动态、强大的日志记录系统。
它支持:
- OSGi 日志服务
- Apache Log4j v1 和 v2 框架
- Apache Commons Logging 框架
- Logback 框架
- SLF4J 框架
- 原生 Java Util Logging 框架
这意味着应用可以使用任何日志记录框架,Apache Karaf 将使用中央日志系统来管理日志记录器、附加程序等。
20.1. 配置文件
初始日志配置从 etc/org.ops4j.pax.logging.cfg
加载。
此文件是 标准 Log4j2 配置文件。
找到不同的 Log4j2 元素:
- loggers
- appenders
- 布局
您可以在 文件中直接添加自己的初始配置。
默认配置如下:
# # Copyright 2005-2018 Red Hat, Inc. # # Red Hat licenses this file to you under the Apache License, version # 2.0 (the "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # implied. See the License for the specific language governing # permissions and limitations under the License. # # # Internal Log4j2 configuration # log4j2.status = WARN log4j2.verbose = false log4j2.dest = out # # Common pattern layouts for appenders defined as reusable properties # See https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout # references will be replaced by felix.fileinstall # log4j2.pattern = %d{DEFAULT} | %-5.5p | %-20.20t | %-32.32c{1.} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n #log4j2.pattern = %d{DEFAULT} %-5.5p {%t} [%C.%M()] (%F:%L) : %m%n # # Appenders configuration # # JDBC Appender log4j2.appender.jdbc.type = JDBC log4j2.appender.jdbc.name = JdbcAppender log4j2.appender.jdbc.tableName = EVENTS log4j2.appender.jdbc.cs.type = DataSource log4j2.appender.jdbc.cs.lazy = true log4j2.appender.jdbc.cs.jndiName = osgi:service/jdbc/logdb log4j2.appender.jdbc.c1.type = Column log4j2.appender.jdbc.c1.name = DATE log4j2.appender.jdbc.c1.isEventTimestamp = true log4j2.appender.jdbc.c2.type = Column log4j2.appender.jdbc.c2.name = LEVEL log4j2.appender.jdbc.c2.pattern = %level log4j2.appender.jdbc.c2.isUnicode = false log4j2.appender.jdbc.c3.type = Column log4j2.appender.jdbc.c3.name = SOURCE log4j2.appender.jdbc.c3.pattern = %logger log4j2.appender.jdbc.c3.isUnicode = false log4j2.appender.jdbc.c4.type = Column log4j2.appender.jdbc.c4.name = THREAD_ID log4j2.appender.jdbc.c4.pattern = %thread log4j2.appender.jdbc.c4.isUnicode = false log4j2.appender.jdbc.c5.type = Column log4j2.appender.jdbc.c5.name = MESSAGE log4j2.appender.jdbc.c5.pattern = %message log4j2.appender.jdbc.c5.isUnicode = false # Console appender not used by default (see log4j2.rootLogger.appenderRefs) log4j2.appender.console.type = Console log4j2.appender.console.name = Console log4j2.appender.console.layout.type = PatternLayout log4j2.appender.console.layout.pattern = ${log4j2.pattern} # Rolling file appender log4j2.appender.rolling.type = RollingRandomAccessFile log4j2.appender.rolling.name = RollingFile log4j2.appender.rolling.fileName = ${karaf.data}/log/fuse.log log4j2.appender.rolling.filePattern = ${karaf.data}/log/fuse-%i.log.gz # uncomment to not force a disk flush #log4j2.appender.rolling.immediateFlush = false log4j2.appender.rolling.append = true log4j2.appender.rolling.layout.type = PatternLayout log4j2.appender.rolling.layout.pattern = ${log4j2.pattern} log4j2.appender.rolling.policies.type = Policies log4j2.appender.rolling.policies.size.type = SizeBasedTriggeringPolicy log4j2.appender.rolling.policies.size.size = 16MB log4j2.appender.rolling.strategy.type = DefaultRolloverStrategy log4j2.appender.rolling.strategy.max = 20 # Audit file appender log4j2.appender.audit.type = RollingRandomAccessFile log4j2.appender.audit.name = AuditRollingFile log4j2.appender.audit.fileName = ${karaf.data}/security/audit.log log4j2.appender.audit.filePattern = ${karaf.data}/security/audit.log.%i log4j2.appender.audit.append = true log4j2.appender.audit.layout.type = PatternLayout log4j2.appender.audit.layout.pattern = ${log4j2.pattern} log4j2.appender.audit.policies.type = Policies log4j2.appender.audit.policies.size.type = SizeBasedTriggeringPolicy log4j2.appender.audit.policies.size.size = 8MB # OSGi appender log4j2.appender.osgi.type = PaxOsgi log4j2.appender.osgi.name = PaxOsgi log4j2.appender.osgi.filter = * # # Loggers configuration # # Root logger log4j2.rootLogger.level = INFO log4j2.rootLogger.appenderRef.RollingFile.ref = RollingFile log4j2.rootLogger.appenderRef.PaxOsgi.ref = PaxOsgi log4j2.rootLogger.appenderRef.Console.ref = Console log4j2.rootLogger.appenderRef.Console.filter.threshold.type = ThresholdFilter log4j2.rootLogger.appenderRef.Console.filter.threshold.level = ${karaf.log.console:-OFF} #log4j2.rootLogger.appenderRef.Sift.ref = Routing # Spifly logger log4j2.logger.spifly.name = org.apache.aries.spifly log4j2.logger.spifly.level = WARN # Security audit logger log4j2.logger.audit.name = org.apache.karaf.jaas.modules.audit log4j2.logger.audit.level = INFO log4j2.logger.audit.additivity = false log4j2.logger.audit.appenderRef.AuditRollingFile.ref = AuditRollingFile # help with identification of maven-related problems with pax-url-aether #log4j2.logger.aether.name = shaded.org.eclipse.aether #log4j2.logger.aether.level = TRACE #log4j2.logger.http-headers.name = shaded.org.apache.http.headers #log4j2.logger.http-headers.level = DEBUG #log4j2.logger.maven.name = org.ops4j.pax.url.mvn #log4j2.logger.maven.level = TRACE
默认配置使用 out
文件 appender 定义 ROOT
日志记录器,其 INFO
日志级别。您可以将日志级别更改为任何 Log4j2 有效值。从最详细到最详细的信息,您可以指定 TRACE, DEBUG, INFO, ERROR, 或 FATAL。
- OSGi appender
-
osgi:*
附加程序是一个特殊的附加程序,用于将日志消息发送到 OSGi 日志服务。 - stdout appender
-
stdout
控制台附加程序是预先配置的,但没有默认启用。此附加程序允许您在标准输出中直接显示日志消息。如果您计划在服务器模式下运行 Apache Karaf (不带控制台),这是值得注意的。
要启用它,您必须在 rootLogger
中添加 stdout
附加程序:
log4j2.rootLogger=INFO, out, stdout, osgi:*
- out appender
-
out
appender 是默认值。它是维护并轮转 10 1MB 日志文件的滚动文件。日志文件默认位于data/log/fuse.log
中。 - sift appender
-
默认情况下,
sift
appender 不会被启用。此附加器允许您为每个部署的捆绑包有一个日志文件。默认情况下,日志文件名称格式使用捆绑包符号名称(在data/log
文件夹中)。您可以在运行时编辑此文件。Apache Karaf 重新加载文件,并且更改生效。您不需要重新启动 Apache Karaf。Apache Karaf 使用另一个配置文件,即etc/org.apache.karaf.log.cfg
。此文件配置由日志命令使用的日志服务(请参阅后文)。 - JDBC appender
-
jdbc
appender 有一个lazy
标志,当true
(启用)时,如果数据源不可用,则不会将日志记录添加到数据库中。但是,当 jndi 时,数据源或连接会返回,日志会重启。
log4j2.appender.jdbc.cs.lazy = true
重要
如果要避免丢失日志记录消息,我们还会重新配置紧急附加程序。