Clogr

Csar Logging Registration

Making SLF4J logging easier to put on.

Csar Logging Registration (Clogr) simplifies SLF4J logger access while providing compartmentalized logging configurations via Csar.

Quick Start

1. Include Clogr Dependency

Include the appropriate Clogr dependency from Maven. To support Logback, for example, simply add io.clogr:clogr-logback:x.x.x to your POM which transitively brings in the Logback and SLF4J dependencies as wel. If you merely wish easy logger access, instead add io.clogr:clogr:x.x.x and configure your SLF4J dependencies as you normally would.

pom.xml
<project>
  …
  <dependencies>
    …
    <dependency>
      <groupId>io.clogr</groupId>
      <artifactId>clogr-logback</artifactId>
      <version>x.x.x</version>
    </dependency>
  </dependencies>
</project>

2. Implement Clogged

Implement Cloggedto bring quick and easy logger access to any class.

MyClass.java
public class MyClass implements Clogged {
  …

3. Get a Logger

Acquire an SLF4J Logger instance via Clogged—the same logger you would get from a LoggerFactory.getLogger(getClass()), but easier and with the ability to compartmentalize logging configurations.

MyClass.java
public class MyClass implements Clogged {
  …

    getLogger().info("Clogr makes SLF4J logging easier.");
  …

4. Use multiple logging configurations

Using Csar (included) you can have the same code running in different threads use different loggers! Clogr's Logback extensions even allow legacy code using LoggerFactory.getLogger(getClass()) to retrieve the correct logger! (Static loggers will not be affected.) The following example assumes you have included the io.clogr:clogr-logback:x.x.x dependency.

MyClass.java
public class MyClass implements Clogged {
  …

    getLogger().info("foo"); //uses default logging configuration

    LogbackLoggingConcern localLoggingConcern = new LogbackLoggingConcern();
    try(final InputStream configFromResources = getClass().getResourceAsStream("other-config.xml")) {
      localLoggingConcern.configure(configFromResources);
    }

    Csar.run(localLoggingConcern, () -> {
      getLogger().info("bar"); //uses local, customized logging configuration
    }).join();

  …

Learn More

You can access an in-depth introduction to Clogr, and learn more about logging in general.