0

I have a Java application that uses the log4j logging framework. 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). I have set each File Appender to

...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. 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)

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. Any ideas?

Metalhead89
  • 1,740
  • 9
  • 30
  • 58

2 Answers2

0

What you want is a fresh new log file everytime you run your program modul.

Have you considered to use a RollingFileAppender? Afer the setup of your RollingFileAppender you use appender.rollOver(); 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. This delegates the management of files to the framework.

A different way would be to use a DailyRollingFileAppender. You can configure the time intervall for a file to roll over.

Simulant
  • 19,190
  • 8
  • 63
  • 98
  • Ok thanks thats a good idea. But what if I do not know how many modules are being started? So if I have 3 modules then each log file would be created 3 times so I would have to set the MaxBackupIndex to 3. But I use a variable number of modules in later times. – Metalhead89 Aug 23 '12 at 11:36
  • each modul uses it's own Appender. so you create 3 log files only once. If you restart just one modul the other Appenders did not get rolled over. – Simulant Aug 23 '12 at 11:45
  • But I use the settings with a xml configuration file because I do not want to create the appenders programatically – Metalhead89 Aug 23 '12 at 11:50
  • And another problem is, that I have to set the log files at the very first of my application (even before I start my main method) like this: `System.setProperty("main.log", pathToLogFiles + "overview.log"); System.setProperty("import.log", pathToLogFiles + "importToDB.log"); System.setProperty("runJdbcConn.log", pathToLogFiles + "runJDBCconnector.log"); System.setProperty("testJdbcConn.log", pathToLogFiles + "testJDBCconnector.log");` – Metalhead89 Aug 23 '12 at 11:52
0

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:

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:

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:

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

import.log (size >0)
parsing.log (size >0)
deletion.log (empty)
Metalhead89
  • 1,740
  • 9
  • 30
  • 58