What is the alternative for LOGGER.entering() and LOGGER.exiting() from Java logger to log4j2 using slf4j #2083
-
I am migrating code base from simple java logger to log4j2 using sl4j because of having issue while writing asynchronously to a single log file in multiple threads. I noticed entry and exit methods are not available in sl4j interface. what is the best to achieve entry and exit to print class name, method name and parameters which can be object. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
The equivalents of JUL's Basically these allow you to replace: private static final java.util.logging.Logger logger = ...
public String computeFoo(String param1, int param2) {
logger.entering("org.example.MyClass", "computeFoo", new Object[] {param1, param2});
// do something
//
logger.exiting("org.example.MyClass", "computeFoo", result);
return result; with: private static final org.apache.logging.log4j.Logger logger = ...
public String computeFoo(String param1, int param2) {
logger.traceEntry(null, new Object[] {param1, param2});
// or just
logger.traceEntry(null, param1, param2);
// do something
//
return logger.traceExit(result); As you can see the location information is removed, because most modern logging backends can compute it on the fly. E.g. with Log4j Core (the reference implementation of Log4j API) you can use: <PatternLayout pattern="...%C#%M %m%n"/> Normally runtime location information is expensive, but you can pre-compute it as compiletime using the Log4j Transform Plugin. Remark: This solution requires using Log4j API instead of SLF4J. The default SLF4J |
Beta Was this translation helpful? Give feedback.
-
Check my OpenRewrite PR for a basic JUL to Log4j API conversion: openrewrite/rewrite-logging-frameworks#133 |
Beta Was this translation helpful? Give feedback.
The equivalents of JUL's
Logger#entering(String, String, Object[])
andLogger#exiting(String, String, Object)
are:Logger#traceEntry
,Logger#traceExit
.Basically these allow you to replace:
with: