Reputation: 722
For my application, I have managed to configure log4j to generate multiple logs.
Both of the appenders output to the console and to a file.
But since the first log is my main log, I feel that this log should be the only log outputed to the console.
Would it be possible to disable the second log so that log4j does not use the console but still write to the file?
log4j.rootLogger=DEBUG, stdout
# stdout is set to be ConsoleAppender sending its output to System.out
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss SSS}ms %-5p [%t] - %m%n
log4j.appender.X=org.apache.log4j.FileAppender
log4j.appender.X.File=X.log
log4j.appender.X.Append=false
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss SSS}ms %-5p [%t] - %m%n
log4j.logger.X=DEBUG,X
the second appender 'Y' is configured the same way as 'X' .
I was also thinking of disabling altogether the console for both appenders and use: tail -f X.log inside a shell window to view the logs but it is not very practical when working inside Eclipse.
Any tips would be greatly appreciated.
Regards,
Upvotes: 2
Views: 5892
Reputation: 140061
Both of the appenders output to the console and to a file.
I think you are confused about the difference between a logger and an appender.
An appender only goes to one place - in your configuration you have declared a ConsoleAppender and a FileAppender. These are two separate entities. Neither of these appenders outputs to more than one location.
Loggers can be configured to send their output to zero to many appenders, and you have configured all of your loggers to send their output to the console by virtue of the rootLogger
.
If you would like to have only certain loggers send output to the console, then don't configure the root logger to use the console, but only the specific logger names to do so.
If you would like to have all loggers except X
send their output to the console, then you need to disable additivity for X
so that it does not inherit from the root logger.
Upvotes: 1
Reputation: 32969
What about not outputting the root logger to stdout and instead send your X logger to both X and stdout appenders? This way your Y logger would not output to stdout as well.
log4j.logger.X=DEBUG,X,stdout
Upvotes: 2
Reputation: 116306
The only way I know of is disabling additivity for certain loggers (categories), at the point where you direct them into one appender or the other. E.g.
log4j.logger.com.foo.bar=INFO, X
log4j.additivity.com.foo.bar=false
Upvotes: 2