9 Logging
9 Logging
9 Logging
Logging
============
=> To understand runtime behaviour of the application we will use logging in the
application.
=> With the help of logging we can identify root cause of the exception.
1) log4j
2) log4j2
4) logback
5) logstash
=======================
Logging Architecture
=======================
1) Logger
2) Layout
3) Appender
==================
What is Logger ?
==================
=> Logger is a predefined class which provides several methods to perform logging.
Ex:
void m1(){
// logic
Note: For every java class we will create one logger object.
=================
What is Layout ?
=================
====================
What is Appender ?
====================
=======================
Spring Boot logging
=======================
=> Run the boot application from the start class and check console.
Note: We can see several log msgs on the console. That means springboot is using
logging to provide runtime behaviour of springboot.
Note: In springboot by default Pattern Layout and Console Appender will be used.
Note: To print log msgs in the log file then add below property in
application.properties file
logging.file.name=app.log
-------------------------------------------------------------------------------
@RestController
public class MsgRestController {
@GetMapping("/welcome")
public String getWelcomeMsg() {
return msg;
}
}
----------------------------------------------------------------------------
@RestController
public class ContactRestController {
return response;
}
===============
Logging Levels
===============
EX: logger.trace("msg");
Ex: logger.debug("msg") ;
Ex: logger.info("msg");
Ex: logger.warn("msg");
Ex: logger.error("msg");
=> We can change log level in the application using below property
logging.level.root=ERROR
Note: When we set log level, log msgs will be printed from that level to all higher
levels also.
======================
Logging with Rolling
======================
=> If we use single log file to store log msgs then after few days log file size
will become very very big.
=> If file size is keep on increasing then it will become difficult to open/read
log file data.
=> Size Based Rolling is used to create new log file once old log is reached to
given limit.
Ex: 1 GB
=> Time based rolling is used to create every day new log file.
1) application.properties
2) logback.xml
=========================
What is logback.xml ?
=========================
3) Log Level
------------------------------- logback.xml
------------------------------------------
<configuration>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>ashokit.log</file>
<encoder>
<pattern>%d [%thread] %-5level %-50logger{40} - %msg%n</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>ashokit-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>1GB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="RollingFile" />
</root>
</configuration>
-----------------------------------------------------------------------------------
-----
================
Log Monitoring
================
=> It is the process of reading or observing log msgs available in log file.
=> To get log msgs from log file we have several tools.
1) Putty
2) WinScp
Note: Putty and WinScp softwares are used to connect with Linux VM.
=> In Realtime, operations team will share log server details like below
username : loguser
Password: loguser@ashokit
=========
Summary
=========
1) What is Logging
2) Why Logging
3) Logging Architecture
4) Log Levels
5) Logging in SpringBoot
6) Rolling Policies
7) Log Monitoring
- Putty
- WinScp
- ELK
- SPlunk