• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

Multiple LOGGERS in 1 java class

I have a class that based on a parameter branches out to some piece of logic.  I need to have INFO level logging for any path of execution.  
I have defined a logger
private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);

I've also defined a certain amount of appenders relevant to each of the paths of executions which log to specific unique log files.
if i use LOGGER.info("blah"), it will log all the info to ALL the files.  

My question is, Lets say i have a function with a switch statement, how can i create many loggers so that i can say something like this:
LOGGERX1.info("x1 process"); // this will log info level to a file called x1.log
LOGGERX2.info("x2 process"); // this will log info level to a file called x2.log

from the same function so that even though all the loggers are INFO level, only certain logger receives the notification to log there rather then all have the same info...

Please help.
1 Solution
Just give each logger a unique name, e.g.

private static final Logger LOGGER1 = LoggerFactory.getLogger(MyClass.class + ".1");
private static final Logger LOGGER2 = LoggerFactory.getLogger(MyClass.class + ".2");

and log away.

You can set appenders differently for each, log to one at INFO and the other at DEBUG or log to both at INFO.  Whatever you like.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now