Class LoggerUtils


  • public class LoggerUtils
    extends Object
    Author:
    moskowitz
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  LoggerUtils.LoggingOutputStream
      An OutputStream that writes contents to a Logger upon each call to flush() Original URL: https://blogs.oracle.com/nickstephen/entry/java_redirecting_system_out_and Author gives permission for free use in blog comments section.
      static class  LoggerUtils.StdOutErrLevel
      Class defining 2 new Logging levels, one for STDOUT, one for STDERR, used when multiplexing STDOUT and STDERR into the same rolling log file via the Java Logging APIs.

      From: http://blogs.sun.com/nickstephen/entry/java_redirecting_system_out_and
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Level FORCE
      Log statements with this level are always loggable -- even for loggers set to OFF.
    • Constructor Summary

      Constructors 
      Constructor Description
      LoggerUtils()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void addTerseFileLogger​(Level level, String filename)
      In cases where a logging.properties file is too cumbersome, sets terse formatter for file handler.
      void dumpAncestry​(Logger logger)
      Prints own and parents' log levels to standard out (for debugging).
      static Level getLevelRecursive​(Logger logger)
      Traverses parents to find effective log level.
      static Logger getLogger​(Class<?> clazz)
      Convenience wrapper around Logger.getLogger(String).
      static boolean initLogging​(URL... urls)
      This method tries to configure logging by loading a configuration from each of the specified URLs, in order, until an attempt succeeds.
      static void log​(Logger logger, Level level, String format, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void log​(Logger logger, Level level, String format, Throwable thrown, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logConfig​(Logger logger, String format, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logConfig​(Logger logger, String format, Throwable thrown, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logFine​(Logger logger, String format, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logFine​(Logger logger, String format, Throwable thrown, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logFiner​(Logger logger, String format, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logFiner​(Logger logger, String format, Throwable thrown, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logFinest​(Logger logger, String format, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logFinest​(Logger logger, String format, Throwable thrown, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logInfo​(Logger logger, String format, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logInfo​(Logger logger, String format, Throwable thrown, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logSevere​(Logger logger, String format, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logSevere​(Logger logger, String format, Throwable thrown, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logWarning​(Logger logger, String format, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void logWarning​(Logger logger, String format, Throwable thrown, Object... args)
      Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      static void sendStderrToLog()  
      static void sendStdoutToLog()  
      static void setConsoleLogger​(Formatter formatter, Level level)  
      static void setLoggerLevel​(Level level)  
      static void setTerseConsoleLogger​(Level level)
      In cases where a logging.properties file is too cumbersome, sets terse formatter for console handler.
    • Field Detail

      • FORCE

        public static final Level FORCE
        Log statements with this level are always loggable -- even for loggers set to OFF.
    • Constructor Detail

      • LoggerUtils

        public LoggerUtils()
    • Method Detail

      • getLogger

        public static Logger getLogger​(Class<?> clazz)
        Convenience wrapper around Logger.getLogger(String). Uses the fully qualified classname of the specified class as the logger name.
      • getLevelRecursive

        public static Level getLevelRecursive​(Logger logger)
        Traverses parents to find effective log level.
      • dumpAncestry

        public void dumpAncestry​(Logger logger)
        Prints own and parents' log levels to standard out (for debugging).
      • initLogging

        public static boolean initLogging​(URL... urls)
        This method tries to configure logging by loading a configuration from each of the specified URLs, in order, until an attempt succeeds. After the first successful attempt, the method returns true. If there are no successful attempts, the method returns false.

        Basic examples, using IoUtils.file(String) and IoUtils#resource(Class, String):

         public static void main( String[] args )
         {
             // Example 1: Use classpath resource "com/company/package/logging.properties"
             initLogging( Main.class.getResource( "logging.properties" ) );
             ...
        
             // Example 2: Use a file, or fall back to classpath resource if file is not available
             initLogging( file( "logging.properties" ), resource( Main.class, "logging.properties" ) );
             ...
         }
         
      • setTerseConsoleLogger

        public static void setTerseConsoleLogger​(Level level)
        In cases where a logging.properties file is too cumbersome, sets terse formatter for console handler.
        Parameters:
        level - maximum logging level; set on root logger
      • setConsoleLogger

        public static void setConsoleLogger​(Formatter formatter,
                                            Level level)
      • addTerseFileLogger

        public static final void addTerseFileLogger​(Level level,
                                                    String filename)
                                             throws SecurityException,
                                                    IOException
        In cases where a logging.properties file is too cumbersome, sets terse formatter for file handler.
        Parameters:
        level - maximum logging level; set on root logger
        Throws:
        IOException
        SecurityException
      • setLoggerLevel

        public static final void setLoggerLevel​(Level level)
      • sendStdoutToLog

        public static void sendStdoutToLog()
      • sendStderrToLog

        public static void sendStderrToLog()
      • log

        public static void log​(Logger logger,
                               Level level,
                               String format,
                               Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logFinest

        public static void logFinest​(Logger logger,
                                     String format,
                                     Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logFiner

        public static void logFiner​(Logger logger,
                                    String format,
                                    Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logFine

        public static void logFine​(Logger logger,
                                   String format,
                                   Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logConfig

        public static void logConfig​(Logger logger,
                                     String format,
                                     Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logInfo

        public static void logInfo​(Logger logger,
                                   String format,
                                   Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logWarning

        public static void logWarning​(Logger logger,
                                      String format,
                                      Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logSevere

        public static void logSevere​(Logger logger,
                                     String format,
                                     Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • log

        public static void log​(Logger logger,
                               Level level,
                               String format,
                               Throwable thrown,
                               Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logFinest

        public static void logFinest​(Logger logger,
                                     String format,
                                     Throwable thrown,
                                     Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logFiner

        public static void logFiner​(Logger logger,
                                    String format,
                                    Throwable thrown,
                                    Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logFine

        public static void logFine​(Logger logger,
                                   String format,
                                   Throwable thrown,
                                   Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logConfig

        public static void logConfig​(Logger logger,
                                     String format,
                                     Throwable thrown,
                                     Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logInfo

        public static void logInfo​(Logger logger,
                                   String format,
                                   Throwable thrown,
                                   Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logWarning

        public static void logWarning​(Logger logger,
                                      String format,
                                      Throwable thrown,
                                      Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.
      • logSevere

        public static void logSevere​(Logger logger,
                                     String format,
                                     Throwable thrown,
                                     Object... args)
        Wraps call to Java logger with varargs and performance optimization: no argument formatting if unneeded.