Logging

The Data Client Base Library uses grizzled-slf4j logging which provides a very thin Scala-friendly layer on top of the SLF4J (Simple Logging Facade for Java) API.

Grizzled-slf4j is a dependency of Data Client Base Library, so if your application depends on Data Client Base Library you can use grizzled-slf4j right away.

There are two ways of using it, either the classic way of getting a Logger or the Scala way of mixing in Logging trait.

Getting a Logger

To get a logger, use the Logger object and pass it a name, a class, or a type. By convention, a name should be a class name. This is the preferred way for Java.

Examples:

Using a name
Using a class
Using a type
import grizzled.slf4j.Logger

class Foo {
  val logger = Logger("org.example.foo")
}
import grizzled.slf4j.Logger

class Foo {
  val logger = Logger(classOf[Foo])
}
import grizzled.slf4j.Logger

class Foo {
  val logger = Logger[this.type] // or Logger[Foo]
}

Mixing in Logging trait

Instead of instantiating a Logger object and invoking its methods explicitly, you can mix the Logging trait into any class, which automatically:

  • adds logging methods (debug, info, error, etc.)
  • adds a lazily-instantiated Logger object
  • adds a logger property that you can use to retrieve the Logger object without changing the class’s public API

Example:

Using Logging trait
class Foo extends Logging {
  def test: Unit =
    info("test")
}

Logger Messages

The Logger class supports the following methods:

  • debug
  • error
  • info
  • trace
  • warn

For the complete details, see the API documentation.

Log Configuration

Add the corresponding log configuration to your project.

log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.here.platform.data.client.base" level="info" />
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

results matching ""

    No results matching ""