简体   繁体   中英

How do I configure Hibernate to use standard logging

How do I configure Hibernate to use standard logging ?

Articles I read say to put the logging framework on your classpath, but I want to to just use the standard that comes with java so that doesnt make sense to be programtically.

Im trying to log SQL so in my code (before I use Hibernate) I have

Logger hibernateLogger = Logger.getLogger("org.hibernate.SQL");
hibernateLogger.setLevel(Level.ALL);

and attach my console handler that I use for my other debugging

            ConsoleHandler ch = new ConsoleHandler();
            ch.setFormatter(new com.jthink.songkong.logging.LogFormatter());
            ch.setLevel(Level.FINEST);
            MainWindow.logger.addHandler(ch);
            hibernateLogger.addHandler(ch);

but It has no effect, no Hibernate logging gets written to the console.

So I assume the problem is defaulting to log4 or something, but I dont see how to change this behaviour. I include Hibernate as part of my application with maven (pom.xml), and I want to programtically select standard logging if possible.

Loggers are subject to garbage collection . One bug in your code is the following: Logger hibernateLogger = Logger.getLogger("org.hibernate.SQL");

Remove that line and create a constant:

private static final Logger hibernateLogger = Logger.getLogger("org.hibernate.SQL");

Defining this in the config file will only work if the code at run-time demands the logger .

I found the solution at https://docs.jboss.org/hibernate/orm/4.3/topical/html/logging/Logging.html

I am using Hibernate 4.3.11 and the problem is that Hibernate automatically picks the logging framework based on what is loaded on the classpath, and only uses standard logging as last resort.

I use maven for my build and do not explicitly include any other logging framework, however that is not to say that one of the 3rd party libraries I use doesn't include it, which makes the logic of jboss framework fundamentally flawed.

Adding the folowing system property

-Dorg.jboss.logging.provider=jdk

solves the problem.

Personally I see little value in all these 3rd party logging frameworks, if there are flaws with standard logging then those flaws should be addressd by fixing standard logging, but it has always worked well for me.

Also with logging being crucial to performance it seems that the Java engineers have an advantage with standard logging over 3rd party logging that they can make adjustments due to better knowledge of the VM and the fact that it is a standard class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM