Having set up your spring-boot microservice and want to switch to the refcodes-logger tool-box because you want to have some fancy log output and want to log into a NoSQL database later on? Here is a quick start on how to enable the refcodes-logger-alt-console logger which produces fancy output.
After having tweaked your logger, you may produce fancy output as below, even on a Windows
machine:
The output as a console copy’n’paste dump (note that the tilde character
~
is used to truncate long lines; all aboveWARNING
is not truncated):
steiner@proteus:~/Workspaces/org.refcodes/refcodes-runtime-ext/refcodes-runtime-ext-console$ java -jar target/refcodes-runtime-ext-console-0.1.1-SNAPSHOT.jar ───────┬───────────────────┬───────┬───────────────┬──────────────────────────────┬──────────────────────┬──────────────────────────────────────────────────── 0000001│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │~12 12:19:35 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 0000002│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │Console width := 158 0000003│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │Console height := 24 0000004│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │ANSI support := true 0000005│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │Operating system := UNIX 0000006│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │CLI := SHELL 0000007│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │Encoding := UTF8 0000008│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │Process (PID) := 13082 0000009│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │~rg.refodes.runtime.ext.console.SystemInfoConsoleApp 0000010│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │stty size := 0000011│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │tput cols := 80 0000012│2015-04-04T11:21:19│INFO │main │~.console.SystemInfoConsoleApp│printInfo() │tput lines := 24 steiner@proteus:~/Workspaces/org.refcodes/refcodes-runtime-ext/refcodes-runtime-ext-console$
First of all, disable all the logger SLF4J
bindings spring-boot
brings with it; this is done by excluding the spring-boot-starter-logging
artifact in your project’s pom.xml
as such:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
Now we are ready to bind SLF4J
to the refcodes-logger
framework using the refcodes-logger-ext-slf4j
binding:
1
2
3
4
5
6
7
8
9
<dependencies>
...
<dependency>
<groupId>org.refcodes</groupId>
<artifactId>refcodes-logger-ext-slf4j</artifactId>
<version>3.0.0</version>
</dependency>
...
</dependencies>
Last we bind the refcodes-logger
framework to the refcodes-logger-alt-console
implementation, which does all the fancy log formatting and coloring:
1
2
3
4
5
6
7
8
9
<dependencies>
...
<dependency>
<groupId>org.refcodes</groupId>
<artifactId>refcodes-logger-alt-console</artifactId>
<version>3.0.0</version>
</dependency>
...
</dependencies>
You do not need to add any additional Maven repositories as everything is hosted at Maven Central.
Your modified pom.xml
now looks similar to the result below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
...
<dependency>
<groupId>org.refcodes</groupId>
<artifactId>refcodes-logger-alt-console</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.refcodes</groupId>
<artifactId>refcodes-logger-ext-slf4j</artifactId>
<version>3.0.0</version>
</dependency>
...
</dependencies>
That’s it! Your logs are pretty printed with the default settings of the refcodes-logger-alt-console
implementation!
Need to tweak?
See the article refcodes-logger: Fancy runtime-logs and highly scalable cloud logging on how to tweak your logs and on how to log into a
NoSQL
data sink!