Friday, 25 March 2016

JUL vs. Log4j


JUL (Java Util Logging) vs. Log4j

Similarities
  • Log4j and JUL are almost conceptually identical.
    But JUL renamed some concepts : For example appenders are "handlers," layouts are "formatters," and LoggingEvents are "LogRecords"
  • JUL uses log levels the same way that Log4J uses levels.
    Although JUL has 9 default levels instead of 7
  • JUL organizes loggers in a hierarchy and JUL loggers inherit properties from their parent loggers.
    Same way Log4j organizes its loggers.

Differences
  • JUL uses API java.util.logging
  • Log4j uses API org.apache.commons.logging
  • JUL has Unclear log levels :
    SEVERE - WARNING - INFO - CONFIG - FINE - FINER - FINEST 
  • Log4j has Clear log level using different names :
    FATAL - ERROR - WARN - INFO - DEBUG - TRACE
  • No ERROR level in JUL
  • There is an ERROR level in Log4j
  • To log an exception in JUL, it is unclear what Level to use and why do I need to pass e.getMessage() parameter, Like :
    logger.log(Level.SEVERE, e.getMessage(), e);
  • Simpler logging syntax in Log4j, Like
    logger.error(e);
  • In JUL, there is no way to put a configuration in the classpath to be automatically picked up by the logging framework.
    To configure either modify the logging.properties in your JRE/lib folder
    OR 

    Set JVM parameter -Djava.util.logging.config.file=/my/folder/logging.properties
  • In Log4j, there is a possibility to put an already configured configuration file.
  • Another method to create a logger is missing in JUL, Like : 
    Logger.getLogger(class)


    Method to create logger is :
    private static Logger logger =
              Logger.getLogger(MyCLass.class.getName());

  • Log4j has method to create a logger with syntax : 
    private Logger logger = Logger.getLogger(getClass());

No comments:

Post a Comment

Note: only a member of this blog may post a comment.