Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement super-detailed log configuration of springboot through logback

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to achieve springboot super detailed log configuration through logback. The quality of the article is high, so Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.

preface

There are several logging frameworks under Java Web, such as logback, log4j, log4j2 (slj4f is not a logging framework, it is equivalent to defining a specification, and the logging framework that implements this specification can be called with slj4f). Among them, the highest performance should make logback, and springboot defaults to logback logs, so this article will explain the logback log configuration scheme in detail.

The main contents are as follows:

·Logback configuration file composition·How to output logs to files·How to divide logs by time and size·How to make a log file have only one log level·How to output logs under specified packages to specified log files

simple use

If complex log configuration is not required, simply set the log print level, and the print mode can be configured directly in application.yml.

By default, Spring Boot outputs info-level logs to the console, does not write to log files, and cannot be configured in complex ways.

Print to file

To export logs to a file, you can configure them as follows:

logging: #configuration output log file name, can take path # file: out.log #configuration log storage path, log file name: spring.log path: ./ log file: #Set log file size max-size: 10MB

Note:file and path cannot be configured at the same time. If path is configured at the same time, it will not take effect.

Print Level Control

Log print levels can be controlled by configuration in the following format:

logging.level.*= TRACE/DEBUG/INFO/...* It can be a package name or Logger name, as follows: logging: level: # root log output root: info #all classes in this package output com.example.log_demo.log1: warn at DEBUG level

logback Detailed configuration

Next, we explain how to configure log printing through a separate xml configuration file. Although springboot is meant to eliminate xml, some complications still require xml to be written. After using xml, remove the configuration in application.yml to avoid conflicts.

According to different logging systems, the configuration file name is organized according to the specified rules, and placed in the resources directory, which can be automatically loaded by spring boot:

·Logback:logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy·Log4j: log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml·Log4j2: log4j2-spring.xml, log4j2.xml·JDK (Java Util Logging): logging.properties

Configurable file name you want to customize: logge.config Specify configuration file name:

logging.config=classpath:logging-config.xml

Spring Boot officially recommends using a file name with-spring as your logging configuration (e.g. logback-spring.xml instead of logback.xml), a logging configuration file named logback-spring.xml, and spring boot can add some spring-boot-specific configuration items to it (mentioned below).

Composition of logback configuration file

The root node has five child nodes. Let's introduce them one by one.

node

The root node is a required node, which is used to specify the most basic log output level. There is only one level attribute, which is used to set the print level. The options are as follows: TRACE,DEBUG,INFO,WARN,ERROR,ALL,OFF.

The root node can contain zero or more elements, and appenders are added. As follows:

appender is also a child node, as will be explained later.

node

Set context name, default, can print context name by %contextName, generally do not use this attribute.

node

Used to define variables for ease of use. There are two attributes: name and value. Once a variable is defined, it can be used with ${}. As follows:

node

The appender is the node used to format the log output, which is the most important. There are two attributes:

·name: name of the appender·class: specify output policy, usually two kinds: console output, file output

Here are some examples of how to use this:

1. Output to console/Output logs by time

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n UTF-8 ${LOG_HOME}/timeFile/out.log ${LOG_HOME}/timeFile/info.% d{yyyy-MM-dd}.% i.log.gz 30 10MB %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n UTF-8

1. Set to output only a single level

Set,filter child node in appender, filter on default level, configure onMatch, onMismatch to output only a single level

INFO ACCEPT DENY

node

constitute

This node is used to set the log print level of a package or a specific class, as well as specify, and has the following three attributes:

·Name: Required. Used to specify a package or a specific class that is constrained by this loger·level: optional. Sets the print level. The default level is root.·addtivity: Optional. Whether to pass print information to the parent loger(i.e. root node). Default is true.

Examples of usage are as follows:

1. No level specified, no appender specified

2. Specify level, do not specify appender

3. Specify level, specify appender

By specifying appender, you can print the logs under the specified package to the specified file.

Multiple environment log output

By setting the file name to end with-spring, you can configure logger in different environments. Examples are as follows:

You can also switch the logger print settings above by configuring spring.profiles.active

Log printing can be complex, but here are just some examples of common logback configurations.

About how to achieve springboot through logback super detailed log configuration to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report