satya posted: " Logging in spring boot is very flexible and easy to configure. Spring boot supports various logging providers through some simple configuration. Spring boot active enabled logging is determined by spring-boot-starter-logging artifact and it's"
Logging in spring boot is very flexible and easy to configure.
Spring boot supports various logging providers through some simple configuration.
Spring boot active enabled logging is determined by spring-boot-starter-logging artifact and it's auto-configuration which enables anyone of supported logging providers (Java UtilLogging, Log4J2, and Logback) based on configuration provided.
If we do not provide any logging specific configuration, we will still see logs printed in "console". These are because of default logging support provided in spring boot which uses Logback.
Spring boot's internal logging is written with Apache Commons Logging so it is one and only mandatory dependency.
spring-boot-starter-web depends on spring-boot-starter-logging, which pulls in spring-jcl for us.
Add log statements
To add log statements in application code, use org.slf4j.Logger and org.slf4j.LoggerFactory from SLF4J.
It provides lots of useful methods for logging and also decouple the logging implementation from application.
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("Simple log statement with inputs {}, {} and {}", 1,2,3); } }
Logging Level
Logback supports ERROR, WARN, INFO, DEBUG, or TRACE as logging level.
By default, logging level is set to INFO. It means that code>DEBUG and TRACE messages are not visible.
To enable debug or trace logging, we can set the logging level in application.properties file.
Also, we can pass the –debug or –trace arguments on the command line while starting the application.
# In properties file debug=true # In Console $ java -jar target/my-app-0.0.1-SNAPSHOT.jar --trace
We can apply logging levels to specific packages as well. It can be done either in console or application.properties file.
# In Console -Dlogging.level.org.springframework=ERROR -Dlogging.level.com.howtodoinjava=TRACE # In properties file logging.level.org.springframework=ERROR logging.level.com.howtodoinjava=TRACE
If the log level for a package is defined multiple times with different log levels, the lowest level will be used. TRACE is lowest and ERROR is highest.
Log format
The default log statement formatting is mentioned in defaults.xml file.
%d is the current date, and the part in curly braces is the format. ${VARIABLE}:-default is a way of specifying that we should use the $VARIABLE environment variable for the format, if it is available, and if not, fall back to default. This is handy if you want to override these values in your properties files, by providing arguments, or by setting environment variables.
In this example, the default format is yyyy-MM-dd HH:mm:ss.SSS unless we specify a variable named LOG_DATEFORMAT_PATTERN. In the logs above, we can see 2020-10-19 10:09:58.152 matches the default pattern, meaning we did not specify a custom LOG_DATEFORMAT_PATTERN.
${LOG_LEVEL_PATTERN:-%5p}
Uses the LOG_LEVEL_PATTERN if it is defined, else will print the log level with right padding up to 5 characters (E.g "INFO" becomes "INFO " but "TRACE" will not have the trailing space). This keeps the rest of the log aligned as it'll always be 5 characters.
${PID:- }
The environment variable $PID, if it exists. If not, space.
t
The name of the thread triggering the log message.
logger
The name of the logger (up to 39 characters), in our case this is the class name.
%m
The log message.
%n
The platform-specific line separator.
%wEx
If one exists, wEx is the stack trace of any exception, formatted using Spring Boot's ExtendedWhitespaceThrowableProxyConverter.
Spring Environment
System Property
Comments
logging.exception-conversion-word
LOG_EXCEPTION_CONVERSION_WORD
The conversion word used when logging exceptions.
logging.file
LOG_FILE
If defined, it is used in the default log configuration.
logging.file.max-size
LOG_FILE_MAX_SIZE
Maximum log file size (if LOG_FILE enabled). (Only supported with the default Logback setup.)
logging.file.max-history
LOG_FILE_MAX_HISTORY
Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.)
logging.path
LOG_PATH
If defined, it is used in the default log configuration.
logging.pattern.console
CONSOLE_LOG_PATTERN
The log pattern to use on the console (stdout). (Only supported with the default Logback setup.)
logging.pattern.dateformat
LOG_DATEFORMAT_PATTERN
Appender pattern for log date format. (Only supported with the default Logback setup.)
logging.pattern.file
FILE_LOG_PATTERN
The log pattern to use in a file (if LOG_FILE is enabled). (Only supported with the default Logback setup.)
logging.pattern.level
LOG_LEVEL_PATTERN
The format to use when rendering the log level (default %5p). (Only supported with the default Logback setup.)
PID
PID
The current process ID (discovered if possible and when not already defined as an OS environment variable).
The following table describes the mapping of log levels to colors:
FATAL
Red
ERROR
Red
WARN
Yellow
INFO
Green
DEBUG
Green
TRACE
Green
Logging to file
By default spring boot logs to console only.
If we want to enable file logging, we can easily do it using simple propertylogging.file or logging.path.
When using logging.path, it will create a file named spring.log in mentioned package.
application.properties # Output to a temp_folder/file logging.file=c:/temp/application.log #logging.path=/my-folder/ # Logging pattern for file logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%
Logback Logging
The default logging is good enough for most usecases. But sometimes in enterprise applications, we need more fine control over logging with other complex requirements. In that case, having a dedicated logging configuration is suitable.
Spring boot by default uses logback, so to customize it's behavior, all we need to add only logback.xml in classpath and define customization over the file.
As mentioned earlier, spring boot uses logback as default.
So if we have to use any other logging framework e.g. log4j2, we must exclude logback from classpath of the application. Also, add spring-boot-starter-log4j2 to classpath.
Now, add log4j2 specific configuration file in classpath (typically in resources folder).
It can be named as any of the following:
log4j2-spring.xml
log4j2.xml
If we have logging configuration in any other file (e.g. log4j2.properties, applogs.xml etc), we can use logging.file property to specify it's path application.properties file.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.