-
Notifications
You must be signed in to change notification settings - Fork 28
Logging
This article describes how to enable logging for SteemJ and projects based on SteemJ.
TOC
Prior to SteemJ version 0.4.0, SteemJ was shipped with a logging framework implementation. As this is a bad practice for several reasons, SteemJ 0.4.0 removed this implementation and allows you to configure the logging on your own.
To achieve this, SteemJ only contains the slf4j-api and expects you, to add the slf4j implementation of your choice to your project. A list of the most common implementations can be found at www.slf4j.org/manual, while you can find a lot more in the "vastness of the internet".
One good example of an alternative slf4j implementation is log4j-slf4j-impl, which implements the slf4j-api and adds the powerfull configuration options of log4j2.
As a first step, the log4j-slf4j-impl jar needs to be added to your project. The most easiest way to achieve this is the usage of a Build-Management tool like Maven, because you only need to add the required dependencies the pom.xml.
<dependency>
<groupId>eu.bittrade.libs</groupId>
<artifactId>steemj-core</artifactId>
<version>${steemJ.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
To finalize the logging configuration of your project, create a log4j2.xml and add it to the src/main/resources folder. An example log4j2.xml configuration is shown below.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<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="eu.bittrade.libs.steemj" level="WARN">
<AppenderRef ref="Console" />
</Logger>
<Logger name="my.project" level="DEBUG">
<AppenderRef ref="Console" />
</Logger>
<Root level="ERROR" />
</Loggers>
</Configuration>
The configuration above would append all log messages of:
- SteemJ, that have a severity of WARN or higher
- your project (package my.project), that have a severity of DEBUG or higher to the console, while ignoring all other log messages, because no logger is defined for Root.
If you want to use log4j2 in your project too, you should have a look at the official documentation to find detailed configuration examples.
This project is developed by dez1337