Log4J2: 如何将完全相同的输出同时记录到文件和控制台?

huangapple go评论64阅读模式
英文:

Log4J2: How to log the exact same output to both file and console?

问题

以下是您要翻译的内容:

以下这些行来自我的log4j2属性文件的开头:

rootLogger.level = INFO
rootLogger.appenderRef.stdout.ref = LOG_ROOT
rootLogger.appenderRef.startup.ref = START_UP_LOG
rootLogger.appenderRef.handler.ref = HANDLER_LOG

appender.LOG_ROOT.type = RollingFile
appender.LOG_ROOT.name = LOG_ROOT
appender.LOG_ROOT.fileName = logs/${hostName}_app.log
appender.LOG_ROOT.filePattern = logs/${hostName}_am.log.%d{yyyy-MM-dd}_%i.gz
appender.LOG_ROOT.layout.type = PatternLayout
appender.LOG_ROOT.layout.charset = UTF-8
appender.LOG_ROOT.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p - (%F:%L) %m%n
appender.LOG_ROOT.policies.type = Policies
appender.LOG_ROOT.policies.time.type = TimeBasedTriggeringPolicy
appender.LOG_ROOT.policies.time.interval = 1
appender.LOG_ROOT.policies.time.modulate = true
appender.LOG_ROOT.policies.size.type = SizeBasedTriggeringPolicy
appender.LOG_ROOT.policies.size.size=5120MB
appender.LOG_ROOT.strategy.type = DefaultRolloverStrategy
appender.LOG_ROOT.strategy.fileIndex = nomax
appender.LOG_ROOT.strategy.delete.type = Delete
appender.LOG_ROOT.strategy.delete.basePath = logs
appender.LOG_ROOT.strategy.delete.ifFileName.type = IfFileName
appender.LOG_ROOT.strategy.delete.ifFileName.glob = *am.log.*
appender.LOG_ROOT.strategy.delete.ifAccumulatedFileCount.type = IfAccumulatedFileCount
appender.LOG_ROOT.strategy.delete.ifAccumulatedFileCount.exceeds = 180

在属性文件中,我还有其他使用LOG_ROOT appender的日志记录器,例如:

logger.ORG_HIBERNATE_SQL.name = org.hibernate.SQL
logger.ORG_HIBERNATE_SQL.level = INFO
logger.ORG_HIBERNATE_SQL.additivity = false
logger.ORG_HIBERNATE_SQL.appenderRef.rolling.ref = LOG_ROOT

除了现有的日志文件之外,我希望将完全相同的LOG_ROOT内容显示在控制台上。是否有任何方法可以“克隆”日志输出到控制台?

英文:

The following lines are from the start of my log4j2 properties file:

rootLogger.level = INFO
rootLogger.appenderRef.stdout.ref = LOG_ROOT
rootLogger.appenderRef.startup.ref = START_UP_LOG
rootLogger.appenderRef.handler.ref = HANDLER_LOG
appender.LOG_ROOT.type = RollingFile
appender.LOG_ROOT.name = LOG_ROOT
appender.LOG_ROOT.fileName = logs/${hostName}_app.log
appender.LOG_ROOT.filePattern = logs/${hostName}_am.log.%d{yyyy-MM-dd}_%i.gz
appender.LOG_ROOT.layout.type = PatternLayout
appender.LOG_ROOT.layout.charset = UTF-8
appender.LOG_ROOT.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p - (%F:%L) %m%n
appender.LOG_ROOT.policies.type = Policies
appender.LOG_ROOT.policies.time.type = TimeBasedTriggeringPolicy
appender.LOG_ROOT.policies.time.interval = 1
appender.LOG_ROOT.policies.time.modulate = true
appender.LOG_ROOT.policies.size.type = SizeBasedTriggeringPolicy
appender.LOG_ROOT.policies.size.size=5120MB
appender.LOG_ROOT.strategy.type = DefaultRolloverStrategy
appender.LOG_ROOT.strategy.fileIndex = nomax
appender.LOG_ROOT.strategy.delete.type = Delete
appender.LOG_ROOT.strategy.delete.basePath = logs
appender.LOG_ROOT.strategy.delete.ifFileName.type = IfFileName
appender.LOG_ROOT.strategy.delete.ifFileName.glob = *am.log.*
appender.LOG_ROOT.strategy.delete.ifAccumulatedFileCount.type = IfAccumulatedFileCount
appender.LOG_ROOT.strategy.delete.ifAccumulatedFileCount.exceeds = 180

In the properties file, I also have other loggers that uses the LOG_ROOT appender. For instance,

logger.ORG_HIBERNATE_SQL.name = org.hibernate.SQL
logger.ORG_HIBERNATE_SQL.level = INFO
logger.ORG_HIBERNATE_SQL.additivity = false
logger.ORG_HIBERNATE_SQL.appenderRef.rolling.ref = LOG_ROOT

In addition to the existing log file, I want to display the exact same LOG_ROOT contents to the console as well. Is there any way I can "clone" the log output to the console?

答案1

得分: 1

如果您使用相同的布局,您将获得相同的输出:

rootLogger.appenderRef.console.ref = CONSOLE
appender.CONSOLE.type = Console
appender.CONSOLE.name = CONSOLE
appender.CONSOLE.layout.type = PatternLayout
appender.CONSOLE.layout.charset = UTF-8
appender.CONSOLE.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p - (%F:%L) %m%n

如果您希望确保所有附加程序中的模式相同,请定义一个用户属性:

property.PATTERN = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p - (%F:%L) %m%n

然后在需要的地方使用${PATTERN}

编辑: 默认情况下,记录器还会记录到其父记录器的附加程序(additivity="true")。如果禁用了附加性,您将不得不为每个直接使用LOG_ROOT的记录器添加对CONSOLE附加程序的引用。

在您的情况下,可能更容易将附加性保留在其默认值true,仅在根记录器上保留LOG_ROOTCONSOLE附加程序,并将START_UP_LOGHANDLER_LOG附加程序移到更具体的记录器。

英文:

I you use the same layout, you'll get the same output:

rootLogger.appenderRef.console.ref = CONSOLE
appender.CONSOLE.type = Console
appender.CONSOLE.name = CONSOLE
appender.CONSOLE.layout.type = PatternLayout
appender.CONSOLE.layout.charset = UTF-8
appender.CONSOLE.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p - (%F:%L) %m%n

If you wish to ensure that the patterns are the same in all appenders, define a user property:

property.PATTERN = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p - (%F:%L) %m%n

and use ${PATTERN} wherever it is required.

Edit: Loggers by default also log to their parent's appenders (additivity="true"). If you disable additivity you'll have to add a reference to the CONSOLE appender to each logger that directly uses LOG_ROOT.

In your case it is probably easier to leave additivity at its default true value, keep just the LOG_ROOT and CONSOLE appenders on the root logger and move the START_UP_LOG and HANDLER_LOG appender to more specific logger.

huangapple
  • 本文由 发表于 2023年7月3日 12:35:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定