xxxxxxxxxx
/*
Create this class somwhere in your app directory structure
(More details on: https://www.baeldung.com/spring-http-logging - Section 5.1)
*/
@Configuration
public class RequestLoggingFilterConfig {
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter
= new CommonsRequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(10000);
filter.setIncludeHeaders(false);
filter.setAfterMessagePrefix("REQUEST DATA: ");
return filter;
}
}
/*
Add those to your application.properties file
(More details on: https://www.tutorialworks.com/spring-boot-log-to-file/)
*/
logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG
logging.file.path=/home/logs
logging.file.name=myapp.log
xxxxxxxxxx
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=ERROR
logging.level.net.guides=DEBUG
xxxxxxxxxx
// pom.xml
// If you are using spring-boot-starter-web, logging is automatically included with "spring-boot-starter-logging" jar
// application.properties
logging.level.org.springframework=debug // everything else will print as debug level
logging.level.[com.package.name]=trace // only this package will print as trace level
// Controlller
@Controller
public class SayHelloController {
private Logger logger = LoggerFactory.getLogger(getClass());
@RequestMapping("say-hello-jsp")
// @RequestParam lets the url take a value as argument to a parameter payload
// "ModelMap" helps to pass data from controller to the specified view
public String sayHelloJsp(@RequestParam(value="name", required = false)String name, ModelMap model){
model.put("name",name); // the name variable now can be accessed in the view with "name" keyword
// what you want to log
System.out.println(name); // This is NOT RECOMMENDED in prod environment
logger.debug(name); // debug level logging
logger.info("This will print on info level"); // info level logging
logger.warn("This will print on warn level"); // warn level logging
logger.trace("This will print on trace level"); // warn level logging
return "sayHello"; // the jsp file name
}
}
xxxxxxxxxx
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApp {
private static final Logger logger = LoggerFactory.getLogger(MyApp.class);
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
// Logging example
logger.trace("This is a TRACE level message.");
logger.debug("This is a DEBUG level message.");
logger.info("This is an INFO level message.");
logger.warn("This is a WARN level message.");
logger.error("This is an ERROR level message.");
}
}
xxxxxxxxxx
Lombok has annotations for that.
Annotate your class with @Slf4j and a static log fiedl will be generated.
xxxxxxxxxx
We can use logging with Spring Boot by specifying log levels on the application.properties file.
Spring Boot loads this file when it exists in the classpath and it can be used to configure both Spring Boot and application code.
Spring Boot, by default, includes spring-boot-starter-logging as a transitive dependency for the spring-boot-starter module.
By default, Spring Boot includes SLF4J along with Logback implementations.
If Logback is available, Spring Boot will choose it as the logging handler.
You can easily configure logging levels within the application.properties file without having to create logging provider-specific configuration files such as logback.xml or log4j.properties.
xxxxxxxxxx
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>