Class AppLog

java.lang.Object
io.avaje.applog.AppLog

public final class AppLog extends Object
Use of System.Logger for Application and Library logging.


   System.Logger logger = AppLog.getLogger("org.foo");
   logger.log(Level.INFO, "Hello {0}", "world");

 

Defaults to using JDK System.getLogger(String) but allows applications to provide an alternative System.Logger implementation.

Applications wanting control over the System.Logger implementation can firstly provide a System.LoggerFinder and have that loaded via ServiceLoader. This might not be possible for applications for example don't have a dedicated JVM.

Applications that can't use System.LoggerFinder can instead use AppLog.Provider to provide System.Logger implementations returned by AppLog. The reason to use AppLog is that it provides this 1 extra level of indirection that gives applications control over the System.Logger implementation returned by AppLog.


Main differences to slf4j-api

MessageFormat placeholders

System.Logger uses MessageFormat with placeholders like {0}, {1}, {2} ... rather than slf4j which uses {}. This means parameters can be referenced multiple times and the parameters can use formats like {0,number,#.##} {0,time} {0,date} etc. See MessageFormat for more details.

Default number format

By default, numbers are formatted based on locale so 8080 can be formatted as 8,080. For this reason, we often prefer to format integers like {0,number,#} rather than {0}.



   // use {1,number,integer} to format the int port so we get 8080 rather than 8,080
   logger.log(Level.INFO, "started with host {0} port {1,number,integer} ", host, port);

 

Throwable and vararg parameters

When logging a message with vararg parameters, slf4j will automatically try to detect if the last parameter is a Throwable and if so extract it and trim the parameters. System.Logger does not do this, instead we need to be explicit when logging Throwable and formatting the message if needed.



   System.Logger logger = AppLog.getLogger("org.foo");

   // using varargs parameters all ok with no Throwable
   logger.log(Level.INFO, "My {0} Hello {1} message", "silly", "world");
   try {
       someMethodThatThrows();
   } catch (Throwable e) {
     // need to format message here and explicitly pass throwable
     logger.log(Level.ERROR, MessageFormat.format("Error using {0} and {1}", "MyParam0", "OtherParam1"), e);
   }
 

No slf4j MDC or Markers

System.Logger API has no equivalent to slf4j MDC or Markers.

  • Method Details

    • getLogger

      public static System.Logger getLogger(String name)
      Return the logger for the given name.
      
      
        System.Logger logger = AppLog.getLogger("org.foo");
        logger.log(Level.INFO, "Hello {0}", "world");
      
       
      Parameters:
      name - The logger name
      Returns:
      The logger to use
    • getLogger

      public static System.Logger getLogger(Class<?> cls)
      Return the logger for the given class name.
      
      
        System.Logger logger = AppLog.getLogger(Foo.class);
        logger.log(Level.INFO, "Hello {0}", "world");
      
       
      Parameters:
      cls - The logger name provided by the class name.
      Returns:
      The logger to use
    • getLogger

      public static System.Logger getLogger(String name, ResourceBundle bundle)
      Return the logger for the given name and resource bundle.
      
      
        ResourceBundle bundle = ResourceBundle.getBundle("io.bazz.bar");
        System.Logger logger = AppLog.getLogger("io.bazz.Foo", bundle);
      
        logger.log(Level.WARNING, "HELLO_KEY", "world");
      
       
      Parameters:
      name - The logger name
      Returns:
      The logger to use