Can we create two instances of log4j

Can we create two instances of log4j in the first class for writing to a file and the other in jdbcadapter?

// one to write log in console
Logger log = Logger.getLogger(this.getClass()); 

// write in database within one class 
Logger log1 = Logger.getLogger(this.getClass()); 

      

My jdbc adapter My file adapter I went to the Logger instance log for snapshot Logmom Logm for Logger for jdbc.

+3


source to share


3 answers


The log4j

available variety of Appenders

descriptive names, such as FileAppender, ConsoleAppender, SocketAppender, SyslogAppender, NTEventLogAppender and even SMTPAppender

. Several recorders can be connected to any recorder, so it is possible to register the same information on several outputs; for example, to a file locally and to a socket listener on another computer.



Also, if you want to write logs to different outputs according to level, you can try Log to appender by level . "

+3


source


This only works if you give the second registrar a different name. Otherwise, both variables will contain the same instance. For example:

Logger log = Logger.getLogger(this.getClass());
Logger log1 = Logger.getLogger(this.getClass().getName() + ".db");

      



Then you get two split loggers.

+1


source


I'm not sure if this instantiates (or returns the same cached one), but even if it does, it will have the same appender configuration (since the same class name is used for this category).

You can get two different registrars with different category names:

Logger log = Logger.getLogger(this.getClass()); 

Logger log1 = Logger.getLogger(this.getClass().getName()+".extra");

      

Here they are logged in log4j.properties.

In addition, you can have one log log for multiple applications, depending on filters such as priority. So if you don't have complex logic in your code that needs to be recorded anywhere, you can just use one logger and you can still go to file and database.

+1


source







All Articles