Logging like the nerds log

Logging like the nerds log

Logging like the nerds log

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 as below, even on a Windows machine:

Fancy logger output

The output as a console copy’n’paste dump (note that the tilde character ~ is used to truncate long lines; all above WARNING 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           │~le.impls.SystemInfoConsoleApp│printInfo()           │~12 12:19:35 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
0000002│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │Console width    := 158                             
0000003│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │Console height   := 24                              
0000004│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │ANSI support     := true                            
0000005│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │Operating system := UNIX                            
0000006│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │CLI              := SHELL                           
0000007│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │Encoding         := UTF8                            
0000008│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │Process (PID)    := 13082                           
0000009│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │~odes.runtime.ext.console.impls.SystemInfoConsoleApp
0000010│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │stty size        :=                                 
0000011│2015-04-04T11:21:19│INFO   │main           │~le.impls.SystemInfoConsoleApp│printInfo()           │tput cols        := 80                              
0000012│2015-04-04T11:21:19│INFO   │main           │~le.impls.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 <dependencies>
 2   ...
 3   <dependency>
 4     <groupId>org.springframework.boot</groupId>
 5     <artifactId>spring-boot-starter</artifactId>
 6     <version>${spring-boot.version}</version>
 7     <exclusions>
 8       <exclusion>
 9         <groupId>org.springframework.boot</groupId>
10         <artifactId>spring-boot-starter-logging</artifactId>
11       </exclusion>
12     </exclusions>
13   </dependency>
14   ...
15 </dependencies>

Now we are ready to bind SLF4J to the refcodes-logger framework using the refcodes-logger-ext-slf4j binding:

1 <dependencies>
2   ...
3   <dependency>
4     <groupId>org.refcodes</groupId>
5     <artifactId>refcodes-logger-ext-slf4j</artifactId>
6     <version>1.1.7</version>
7   </dependency>
8   ...
9 </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 <dependencies>
2   ...
3   <dependency>
4     <groupId>org.refcodes</groupId>
5     <artifactId>refcodes-logger-alt-console</artifactId>
6     <version>1.1.7</version>
7   </dependency>
8   ...
9 </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 <dependencies>
 2   ...
 3   <dependency>
 4     <groupId>org.springframework.boot</groupId>
 5     <artifactId>spring-boot-starter</artifactId>
 6     <version>${spring-boot.version}</version>
 7     <exclusions>
 8       <exclusion>
 9         <groupId>org.springframework.boot</groupId>
10         <artifactId>spring-boot-starter-logging</artifactId>
11       </exclusion>
12     </exclusions>
13   </dependency>
14   ...
15   <dependency>
16     <groupId>org.refcodes</groupId>
17     <artifactId>refcodes-logger-alt-console</artifactId>
18     <version>1.1.7</version>
19   </dependency>
20   <dependency>
21     <groupId>org.refcodes</groupId>
22     <artifactId>refcodes-logger-ext-slf4j</artifactId>
23     <version>1.1.7</version>
24   </dependency>
25   ...
26 </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 to tweak your logs and to log into a NoSQL data sink …

comments powered by Disqus