[英]Access File from Log4J FileAppender programatically
I have a Java application that uses the log4j logging framework. 我有一个使用log4j记录框架的Java应用程序。 In my application I have several modules and each module creates its own log file.
在我的应用程序中,我有几个模块,每个模块创建自己的日志文件。 I start my application via command line, passing a parameter to tell my application which module it should be run.
我通过命令行启动我的应用程序,传递一个参数告诉我的应用程序应该运行哪个模块。 Lets say I have 3 modules so everytime one module gets started, all 3 log files are being created but just one is filled with information (the one from the module I just started).
可以说我有3个模块,因此每次启动一个模块时,都会创建所有3个日志文件,但是只有一个充满信息(一个是我刚启动的模块中的信息)。 I have set each File Appender to
我已将每个File Appender设置为
...append = false
so that everytime I start my application I have fresh log files without old log data. 这样,每当我启动我的应用程序时,我就会获得没有旧日志数据的新日志文件。
Now I have a batch file which creates a process chain, so all 3 modules are called right after the other within the batch file. 现在,我有了一个创建过程链的批处理文件,因此在批处理文件中,所有3个模块都在另一个模块之后立即调用。 But the problem is: At the end the log files from the first 2 modules are empty and only the last module stored its logs in the correct log file (of course this has to be like that because I set the append option to fasle for each file appender)
但是问题是:最后两个模块的日志文件为空,只有最后一个模块的日志存储在正确的日志文件中(当然,因为我将append选项设置为fasle,所以必须这样)文件附加器)
I am looking for the following solution: When I start a module I want to check if the log file for this module is empty. 我正在寻找以下解决方案:启动模块时,我想检查该模块的日志文件是否为空。 If yes then I just append the logging data to this file.
如果是,那么我只是将日志记录数据附加到此文件。 If there is already data in the file I want to delete the content to store the current logging data.
如果文件中已经有数据,我要删除内容以存储当前的日志记录数据。 So at the end when I call all 3 modules (starting the application 3 times with different parameters each time) I want to have all 3 log files filled.
因此,最后,当我调用所有3个模块时(每次使用不同的参数启动应用程序3次),我想填充所有3个日志文件。 Any ideas?
有任何想法吗?
What you want is a fresh new log file everytime you run your program modul. 每次运行程序模块时,您需要的是一个新的新日志文件。
Have you considered to use a RollingFileAppender ? 您是否考虑过使用RollingFileAppender ? Afer the setup of your
RollingFileAppender
you use appender.rollOver();
在设置
RollingFileAppender
请使用appender.rollOver();
RollingFileAppender
in the beginning of your program to create a new log file. 在程序的开头创建一个新的日志文件。 You can configure to keep a number of old log files with
appender.setMaxBackupIndex(int)
any older log files are getting deleted. 您可以使用
appender.setMaxBackupIndex(int)
配置为保留许多旧日志文件,所有旧日志文件都将被删除。 This delegates the management of files to the framework. 这将文件管理委托给框架。
A different way would be to use a DailyRollingFileAppender . 另一种方法是使用DailyRollingFileAppender 。 You can configure the time intervall for a file to roll over.
您可以配置文件滚动的时间间隔。
If I understand it correctly then the RollingFileAppender would create 3 log files for each module (assumed that I start 3 modules) So lets say I have the following modules with the appropriate log files in brackets: 如果我正确理解它,那么RollingFileAppender将为每个模块创建3个日志文件(假设我启动了3个模块)。因此,可以说我有以下模块,并在方括号中包含相应的日志文件:
Module 1: -import (import.log)
Module 2: -parsing (parsing.log)
Module 3: -deletion (deletion.log)
After starting modul 1 I would have the following files + their sizes: 启动模块1后,我将拥有以下文件及其大小:
import.log (size >0)
parsing.log (empty)
deletion.log (empty)
Currently after starting the second modul I would have: 当前启动第二个模块后,我将有:
import.log (empty)
parsing.log (size >0)
deletion.log (empty)
When I would use the RollingFileAppender I would have the following after starting the second module: 当我使用RollingFileAppender时,启动第二个模块后,我将得到以下内容:
import1.log (empty)
import.log (size >0)
parsing1.log (empty)
parsing.log (size >0)
deletion.log (empty)
But I want to have the following after starting 2 of the available 3 modules 但是我希望在启动3个可用模块中的2个之后具有以下功能
import.log (size >0)
parsing.log (size >0)
deletion.log (empty)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.