0

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.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • This should work. Have you tried it with a logging.properties? – Simon Martinelli Aug 23 '19 at 14:37
  • No because I wanted to avoid properties file because some of my logging logic depends on logic that cannot be specified in properties file (i.e such as set log level based on user options). But ive found Im meant to be calling my properties class at startup with the -D-Djava.util.logging.config.class option, but if this called before anything it currently, do I really have to call it this way ? – Paul Taylor Aug 23 '19 at 15:47
  • @SimonMartinelli but i now use -Djava.util.logging.config.class so that code is run at startup, I think the issue is that hibernate is using sl4j or something instead. – Paul Taylor Aug 24 '19 at 06:28
  • Found the solution, adding -Dorg.jboss.logging.provider=jdk fixed the problem – Paul Taylor Aug 24 '19 at 06:35

2 Answers2

0

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.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • Ive done this, also my code is no called at start up with the -Djava.util.logging.config.class property. This solved issue with c3p0 but not hibernate, I think I need to tell hibernate Im using standard logging rather than log4j bu I dont know how ? – Paul Taylor Aug 23 '19 at 19:46
0

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.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • Personally I don't like the log levels FINE, FINER, FINEST? INFO, DEBUG, TRACE are much better. On the other I hand log4j was there years before java.util.logging (JUL) and most people were using it. I always suggest to use a logging facade like SLF4J to keep your code independent of the underlying framework. I'm also not sure if JUL supports async logging to not break performance with logging. – Simon Martinelli Aug 24 '19 at 08:47
  • I dont see that it matters what the log levels are called. Using a facade means you can't actually make full use of the logging system you are using, and as my questions shows the multitude of logging frameworks available just make it harder to get logging working when using 3rd party libraries. I have had to add system properties for all my startup scripts for both hibernate and c3p0 – Paul Taylor Aug 24 '19 at 09:08
  • That's probably because you are not using a framework like Spring Boot or Java EE. There the configuration is already done for you. – Simon Martinelli Aug 24 '19 at 13:40
  • Nor would I use them anymore, very tempted to drop Hibernate actually. There is really no advantage to using these alternative loggers and logger facades, just causes a whole heap of problems. – Paul Taylor Aug 24 '19 at 13:50