Java kernel environment that does not require a LOGGER declaration for each class

I tried the following magazines

  • Java Registration API
  • Log4j
  • SLF4J

All this requires a class-level LOGGER declaration, for example below

private final static java.util.logging.Logger.Logger LOGGER = java.util.logging.Logger.Logger.getLogger(MyClass.class.getName()); private final Logger slf4jLogger = LoggerFactory.getLogger(SLF4JHello.class); private final static Logger log4jLogger = Logger.getLogger(Log4jHello.class); 

It looks disgusting to me, is there a logger infrastructure in java that does not require this declaration?

I'm looking for, I can have a global ad like

 private final static Logger Logger = Logger.getLogger(MyApp.class); 

But when I call Logger.log (..) from the class XXX.class, then Logger should use the name XXX.class.

+7
java logging slf4j log4j java.util.logging
source share
2 answers

Your problem is more than likely not with the registration framework, but with the layout.

Specific example

so35592962/App.java

 package so35592962; import org.apache.logging.log4j.*; import so35592962.sub.OtherClass; public class App { public static final Logger logger = LogManager.getLogger(); public static void main(String[] args) { logger.error("in App.main"); OtherClass.act(); } } 

so35592962/sub/OtherClass.java

 package so35592962.sub; import static so35592962.App.logger; public class OtherClass { public static void act() { logger.error("OtherClass.act"); } } 

So you can see that this is absolutely what you want: classes that use the same registrar. You can use Log4J2 for this.

Now I am adding the magic log4j2.xml file

 <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %C{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> 

The launch of this will be printed:

 12:05:28.834 [main] ERROR so35592962.App - in App.main 12:05:28.836 [main] ERROR so35592962.sub.OtherClass - OtherClass.act 

Look, there are different class names here! However, I used Log4J2.

What happened here?

Pay attention to the pattern used in the PatternLayout tag:

%d{HH:mm:ss.SSS} [%t] %-5level %C{36} - %msg%n

Standard examples and what you usually see on the Internet use the %L pattern. This template should display the name of the registrar. But you said you didn’t want this. Fortunately, other templates exist. %C will display the class name instead of the log name. This is the template used here.

According to the PatternLayout documentation , the %C pattern does the following:

Displays the fully qualified class name of the caller issuing the logging request.

Important note also mentioned in the documentation:

Creating a caller class name (location information) is an expensive operation and can affect performance. Use with caution.

+2
source share

You can change both of them in class annotations using Lombok.

+1
source share

All Articles