1

I want to use Log4J for logging my java projects. I created a log4j.properties file in the src directory with the following content:

# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.DEFAULT_LOGGER=INFO,file2

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n



# Direct 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{ABSOLUTE} %5p %c{1}:%L - %m%n


log4j.appender.file2=org.apache.log4j.FileAppender
log4j.appender.file2.File=file2.log
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

For example I want to use the "DEFAULT_LOGGER" only in my main method. So I wrote:

static Logger log = Logger.getLogger("DEFAULT_LOGGER");
log.fatal("Process Logger");

But when I execute the main method I prints the message "Process Logger" to all Appenders (stdout, file and file2) But I only want to print it out to file2. How can I do that or better ro say what do I do wrong?

The second point is, when I execute the main method the second time, it does not overwrite file and file2, it justs adds a line inside the textfile. How can I avoid that?

Metalhead89
  • 1,740
  • 9
  • 30
  • 58

2 Answers2

3

Log4j has something called additivity. by default it is set to true and it means that every log you write will not only be logged by the specific logger but also by its ancestors (the root logger in this case).

To set it to false try this:

log4j.additivity.DEFAULT_LOGGER = false

Learn more about it here.

Tomer
  • 17,787
  • 15
  • 78
  • 137
  • Thank you. It now works that the output is stored only in file2 and NOT in the stdout. But there are still 2 things left. It still creates file and file2 when I start the main method. File is empty and the output is only written to file2 but I dont want file to be created at all. How does this works? And there is still the problem that when I execute the method 2 times, that log4j does not overwrite file2, it just adds the output to the file. – Metalhead89 Aug 07 '12 at 09:11
  • Log4j scans the properties files at the beginning and creates all the needed files that are attached to the logger definitions. So if you don't want the file to be created, remove the appender from the logger: `log4j.rootLogger=INFO, file, stdout`, make it: `log4j.rootLogger=INFO, stdout ` – Tomer Aug 07 '12 at 09:13
  • why would you want the file to be overwritten? that basically beats the purpose of logging... – Tomer Aug 07 '12 at 09:16
  • Because when I restart my application I want to have a fresh logging file without old entries (from the last day maybe) – Metalhead89 Aug 07 '12 at 09:24
  • Of course I could do delete it with java but I dont want to. With the standard Java logging API it is possible to just say if I want to add the output or if I want to overwrite it – Metalhead89 Aug 07 '12 at 09:27
  • No, don't delete it with java, if you want to restart the server, just stop it, go the log directory **DELETE THE PHYSICAL** log file and start the server. – Tomer Aug 07 '12 at 09:28
  • Yes of course I could do that. But my application is a testing framework so the goal is that when I start my application I want to have a fresh environment and I only want to read the current output of logs. And the applciation is client based so the user would have to delete it everytime – Metalhead89 Aug 07 '12 at 09:32
  • Try using RolingFileAppender: http://stackoverflow.com/questions/5538278/how-to-keep-single-file-and-overwrite-the-contents-in-the-same-file-using-log4cx – Tomer Aug 07 '12 at 09:35
  • Sorry but this also did not work. I will create a new question. – Metalhead89 Aug 07 '12 at 09:43
1

try:

log4j.additivity.DEFAULT_LOGGER = false

Wojciech Górski
  • 945
  • 11
  • 29