SpringBoot's default log configuration
SpringBoot is out of the box and provides you with the logging framework by default, using Commons Logging , but the default configuration also provides support for common logs, such as: Java Util Logging, Log4J, Log4J2 and Logback. Each Logger can use the console or file to output log content through configuration.
Console output
Regarding log logs, the first five log levels LEVELs are LEVEL: From high to low, ERROR , WARN , INFO , DEBUG , and TRACE , the lower level will output high-level information, and the high level will not output low-level information. For example: if the level is set to ERROR , the information of WARN , INFO , and DEBUG will not be output. In SpringBoot, ERROR , WARN and INFO levels are configured by default to the console. There is no FATAL level in Logback, it will be treated as an ERROR level.
We can switch to the DEBUG level in two ways:
application.properties . When this property is set to true, the core Logger (including embedded containers, hibernate, and spring) will output more content, but the logs you applied by yourself will not output to the DEBUG level.Colorful output
SpringBoot supports color log output since version 1.4.0. If your terminal supports ANSI, setting color output will make the log more readable. Supported by setting the spring.output.ansi.enabled parameter in application.properties .
NEVER : Disable ANSI-colored output (default item)DETECT : Will check whether the terminal supports ANSI. If yes, use color output (recommended item)ALWAYS : Always use ANSI-colored format to output. If the terminal does not support it, there will be a lot of interference information. It is not recommended to use it.File output
The default configuration of SpringBoot will only be output to the console and will not be recorded in files, but we usually need to record in files when using the production environment.
To increase file output, you need to configure logging.file or logging.path attribute in application.properties .
logging.file : Set the file, which can be an absolute path or a relative path. For example: logging.file=my.loglogging.path : Setting the directory, the spring.log file will be created under this directory and the log content will be written, such as: logging.path=/var/logThe log file will be truncated at 10Mb size, resulting in new log files. The default levels are: ERROR, WARN, INFO
Level control
In SpringBoot, you only need to configure the level control of completing logging in application.properties .
Configuration format: logging.level.*=LEVEL
logging.level : log level control prefix, * is the package name or Logger nameLEVEL : Options TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFFFor example:
DEBUG level.WARN levelCustom log configuration
If you don't want to use the default configuration, just add your own log configuration file. Since logging services are usually initialized before ApplicationContext is created, they do not have to be controlled through Spring 's configuration file. Therefore, log control and management can still be well supported through system properties and traditional Spring Boot external configuration files.
Depending on different logging systems, you can organize the configuration file name according to the following rules and it can be loaded correctly:
SpringBoot official recommendations to use a file name with -spring as your log configuration (such as using logback-spring.xml instead of logback.xml)
Custom output format
In SpringBoot, you can control the output format by configuring the following parameters in application.properties :
logging.pattern.console : defines the style output to the console (JDK Logger is not supported)logging.pattern.file : defines the style of output to the file (JDK Logger is not supported)These formats can also be defined directly in the log configuration file, rather than in the application configuration file.
Common Logback configuration file templates
<?xml version="1.0" encoding="UTF-8"?><configuration> <!--Define the storage address of the log file and do not use relative paths in the configuration of LogBack --> <property name="LOG_HOME" value="d:/logs"/> <!-- Color log dependency rendering class --> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/> <!-- Color log format--> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(--){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <!-- Console Output Settings--> <appender name="console"> <encoder> <!--Format output: %d represents the date, %thread represents the thread name, %-5level: Level displays 5 characters from the left %msg: log message, %n is a newline character --> <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>--> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>utf8</charset> </encoder> </appender> <!-- Generate log files every day --> <appender name="file"> <rollingPolicy> <!-- File name output by log file --> <fileNamePattern>${LOG_HOME}/mixedSys.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Output level of log logger (package.class) --> <logger name="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" level="DEBUG" > <appender-ref ref="console" /> <appender-ref ref="file" /> </logger> <logger name="org.springframework.boot" level="DEBUG"/> <!-- Customize for Hibernate sql--> <!-- <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" /> <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG" /> <logger name="org.hibernate.SQL" level="DEBUG" /> <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" /> <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" /> --> <!-- Log output level --> <root level="INFO"> <!-- At this time, the debug level information will be filtered--> <appender-ref ref="console" /> <appender-ref ref="file" /> </root> <!-- Log asynchronously to the database--> <!-- <appender name="DB"> Log asynchronously to database <connectionSource> Connection pool<dataSource> <driverClass>com.mysql.jdbc.Driver</driverClass> <url>jdbc:mysql://127.0.0.1:3306/databaseName</url> <user>root</user> <password>root</password> </dataSource> </connectionSource> </appender> --> </configuration>The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.