API union slf4j

I want to modify slf4j with Logback in an outdated application. Well, the legacy application has its own logging structure. So all I had to do was change the logging structure for logging into slf4j instead of log4j.

He worked like a dream. I was pleased until I noticed that the journal was logged for each journal event:

Logger.java:... 

Hop! This would not help my fellow developers when trying to figure out where the magazine event came from.

How can I say that Logback looks like several levels on the stack for the actual location to register?

The logger class is a utility class with the following methods:

 public static void debug(String clazz, String message) { org.slf4j.Logger logger = LoggerFactory.getLogger(clazz); logger.debug(message); } 
+6
java slf4j legacy logback
source share
2 answers

Found a solution looking at jcl-over-slf4j . Most slf4j implementations (including logback) use loggers that implement LocationAwareLogger , which has a log method that expects the fully qualified class name of the transfer log class to be one of its arguments:

 private static final String FQCN = Logger.class.getName(); public static void debug(String clazz, String message) { org.slf4j.Logger logger = LoggerFactory.getLogger(clazz); if (logger instanceof LocationAwareLogger) { ((LocationAwareLogger) logger).log(null, FQCN, LocationAwareLogger.DEBUG_INT, message, null, null); } else { logger.debug(message); } } 
+11
source share

See various XXX-over-slf4j implementations for how to do this.

Basically, you want to completely replace the existing log structure. Do not end slf4j.

Edit:

Another approach might be to write your own subclass of the layout that you are using now, which has a revised value for the% m,% l etc fields, which skips the extra stack frame.

+5
source share

All Articles