Solved

Multiple loggers in Log4J

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

624 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