Loading
 
What's Next: View All Tutorial →

Spring Boot - Sl4j Logging Example

Spring boot application has dependecy jar for Sl4J logger implementaion  We need to add the logger property configuration in the application.properties application.properties   # =...

Published Date: 05/09/2018  Last Modified Date: 05/09/2018    New Post



Spring boot application has dependecy jar for Sl4J logger implementaion 

We need to add the logger property configuration in the application.properties

application.properties

 
# ===============================
# Logger statement
# ===============================

logging.level.root=info
logging.level.org.springframework=debug
logging.path=c:/logs
logging.file=${logging.path}/log.log
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n



logging.pattern.file - prints the logger in the file

logging.pattern.console - prints the logger in the console

To enable debugger statement for our user define class. we need to add out package suffix with logging.level
For Example  
logger.level.org.springframework =debug   - this print all the debug level statement stating with package org.springframework

 

Application.java

 
package com.demo.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
	private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);

		LOGGER.info("Application Started");

	}
}

private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
LOGGER.info("Application Started");
 

Output:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

05-09-2018 09:58:34.155 [main] INFO  com.demo.spring.Application.logStarting - Starting Application on e24-PC with PID 2500 (C:\work\spring-boot-demo\target\classes started by e24 in C:\work\spring-boot-demo)
05-09-2018 09:58:34.160 [main] INFO  com.demo.spring.Application.logStartupProfileInfo - No active profile set, falling back to default profiles: default
05-09-2018 09:58:34.194 [main] INFO  org.springframework.context.annotation.AnnotationConfigApplicationContext.prepareRefresh - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3b07a0d6: startup date [Wed Sep 05 09:58:34 IST 2018]; root of context hierarchy
05-09-2018 09:58:34.884 [main] INFO  org.springframework.jmx.export.annotation.AnnotationMBeanExporter.afterSingletonsInstantiated - Registering beans for JMX exposure on startup
05-09-2018 09:58:34.899 [main] INFO  com.demo.spring.Application.logStarted - Started Application in 1.064 seconds (JVM running for 1.665)

05-09-2018 09:58:34.901 [main] INFO  com.demo.spring.Application.main - Application Started

05-09-2018 09:58:34.903 [Thread-3] INFO  org.springframework.context.annotation.AnnotationConfigApplicationContext.doClose - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3b07a0d6: startup date [Wed Sep 05 09:58:34 IST 2018]; root of context hierarchy
05-09-2018 09:58:34.905 [Thread-3] INFO  org.springframework.jmx.export.annotation.AnnotationMBeanExporter.destroy - Unregistering JMX-exposed beans on shutdown
Awaiting for Administrator approval




← Back to list


Related Post




×