Configuration - epam/gflog GitHub Wiki

Configuration

This page describes how to configure gflog.

System Properties

Property Default Description
gflog.config Default config location. Use "classpath:" prefix to load a config from the classpath.
gflog.sync false Default log service in the default console configuration. Good for testing to not mess up log messages.

Implicit Configuration

gflog uses the following algorithm to configure itself automatically on the first usage of LogFactory::get* methods:

  1. Loads the configuration from gflog.config system property if specified. Goes to 4 if fails.
  2. Loads gflog-test.xml configuration from the classpath if found. Goes to 4 if fails.
  3. Loads gflog.xml configuration from the classpath if found. Goes to 4 if fails.
  4. Uses the default console configuration.

Explicit Configuration

gflog can be configured programmatically using the following API:

  1. LogConfigFactory - to load a configuration from file/classpath/stream.
  2. LogConfig - to create/change a configuration.
  3. LogConfigurator - to configure/unconfigure.
LogConfig config = LogConfigFactory.load("classpath:gflog-sample.xml");
        
Logger logger = config.getLogger("my-logger");                        
config.removeLogger(logger);       
        
LogConfigurator.configure(config);

Substitutions

Pass a Properties instance to LogConfigurator::configure to use the properties as substitutions when loading a configuration from a xml file. System properties are used by default if called without.

Properties properties = new Properties(System.getProperties());  
properties.put("app.name", "my-app");                                         // use ${app.name} in a xml config
        
LogConfigurator.configure("/my-absolute-path/gflog-sample.xml", properties);

Gflog supports nested substitutions in default values from system properties and environment variables:

  • ${some.property:-some.value} - resolved from the passed properties or from system properties by default.
  • ${sys:some.property:-some.value} - resolved from system properties.
  • ${env:SOME_PROPERTY:-some.value} - resolved from environment variables.
  • ${env:SOME_PROPERTY:-${sys:some.property:-some.value}} - resolved from environment variables if found, otherwise from system properties.

Shutdown Hook

Use LogConfigurator::configureWithShutdown to register a shutdown hook to automatically unconfigure gflog. But be aware that you can miss some log messages at the end because shutdown hooks are executed in parallel. So gflog can be unconfigured before some code logs messages. The best way to prevent this is to configure gflog without a shutdown hook and to register one shutdown hook which stops your application and then unconfigures gflog.

 Runnable shutdownHook = () -> {
    try {
        myApplication.stop();
    } finally {
        LogConfigurator.unconfigure();
    }
};

Thread threadToRunShutdownHook = new Thread(shutdownHook);
Runtime.getRuntime().addShutdownHook(threadToRunShutdownHook);