我们可以在日志消息中添加服务器名称吗?
Can we add server name in log message?
我正在使用
jboss at domain mode
并将其配置为为所有托管服务器创建合并日志文件
现在我想在我的日志消息中添加服务器名称,有没有我可以尝试使用简单 java 的方法。
I expect these results
11:44:01,937 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne 开始..
11:44:01,938 INFO [stdout] (schedulerFactoryBean_Worker-1) 服务器一上的作业 运行
11:44:01,937 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne 开始..
11:44:01,938 INFO [stdout] (schedulerFactoryBean_Worker-1) 服务器二
上的作业 运行
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout" >
<param name="ConversionPattern" value="[%c] (%x:%t) [User:%X{logguserid}] %m%n"/>
</layout>
</appender>
<appender name="AppLogWriter" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="D:/jboss-as-7.1.1.Final/domain/log/server.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="100"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%x:%t) [User:%X{logguserid}] %m%n"/>
</layout>
</appender>
<appender name="AuditLogWriter" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="D:/jboss-as-7.1.1.Final/domain/log/server.log"/><!--path of auditlog file-->
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%x:%t) [User:%X{logguserid}] %m%n"/>
</layout>
</appender>
<root>
<priority value="info"/>
<appender-ref ref="AppLogWriter"/>
<appender-ref ref="stdout"/>
</root>
<logger name=".audit.log" level="info" additivity="false">
<appender-ref ref="AuditLogWriter" />
</logger>
<appender name="CLASSLOADING" class="org.apache.log4j.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value= "D:/Logs/classloading.log"/>
<param name="Append" value="false"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %m%n"/>
</layout>
</appender>
<category name="org.jboss.classloading">
<priority value="TRACE"/>
<appender-ref ref="CLASSLOADING"/>
</category>
</log4j:configuration>
输出
11:44:01,427 INFO [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a info message
11:44:01,428 WARN [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a warn message
11:44:01,429 ERROR [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a error message
11:44:01,431 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne done..
11:44:01,728 INFO [org.springframework.batch.core.job.SimpleStepHandler] (schedulerFactoryBean_Worker-1) Duplicate step [stepOne] detected in execution of job=[demoJob]. If either step fails, both will be executed again on restart.
11:44:01,862 INFO [org.springframework.batch.core.job.SimpleStepHandler] (schedulerFactoryBean_Worker-1) Executing step: [stepOne]
11:44:01,937 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne start..
11:44:01,938 INFO [stdout] (schedulerFactoryBean_Worker-1) Job
11:44:01,938 INFO [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a info message
11:44:01,939 WARN [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a warn message
11:44:01,940 ERROR [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a error message
11:44:01,941 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne done..
11:44:02,151 INFO [org.springframework.batch.core.launch.support.SimpleJobLauncher] (schedulerFactoryBean_Worker-1) Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{JobID=1573539240754}] and the following status: [COMPLETED]
11:44:02,153 INFO [org.quartz.plugins.history.LoggingTriggerHistoryPlugin] (schedulerFactoryBean_Worker-1) Trigger [DEFAULT.jobTrigger] completed firing job [DEFAULT.demoJob] with resulting trigger instruction code: DO NOTHING. Next scheduled at: 12-11-2019 11:45:00.000
您可以将其放入 Log4j 映射诊断上下文:
MDC.put("jboss.server.name", System.getProperty("jboss.server.name"));
您可以将其添加到 Log4j 转换模式中:
"... [jboss-server-name=%X{jboss.server.name}] ..."
我正在使用
jboss at domain mode
并将其配置为为所有托管服务器创建合并日志文件
现在我想在我的日志消息中添加服务器名称,有没有我可以尝试使用简单 java 的方法。
I expect these results
11:44:01,937 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne 开始..
11:44:01,938 INFO [stdout] (schedulerFactoryBean_Worker-1) 服务器一上的作业 运行
11:44:01,937 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne 开始..
11:44:01,938 INFO [stdout] (schedulerFactoryBean_Worker-1) 服务器二
上的作业 运行<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="false" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout" > <param name="ConversionPattern" value="[%c] (%x:%t) [User:%X{logguserid}] %m%n"/> </layout> </appender> <appender name="AppLogWriter" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="D:/jboss-as-7.1.1.Final/domain/log/server.log"/> <param name="Append" value="true"/> <param name="MaxFileSize" value="10MB"/> <param name="MaxBackupIndex" value="100"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%x:%t) [User:%X{logguserid}] %m%n"/> </layout> </appender> <appender name="AuditLogWriter" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="D:/jboss-as-7.1.1.Final/domain/log/server.log"/><!--path of auditlog file--> <param name="Append" value="true"/> <param name="MaxFileSize" value="10MB"/> <param name="MaxBackupIndex" value="10"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%x:%t) [User:%X{logguserid}] %m%n"/> </layout> </appender> <root> <priority value="info"/> <appender-ref ref="AppLogWriter"/> <appender-ref ref="stdout"/> </root> <logger name=".audit.log" level="info" additivity="false"> <appender-ref ref="AuditLogWriter" /> </logger> <appender name="CLASSLOADING" class="org.apache.log4j.RollingFileAppender"> <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/> <param name="File" value= "D:/Logs/classloading.log"/> <param name="Append" value="false"/> <param name="MaxFileSize" value="5000KB"/> <param name="MaxBackupIndex" value="10"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %m%n"/> </layout> </appender> <category name="org.jboss.classloading"> <priority value="TRACE"/> <appender-ref ref="CLASSLOADING"/> </category> </log4j:configuration>
输出
11:44:01,427 INFO [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a info message
11:44:01,428 WARN [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a warn message
11:44:01,429 ERROR [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a error message
11:44:01,431 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne done..
11:44:01,728 INFO [org.springframework.batch.core.job.SimpleStepHandler] (schedulerFactoryBean_Worker-1) Duplicate step [stepOne] detected in execution of job=[demoJob]. If either step fails, both will be executed again on restart.
11:44:01,862 INFO [org.springframework.batch.core.job.SimpleStepHandler] (schedulerFactoryBean_Worker-1) Executing step: [stepOne]
11:44:01,937 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne start..
11:44:01,938 INFO [stdout] (schedulerFactoryBean_Worker-1) Job
11:44:01,938 INFO [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a info message
11:44:01,939 WARN [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a warn message
11:44:01,940 ERROR [com.Quartz_Oracle_11g.task.MyTaskOne] (schedulerFactoryBean_Worker-1) this is a error message
11:44:01,941 INFO [stdout] (schedulerFactoryBean_Worker-1) MyTaskOne done..
11:44:02,151 INFO [org.springframework.batch.core.launch.support.SimpleJobLauncher] (schedulerFactoryBean_Worker-1) Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{JobID=1573539240754}] and the following status: [COMPLETED]
11:44:02,153 INFO [org.quartz.plugins.history.LoggingTriggerHistoryPlugin] (schedulerFactoryBean_Worker-1) Trigger [DEFAULT.jobTrigger] completed firing job [DEFAULT.demoJob] with resulting trigger instruction code: DO NOTHING. Next scheduled at: 12-11-2019 11:45:00.000
您可以将其放入 Log4j 映射诊断上下文:
MDC.put("jboss.server.name", System.getProperty("jboss.server.name"));
您可以将其添加到 Log4j 转换模式中:
"... [jboss-server-name=%X{jboss.server.name}] ..."