logs with spring boot - morris & opazo...2018/10/11 · to accomplish this, we are going to use...
TRANSCRIPT
Logs with Spring Boot
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
Following the 12 Factor App Pattern, it is necessary to keep separated the logs processing from the applicationitself.
To accomplish this, we are going to use the library slf4j (Simple Logging Facade for Java), which can be loadedthrough dependencies in the Spring Boot Framework.
Currently we are using JDK 1.8 to work with slf4j.
Context
Treat logs as an event stream, and keep the logic for routing and processing logs separate from the application itself.
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
Dependencies Required
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-
encoder</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${ch.qos.logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${ch.qos.logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>${ch.qos.logback.version}</version>
</dependency>
<properties>
<project.build.sourceEncoding>UTF-
8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<ch.qos.logback.version>1.2.3</ch.qos.logbac
k.version>
</properties>
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
Configurations Required
In resources folder (has to be in the classpath)
create a new file called: logback-spring.xml, it has
to start with the following:
<configuration>
<property></property>
<appender></appender>
<root></root>
….
</configuration>
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
Write Methods: Output Console
Known as appenders, they are XML elements that reference a class and can be configured via its children elements:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<springProfile name="default">
<pattern>%d{HH:mm:ss.SSS} [%thread] RID=%X{Slf4jMDCFilter.UUID}
RIP=%X{Slf4jMDCFilter.ClientIP} %-5level %logger{36} - %msg%n</pattern>
</springProfile>
<springProfile name="staging,production">
<pattern>%d{HH:mm:ss.SSS} [%thread] RID=%X{Slf4jMDCFilter.UUID}
RIP=%X{Slf4jMDCFilter.ClientIP} %-5level %logger{36} - %msg%n</pattern>
</springProfile>
</encoder>
</appender>
The most simple one is STDOUT, but the output could be anything from a file output, to log streaming to CloudWatch or logstash. The logger mustreference an appender in order to work.
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
Write Methods: File Output
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/${LOG_FILE}.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-
dd}.gz</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread]
RID=%X{Slf4jMDCFilter.UUID} RIP=%X{Slf4jMDCFilter.ClientIP} %-
5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
Write Methods: Stream Logstash
<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>127.0.0.1:4560</destination>
<!-- encoder is required -->
<encoder class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
Write Methods: Stream CloudWatch
<appender name="CLOUDWATCH" class="com.j256.cloudwatchlogbackappender.CloudWatchAppender">
<region>us-east-1</region>
<logGroup>your-log-group-name-here</logGroup>
<logStream>your-log-stream-name-here</logStream>
<layout>
<!-- possible layout pattern -->
<pattern>%d{HH:mm:ss.SSS} [%thread] RID=%X{Slf4jMDCFilter.UUID}
RIP=%X{Slf4jMDCFilter.ClientIP} %-5level %logger{36} - %msg%n</pattern>
</layout>
</appender>
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
slf4j Logging Levels
There are 4 levels of logging in sl4fj : Debug, Error, Info,
Warn. Whichever one you set at the root level, all child
loggers in a cascade fashion will inherit this property and log
according to the root level. So if you set the root logger to
INFO, then the application output will contain logs from
WARN and INFO. And if you set the logging level to ERROR,
then the output logs will contain WARN, INFO and ERROR
logs.
We Add Value to your Business
www.morrisopazo.com / [email protected] - Antofagasta - Temuco - Santiago
slf4j Logging Code
Import the LoggerFactory class and use the static method getLogger and pass in the current class as argument,which has methods that you want to log, then use the logger object to start logging at different levels.
public class Endpoint {
private static final Logger logger = LoggerFactory.getLogger(Endpoint.class);
@RequestMapping(value = "/", method = RequestMethod.GET)
public String demo(){
logger.warn("Warning Log Message");
logger.info("Info Log Message");
logger.error("Error Log Message");
logger.debug("Debug Log Message");
return "Hello World";
}
}