Skip to main content
6-Contributor
November 17, 2023
Solved

Print different statements in different logger

  • November 17, 2023
  • 2 replies
  • 1995 views

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");

 

Best answer by HelesicPetr

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

2 replies

avillanueva
23-Emerald I
23-Emerald I
November 17, 2023

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.

6-Contributor
November 20, 2023

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.

HelesicPetr
22-Sapphire II
22-Sapphire II
November 20, 2023

Hi @AK_10341824 

Please describe what is not working.

PetrH

HelesicPetr
22-Sapphire II
22-Sapphire II
November 20, 2023

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