Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X
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");
Solved! Go to Solution.
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
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.
Hi @avillanueva , Can you please help me to configure these different log files for a single java code in log4jMethodserver.properties?
I entered for Pre logger only. But it was not working properly.
Please refer to the below sample code:
logger.LoggerExample.name=ext.test.LoggerExample
logger.LoggerExample.level=DEBUG
logger.LoggerExample.appenderRef.LoggerExample_Pre.ref=LoggerExample_Pre
# Pre-logger
appender.LoggerExample_Pre.type = RollingFile
appender.LoggerExample_Pre.name = LoggerExample_Pre
appender.LoggerExample_Pre.createOnDemand = true
appender.LoggerExample_Pre.fileName = ${sys:wt.logs.dir}/LoggerExampleUtility/LoggerExample_Pre-${sys:wt.manager.serviceName}-${sys:wt.jvm.startTime.formatted.short}-${sys:wt.jvm.id}-log4j.log
appender.LoggerExample_Pre.filePattern = ${sys:wt.logs.dir}/LoggerExampleUtility/LoggerExample_Pre-${sys:wt.manager.serviceName}-${sys:wt.jvm.startTime.formatted.short}-${sys:wt.jvm.id}-log4j.log.%d{yyyy-MM-dd}_%i
appender.LoggerExample_Pre.layout.type = PatternLayout
appender.LoggerExample_Pre.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS}{${timeZone}} [%-5p]-[%t]-%c[%M] - %m%ex%n
appender.LoggerExample_Pre.policies.type = Policies
appender.LoggerExample_Pre.policies.midnight.type = SizeBasedMidnightTriggeringPolicy
appender.LoggerExample_Pre.policies.midnight.size=9MB
appender.LoggerExample_Pre.strategy.type = DefaultRolloverStrategy
appender.LoggerExample_Pre.strategy.max =2147483647
But I am getting output logs only for LOGGER.debug, LOGGER.info all these.
I need to get output in LOGGER_PRE.debug, LOGGER_POST.debug too.
Can you please help me here?
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
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