I have a java project that has a log4j logging. It uses a rolling file appender and multiple loggers to log to a file. I want to add a DBappender and have a seperate logger that only writes to this appender, with none of the other loggers sending messages to it. I need, say one class to have two loggers, one writing to the fileAppender and one writing to the dbAppender. Is this possible, if so what is the configuration for it?
Thanks
It's possible to use two
Logger
s in one class.First idea: get the two loggers with different names:
Config for the package of the
dbLogger
:(Not tested.) In that case the
dbLogger
also logs to themainlog
appender. If it's not appropriate you could use a custom filter in themainlog
(and other) appenders which filters out the messages of thedbLogger
. Another solution is using a completely different prefix for thedbLogger
:Log4j config:
Note that if you pass the same parameter to the
getLogger()
method you will get sameLogger
object, so you have to use different names.