Solved

Multiple loggers in Log4J

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 67
Java: The Public Class Main 4 44
learn programming 8 66
Tomcat 9 + java 8 error while trying to deploy a war file 2 675
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

680 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