Skip to main content
Nov 17, 2023 2:13 PM UTC
Solved

Print different statements in different logger

Initially posted in this topic

 

Hi @HelesicPetr , I want to print different statements in different logger. Can you please help me here?
For example, I want different loggers purpose wise. Please refer to the below section.

private static final Logger LOGGER = LogManager.getLogger(LoggerExample.class );

private static final Logger LOGGER_PRE = LogManager.getLogger("LoggerExample_Pre");

private static final Logger LOGGER_SUMMARY = LogManager.getLogger("LoggerExample_Summary");

private static final Logger LOGGER_POST = LogManager.getLogger("LoggerExample_Post");

private static final Logger LOGGER_ERROR = LogManager.getLogger("LoggerExample_Error");

 

Nov 20, 2023 11:04 AM UTC

Hi @AK_10341824 

But in your config there is not definition for the Logger> LoggerExample_Pre 

 

There is logger ext.test.LoggerExample

 

the LoggerExample_Pre definition is just appender that appends the ext.test.LoggerExample logger.

 

you need to define another logger with that name.

 

logger.LoggerExample_Pre.name=ext.test.LoggerExample_Pre 
logger.LoggerExample_Pre.level=DEBUG 

 

and so on...

 

PetrH

7 replies

Nov 17, 2023 8:34 PM UTC

Here is what I do in my code:

Imports look like this for 

import org.apache.logging.log4j.Logger; //updated for Log4J 2.x
import wt.log4j.LogR;

Note my update for Log4J 2.x as @HelesicPetr mentioned. In the code block I call out my logger which is typically the class name.

private static Logger log=null;

 static
 {
 try
 {
 log = LogR.getLogger(MyCustomClass.class.getName());
 }
 catch(Exception e)
 {
 }
 }

 In your example, I would just repeat that call for each one. Seems excessive but your call.

Nov 20, 2023 7:45 AM UTC

Hi @avillanueva , Can you please help me to configure these different log files for a single java code in log4jMethodserver.properties?

AK_10341824_0-1700466273876.png

I entered for Pre logger only. But it was not working properly.

Nov 20, 2023 7:55 AM UTC

Hi @AK_10341824 

Please describe what is not working.

PetrH

Nov 20, 2023 7:44 AM UTC

Hi @AK_10341824 

The original post talk about configuration in the log4jMethodServer.properties

If you want to use your own logger in your code, use the LogR.getLogger

With this method, you can use any logger name. Not just class name. 

for example I use logger for all classes in specific functionality.  

LogR.getLogger("cz.aveng.WVSPublish");

the WVSPublish is not existing class

LogR.getLogger("cz.aveng.WVSPublish");

your logger could be 

private static final Logger LOGGER_ERROR = LogR.getLogger("LoggerExample_Error");

 

PetrH