用于具有不同输出的线程的 log4j files/configs
log4j for threads with different output files/configs
我有一个应用程序可以执行一些工作并生成日志。
我用 org.apache.log4j.Logger
在我的 class 我写道:
static final Logger logger = Logger.getLogger(App.class);
在 main 中我从文件加载属性
PropertyConfigurator.configure("log4j.properties");
并写入输出
logger.debug("Hello World!");
一切正常(log4j.properties 的内容见下文)
现在我想重写一个将生成不同线程的应用程序(它们将被安排为作业并且可以并行执行)。
据我了解,每个线程都必须创建自己的记录器。像这样:
public class TestJob implements Job {
private Logger thread_logger = null;
//...
}
现在(在 "do thread" 方法中)我想为此记录器设置属性。
thread_logger = Logger.getLogger(TestJob.class);
//and assign the configuration, but how?
PropertyConfigurator.configure("mythreadname.log4j.properties"); //?
原始记录器有一个 log4j.properies 文件(这里声明了输出文件):
# Root logger option
log4j.rootLogger=DEBUG, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.append=false
log4j.appender.file.File=.\streambackup.execution.log
log4j.appender.file.MaxFileSize=100MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Duplicate log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
是否可能(以及如何做到)- link 每个线程一个 .属性 文件(具有不同的输出文件)?
IE。我想这样做
main thread writes log to log\logmain.txt
thread1 writes log to log\trh1\log1.txt
thread2 writes log to log\trh2\log2.txt
...
threadN writes log to log\trhN\logN.txt
//I don't now the count of treads...
PropertyConfigurator 似乎是所有线程的全局配置?
您不需要为每个线程创建一个配置文件,但您可以通过编程方式添加一个附加程序。例如:
public static class TestJob implements Job {
private Logger logger;
private RollingFileAppender appender;
private void init() {
logger = Logger.getLogger(TestJob.class);
appender = new RollingFileAppender();
appender.setLayout(new PatternLayout("%d %-5p %c{1}:%L - %m%n"));
appender.setFile("logs/trh1/log1.txt");
appender.setAppend(false);
appender.setMaxFileSize("100MB");
appender.setMaxBackupIndex(10);
appender.activateOptions();
logger.setAdditivity(false);
logger.addAppender(appender);
}
private void destroy() {
logger.removeAppender(appender);
}
public void execute(JobExecutionContext context) throws JobExecutionException {
init();
for (int i = 0; i < 10; i++) {
if (logger.isInfoEnabled()) {
logger.info("This is " + i);
}
}
destroy();
}
}
这是我的工作(我希望)由 Paul Vargas 进行的代码更新
起初我重写了 init 函数调用。我根据工作身份命名:
public void execute(JobExecutionContext jExeCtx) throws JobExecutionException {
JobKey jobKey = jExeCtx.getJobDetail().getKey();
String OutputForLog = jExeCtx.getJobDetail().getJobDataMap().getString(PR_LOGFILNAME); //<--Here I pass the name of the log file from main thread to scheduller job
init("Thread" + jobKey.toString().replaceAll("\.", ""), OutputForLog); //<-- Here I pass the name for appender and the path for log file name
初始化函数现在看起来像:
private Logger logger = null;
private RollingFileAppender appender = null;
private void init(String NameForAppender, String Name4LogFile) {
logger = Logger.getLogger(NameForAppender); //NOT DEFAULT BY "logger = Logger.getLogger(TestJob.class);"
appender = new RollingFileAppender();
appender.setName(NameForAppender); //<-- I think this helps in pair of the Logger.getLogger(NameForAppender) above
appender.setLayout(new PatternLayout("%d{yyyy-MM-dd/HH:mm:ss.SSS/zzz} %-5p %c{1}:%L - %m%n"));
appender.setFile(Name4LogFile);
appender.setAppend(true);
appender.setImmediateFlush(true);
appender.setMaxFileSize("100MB");
appender.setMaxBackupIndex(10);
appender.activateOptions();
logger.setAdditivity(false); //<--do not use default root logger
logger.addAppender(appender);
}
至少使用这段代码我无法在文件中生成日志合并。
(我同时基于相同的 class 启动了几个作业(线程))
我有一个应用程序可以执行一些工作并生成日志。 我用 org.apache.log4j.Logger 在我的 class 我写道:
static final Logger logger = Logger.getLogger(App.class);
在 main 中我从文件加载属性
PropertyConfigurator.configure("log4j.properties");
并写入输出
logger.debug("Hello World!");
一切正常(log4j.properties 的内容见下文)
现在我想重写一个将生成不同线程的应用程序(它们将被安排为作业并且可以并行执行)。 据我了解,每个线程都必须创建自己的记录器。像这样:
public class TestJob implements Job {
private Logger thread_logger = null;
//...
}
现在(在 "do thread" 方法中)我想为此记录器设置属性。
thread_logger = Logger.getLogger(TestJob.class);
//and assign the configuration, but how?
PropertyConfigurator.configure("mythreadname.log4j.properties"); //?
原始记录器有一个 log4j.properies 文件(这里声明了输出文件):
# Root logger option
log4j.rootLogger=DEBUG, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.append=false
log4j.appender.file.File=.\streambackup.execution.log
log4j.appender.file.MaxFileSize=100MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Duplicate log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
是否可能(以及如何做到)- link 每个线程一个 .属性 文件(具有不同的输出文件)? IE。我想这样做
main thread writes log to log\logmain.txt
thread1 writes log to log\trh1\log1.txt
thread2 writes log to log\trh2\log2.txt
...
threadN writes log to log\trhN\logN.txt
//I don't now the count of treads...
PropertyConfigurator 似乎是所有线程的全局配置?
您不需要为每个线程创建一个配置文件,但您可以通过编程方式添加一个附加程序。例如:
public static class TestJob implements Job {
private Logger logger;
private RollingFileAppender appender;
private void init() {
logger = Logger.getLogger(TestJob.class);
appender = new RollingFileAppender();
appender.setLayout(new PatternLayout("%d %-5p %c{1}:%L - %m%n"));
appender.setFile("logs/trh1/log1.txt");
appender.setAppend(false);
appender.setMaxFileSize("100MB");
appender.setMaxBackupIndex(10);
appender.activateOptions();
logger.setAdditivity(false);
logger.addAppender(appender);
}
private void destroy() {
logger.removeAppender(appender);
}
public void execute(JobExecutionContext context) throws JobExecutionException {
init();
for (int i = 0; i < 10; i++) {
if (logger.isInfoEnabled()) {
logger.info("This is " + i);
}
}
destroy();
}
}
这是我的工作(我希望)由 Paul Vargas 进行的代码更新 起初我重写了 init 函数调用。我根据工作身份命名:
public void execute(JobExecutionContext jExeCtx) throws JobExecutionException {
JobKey jobKey = jExeCtx.getJobDetail().getKey();
String OutputForLog = jExeCtx.getJobDetail().getJobDataMap().getString(PR_LOGFILNAME); //<--Here I pass the name of the log file from main thread to scheduller job
init("Thread" + jobKey.toString().replaceAll("\.", ""), OutputForLog); //<-- Here I pass the name for appender and the path for log file name
初始化函数现在看起来像:
private Logger logger = null;
private RollingFileAppender appender = null;
private void init(String NameForAppender, String Name4LogFile) {
logger = Logger.getLogger(NameForAppender); //NOT DEFAULT BY "logger = Logger.getLogger(TestJob.class);"
appender = new RollingFileAppender();
appender.setName(NameForAppender); //<-- I think this helps in pair of the Logger.getLogger(NameForAppender) above
appender.setLayout(new PatternLayout("%d{yyyy-MM-dd/HH:mm:ss.SSS/zzz} %-5p %c{1}:%L - %m%n"));
appender.setFile(Name4LogFile);
appender.setAppend(true);
appender.setImmediateFlush(true);
appender.setMaxFileSize("100MB");
appender.setMaxBackupIndex(10);
appender.activateOptions();
logger.setAdditivity(false); //<--do not use default root logger
logger.addAppender(appender);
}
至少使用这段代码我无法在文件中生成日志合并。 (我同时基于相同的 class 启动了几个作业(线程))