Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple loggers in Log4J

Posted on 2010-11-22
4
Medium Priority
?
801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
garypfirstech earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

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