-
DescriptionThe encapsulated LogUtils utility class has an error printing line numbers, and the output log is the line number in LogUtils. ConfigurationVersion: [Log4j version] Operating system: [OS and version] JDK: [JDK distribution and version] Logs
Reproduction[An isolated test reproducing the test. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
Hi @scwlkq, Log4j does not provide any class named Can you add an excerpt of the class code? |
Beta Was this translation helpful? Give feedback.
-
Edit: in the original answer I missed the fact that you are using SLF4J instead of the Log4j API. Both SLF4J and Log4j API create log messages lazily only if the log level is enabled (cf. e.g. Log4j API source code), so you can safely replace: public static void trace(Logger logger, Marker marker, String msg, Object arg) {
if (isTraceEnabled(logger, marker)) {
logger.trace(marker, msg, arg);
}
} in your code with: public static void trace(Logger logger, Marker marker, String msg, Object arg) {
logger.trace(marker, msg, arg);
} Now you have two choices:
Inlining the utility classIn order to inline all call sites of @InlineMe(replacement = "logger.trace(marker, msg, arg)")
public static void trace(Logger logger, Marker marker, String msg, Object arg) {
...
} and use Error Prone's patching feature (cf. documentation). Fix location informationIf you decided against inlining and you use the Log4j API, you need to use the methods of the private static final String FQCN = LogUtils.class.getName();
public static void trace(Logger logger, Marker marker, String msg, Object arg) {
((ExtendedLogger) logger).logIfEnabled(FQCN, Level.TRACE, marker, msg, arg);
} Edit: the SLF4J version of the above is: private static final String FQCN = LogUtils.class.getName():
public static void trace(Logger logger, Marker marker, String msg, Object arg) {
final LoggingEventBuilder builder = logger.atTrace();
if (builder instanceof CallerBoundaryAware) {
((CallerBoundaryAware) builder).setCallerBoundary(FQCN);
}
builder.addMarker(marker).log(msg, arg);
} |
Beta Was this translation helpful? Give feedback.
-
There seems to be an error in your suggestion. It should be |
Beta Was this translation helpful? Give feedback.
@scwlkq,
Edit: in the original answer I missed the fact that you are using SLF4J instead of the Log4j API.
I adapted some content to also work with SLF4J.
Both SLF4J and Log4j API create log messages lazily only if the log level is enabled (cf. e.g. Log4j API source code), so you can safely replace:
in your code with:
Now you have two choices: