Solved

Multiple loggers in Log4J

Posted on 2010-11-22
4
749 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now