"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Boot Logging Date : 13/02/2025 (Session - 123) _____________________________________________________________________________________________________________________________ Today Session ============= * Logging is one of important aspect of Software development which helps to track activites of an application easily. * During development of project (or) when Debugging code actually we are using SOP statements which can't be useful for realtime development for debugging code. * As we know that SOP statements are always get displayed on Console and not all storing that SOP statements any where in file for future references. Example ======= Develop the Code >>>> Pushed changes to Dev Env >>>> Your Code is working as expected >>>>>> No issues Your code will promoted to other higher environment >>>> QA >>>> In QA your code is not working >>>>> Simply will get the log file of QA environment.... * Inorder to overcome all the problems we need to implement logging mechanism in Project using some logging libraries in Java such as java.util.logging,log4j,Apache Commons Logging,SLF4J,Logback etc., System.out.println("First name :::::" + firstName); // this will print on Application console log.info("First name :::::" + firstName); // this will print in log file of an application * Every logging library main contains three components 1) logger >>>>>> Logger component is used to decide which classes to be logged. Ex:Controller,Service,DAO,Configuration,Helper classes in project etc Entity,POJO,DTO classes are not required for Logging. How to get the logger/log object in the above classes ===================================================== private static Logger logger = LoggerFactory.getLogger(WelcomeController.class); private static Logger logger = LoggerFactory.getLogger(UserService.class); private static Logger logger = LoggerFactory.getLogger(DemoService.class); private static Logger logger = LoggerFactory.getLogger(LoginController.class); Log Methods =========== 1) DEBUG >>>>> logger.debug("This is Debug Message"); >>>> logging for debugging message. 2) INFO >>>>> logger.info("This is info message"); >>>> Logging for Information messsage 3) WARN >>>>> logger.warn("This is Warn Message); >>>> Logging for warnings to user 4) ERROR >>>>> logger.error("This is Error Messsage") >>>> Logging Exceptions 5) FATAL >>>>> logger.fatal("This is Fatal Message"); >>>> Logging for Severity Errors such configuration missings. Setting log level ================= Every Application need to set some log level based on that log level that log messages will be written into log file. 2) Appender >>>>>> Appender Component is used where to store the Log messages 1) FileAppender >>>>>>>>>> It is used to store log messages into File (application.log) 2) ConsoleAppender >>>>>>>>>> It is used to print the log messages to console 3) SMTPAppender >>>>>>>>>> It is used to write log messages to email NOTE ==== Every Application will requires atleast one Appender (or) more than one Appender This appender is at application level. 3) Layout >>>>>> Layout component is used how to print the Log message in required format %d — date, %level — log level , %c — class path, %t — thread executing, %m — message,%n — new line %clr-color,%L-line numnber, %M - Method For More information : https://logging.apache.org/log4j/2.x/manual/layouts.html Spring Boot Logging =================== * Spring boot support logging mechanism by using slf4j & logback libraries. * let us try to understand the log format in spring boot applciation * Let’s break the log message and understand what each term means , 2022–07–16 13:16:51 — Date and Time >>>> %d{yyyy-MM-DD HH:MM:SS} INFO — Log Level >>>> %level 12620 — Process ID >>>> %pid [nio-8080-exec-1] — Thread name >>>> %t com.logging.Controller — Logger name >>>> %c Initializing Spring embedded WebApplicationContext — Log message >>>>> %m * Spring Boot logging will comes along with Starters implictly Spring-boot-starter-web >>>>>>>>>>> Spring-boot-starter >>>>>>>>>>>> spring-boot-starter-logging >>>>>>>>>>>>>>>spring-jcl(Spring Commons Logging Bridge) * By Default Logging level of an Spring Boot Application is "INFO" Means Info,Error,Warn Messages are get logged automatically. * We can change the log level in spring boot application by adding the below configuration entry in application.properties application.properties ====================== #Server Port configuration server.port=9878 #changing the log level of spring boot application logging.level.root=debug logging.level.com.ashokit=info logging.level.org.springframework.web=error #log message layout configuration logging.pattern.console=%d{dd-MM-YYYY HH:MM:SS} %clr([%level]){blue} [%c]-{%M} {%t} %m%n #Writing the logs into file logging.file.name=logs/application.log #log message layout configuration logging.pattern.file=%d{dd-MM-YYYY HH:MM:SS} %clr([%level]){blue} [%c]-{%M} {%t} %m%n #Logging banner into application.log spring.main.banner-mode=log #setting some size to log files logging.logback.rollingpolicy.max-file-size=20KB #15days log files will be stored logging.logback.rollingpolicy.max-history=15 WelcomeController.java ====================== package com.ashokit.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/welcome") //@Slf4j public class WelcomeController { private static Logger log = LoggerFactory.getLogger(WelcomeController.class); @GetMapping("/{username}") public ResponseEntity getWelcome(@PathVariable("username") String username){ log.debug("This is for Debug Message"); log.info("This is for Information Message"); log.error("This is for Error Message"); log.trace("This is for Trace Message"); log.warn("This is for Warn Message"); return new ResponseEntity("Welcome To AshokIT"+username,HttpStatus.OK); } } Testing the Application ======================= * Run the Spring Boot Application and access the API Endpoint to generat the log messages on console http://localhost:9878/welcome/Mahesh * Refresh our project and find logs folder in that log file is available NOTE ==== * Basically in application development we are not at all configuring logging properties in application.properties (or) application.yml file because if we require any changes we need to modify the properties (or) yaml file changing these things will take redeployement and restarting the application. * Inorder to overcome the above problem we need to maintain logging configuration in xml file to avoid redeployement simply requires restarting of an application. * In spring boot application we can maintain logging configuration with xml file name as logback.xml (or) logback-spring.xml. * Create xml file under src/main/resources logback.xml =========== ${LOG_FILE} logs/archived/applicationlogs.%d{yyyy-MM-dd}.%i.log 5KB 20KB 60 %d %p %c{1.} [%t] %m%n %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n smtp.gmail.com 587 true mahesh.ashokit@gmail.com APP Password mahesh.ashokit@gmail.com mahesh.ashokit@gmail.com ApplicatioErrors: %logger{20} - %m Distributed Logging & Tracing ============================= * When the Microservices are in intra communication enabling tracing and logging activites across the multiple microservices to finding out execution flow of each an every request to response called Distributed Logging & Tracing. * Inorder to implement Distributed logging &tracking in microservices we need to used some libraries i.e.,Sleuth & Zipkin from Spring Cloud ecosystem. Sleuth ====== * A Spring cloud component providing unique id for each request flow....The Programmer can use these unique ids to find out the execution flows of Requests. Two types of Ids ================ 1) tracedId : UniqueId for each request flow across the multiple microservices...If developers get this traceid he can find out all the microservices that are involved in a request flow. 2) spanId : UniqueId given for each microservice....if developer get this spanid then he can find out all the executions that happens in a microservice. Zipkin Client ============= * We need to add zipclient dependency to every microservice along with sleuth dependency. * Zipkin contains one predefined class i.e.,Sampler we need to create spring bean object for this class because it will collects data from all microservices of an request. * Zipkin Provides the lot of details related traceid,spanid etc., with respect to distributed logging and tracing enabled across the multiple microservices. Zipkin Server ============= * It provides User Interface environment running on port number 9411 * Zipkin Client of each microservice collects traceid/spanid of distributed logging from sleuth and component and gives to zipkin server to display having UI. NOTE ==== * while working with Distributed Logging and Tracing each MS contains its own zipkin client and sleuth component...but there must be only one zipkin server that collects data from all zipkin clients of different microservices as Sampler Object to display as UI. Steps for implementing Distributed logging and tracing ====================================================== 1) Download the zipkin server from web in given url : https://zipkin.io/pages/quickstart Goto Java Section >>>> Click on latest Release automatically Jar file gets downloaded 2) We need to start the zipkin server by using command prompt. Open Command Prompt >>>> Downloaded Jar File is (Desktop) >>> execute the following command >>>> java -jar zipkin-server-2.24.1-exec.jar Zipkin Server always running on port number as 9411 and we can give request from browser i.e.,http://localhost:9411 3) We already Developed two Microservices earlier i.e.,Customer-Service,Address-Service and Customer Microservice will communicate with Address Microservices to fetch address details of an Customers Add the below dependencies in pom.xml ===================================== org.springframework.cloud spring-cloud-sleuth-zipkin org.springframework.cloud spring-cloud-starter-sleuth spring-cloud-starter-sleuth :::: this dependency we need to collect from maven central repository https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-sleuth/3.1.8 spring-cloud-sleuth-zipkin ::::: We can add this through Add Starter option from STS IDE Of our project 4) We need to create Spring Bean Object for Sampler Class from Sleuth Libarary Open ApplicationConfig.java classes from Customer-Service and Address-Service @Bean public Sampler alwaysSampler() { return Sampler.ALWAYS_SAMPLE; } 5) Make sure we need to provide log file for each microservice as well application.yml (Customer-Service) ================================== logging: file: name: logs/customer-service-api.log application.yml (Address-Service) ================================= logging: file: name: logs/Address-service-api.log 6) we need to start the below microservices * Registry Application * Customer-service * Address-Service 6) We need to give some request from browser/postman tool http://localhost:9494/api/customers/1 http://localhost:9494/api/customers/fetchAllCustomers http://localhost:9595/api/address/1 http://localhost:9595/api/address/allAddress 7) http://localhost:9411 >>>> Explore the Results on zipkin Server UI