Solved

Multiple loggers in Log4J

Posted on 2010-11-22
4
759 Views
Last Modified: 2012-05-10
Hi,

I want specific messages generated from within the same class to be logged separately. So, how can I create 2 different types of loggers within the same class. Currently, the Properties file looks like

log4j.rootCategory=DEBUG, O
 
# Stdout
log4j.appender.O=org.apache.log4j.ConsoleAppender
log4j.appender.O.layout=org.apache.log4j.PatternLayout
log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
 
# File
log4j.appender.MESSAGE=org.apache.log4j.RollingFileAppender
log4j.appender.MESSAGE.File=${catalina.home}/logs/log.log

# File
log4j.appender.MESSAGE=org.apache.log4j.RollingFileAppender
log4j.appender.MESSAGE.File=target/logs/messages.log
# Control the maximum log file size
log4j.appender.MESSAGE.MaxFileSize=1000KB
# Archive log files (one backup file here)
log4j.appender.MESSAGE.MaxBackupIndex=100
log4j.appender.MESSAGE.layout=org.apache.log4j.PatternLayout
log4j.appender.MESSAGE.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M           (%        F:%    L) - %m%n
log4j.appender.MESSAGE.
 
log4j.category.failedMessagesLog=INFO, MESSAGE

I'm using the logging in my code as: –
/** Logger. */
  Logger logger = Logger.getLogger(MyClass.class);
  Logger msgLogger = Logger.getLogger("MESSAGE");

**********************************************************************************************************
Upon testing, I get an empty log file (messages.log) created.
Any suggestions??

Thanks
0
Comment
Question by:GlobalDictator
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
garypfirstech earned 500 total points
ID: 34189838
You need to create two Loggers, one for each type of failed message.  You already have

  Logger failedMessageLogger = Logger.getLogger(myclass.class);

Add another one like

  Logger otherMessageLogger = Logger.getLogger("OtherMessageLogger");

The first logger is specified in your properties file by
 
  log4j.logger.fqcn.myclass=<LEVEL>, appenders

fqdn stands for the fully qualified package name for my class.  You should use log4j.logger instead of log4j.category which is deprecated.

The second logger is specified in your properties file by

  log4j.logger.OtherMessageLogger = <LEVEL>, appenders

The appenders for each logger should be separate and point to separate files.
0
 

Author Comment

by:GlobalDictator
ID: 34190106
@Gary, you said:

log4j.logger.OtherMessageLogger = <LEVEL>, appenders

Can we add multiple levels to the same appenders, such as :
log4j.logger.OtherMessageLogger = DEBUG, INFO, appenders

Is the above syntax correct representation?
0
 
LVL 7

Expert Comment

by:garypfirstech
ID: 34190185
No.  The first parameter is the level and the remaining parameters are the names of appenders so with your syntax, it will look for an appender named INFO which is not what you want.

When you specify DEBUG, the Logger will log everything that is at the DEBUG level or coarser-grained.  So the level of the OtherMessageLogger includes DEBUG, INFO, WARN, ERROR, and FATAL already.
0
 

Author Closing Comment

by:GlobalDictator
ID: 34195473
Excellent, precise and prompt response
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question