Skip to content

a-hansen/alog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Alog

Overview

An asynchronous log handler for JUL (Java Util Logging). It prints log messages on separate threads for low latency logging.

  • Minimal application interference.
  • Small code / jar size.
  • Files will be zipped after they reach a configurable size and the number of zip backups will be trimmed to a configurable maximum.

There are two ways to use Alog: programmatically and with configuration files.

Dependency Management

  • Repository : JCenter
  • groupId : com.comfortanalytics
  • artifactId : alog

Programmatic Usage

Initially acquire logs with Alog.getLogger(). It will add a log handler only if one has not already been added.

import com.comfortanalytics.alog.*;

public static void main(String[] args) {
    Logger log = Alog.getLogger("myLog", new File("myLog.log")));
    AsyncLogHandler handler = Alog.getHandler(log);
    handler.setThrottle(100);
}

Multiple logs can share the same file. A single FileLogHandler will be maintained for each absolute file path. Be aware that once closed, none of the other logs can use the same handler.

import com.comfortanalytics.alog.*;

public static void main(String[] args) {
    Logger log = Alog.getLogger("myLog", new File("myLog.log")));
    Logger another = Alog.getLogger("anotherLog", new File("myLog.log")));
    if (log.getHandlers()[0] == another.getHandlers()[0]) {
        System.out.println("This will print.");
    }
}

The root log handler that prints to System.out should be replaced as well.

import com.comfortanalytics.alog.*;

public static void main(String[] args) {
    Alog.replaceRootHandler();
}

Configuration Files

Alog uses configuration as specified by Java Util Logging. See javadoc for java.util.logging.LogManager for details.

There are two handlers:

  • com.comfortanalytics.alog.FileLogHandler
  • com.comfortanalytics.alog.PrintStreamLogHandler

The following keys can be used with both:

  • com.comfortanalytics.alog.filter is the name of a Filter class to use (defaults to no Filter).
  • com.comfortanalytics.alog.formatter is the name of a Formatter class to use (defaults to null and uses an optimized Alog format) .
  • com.comfortanalytics.alog.inferCaller is a boolean that determines whether or not to infer the source class and method name before submitting the log record for async processing (expensive, so the default is false).
  • com.comfortanalytics.alog.level is the default level for the Handler (defaults to INFO).
  • com.comfortanalytics.alog.maxQueue is the max async queue size above which records are ignored (defaults to 25000, use 0 for infinite).
  • com.comfortanalytics.alog.throttle is the percentage (0-100) of the maxQueue after which log records less than INFO are ignored (defaults to 90%). A value of 100 effectively disables the throttle.

The following keys can also be used with the FileLogHandler:

  • com.comfortanalytics.alog.backupThreshold is the approximate file size in bytes to zip up the log file and store it with a timestamp appended to the file name (default is 10000000 bytes).
  • com.comfortanalytics.alog.encoding is the charset for encoding log files (default is "UTF-8").
  • com.comfortanalytics.alog.filename is the pattern for generating the output file name. See below for details. (default is "java.log").
  • com.comfortanalytics.alog.maxBackups is the number of zip backups to maintain (default is 10).

The filename pattern uses the following tokens:

  • "/" represents the local pathname separator.
  • "%t" is the system temporary directory.
  • "%h" is the value of the "user.home" system property.
  • "%%" translates to a single percent sign "%".

Example configuration for an async file handler:

myLog.handlers=com.comfortanalytics.alog.FileLogHandler
com.comfortanalytics.alog.level=CONFIG
com.comfortanalytics.alog.maxQueue=50000
com.comfortanalytics.alog.backupThreshold=100000000
com.comfortanalytics.alog.filename=%halog.log
com.comfortanalytics.alog.maxBackups=5

To replace the root handler that prints to the console with one that does it ansynchronously:

handlers=com.comfortanalytics.alog.PrintStreamLogHandler
com.comfortanalytics.alog.level=FINE
com.comfortanalytics.alog.maxQueue=5000
com.comfortanalytics.alog.throttle=95