On java log management logback

1. Problem description

About the log management logback in java, I wrote an article about logback last year. In this project, I optimized it and recorded it. I hope it can help friends in need.

2. Solution

In fact, I encountered a problem this time. Generally, I will create a user on linux alone, which is the same as the online development environment. In this way, if there is a relative directory in the script, it can be consistent. However, this time, because there is a problem in creating other users in the online environment, I can only use the root user to operate, resulting in the inconsistency between the directory defined by logback and the online development, The online directory is root / * * *. The development environment does not have permission to access this directory (ordinary users used by the development environment). Therefore, the logback directory needs to be configured, but it cannot be obtained from the application. It is solved in another way and recorded.

2.1 configuration

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <springProperty scope="context" name="LOG_HOME" source="path.log"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/laowangtest.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <MaxFileSize>1kb</MaxFileSize>
<!--                <MaxFileSize>5MB</MaxFileSize>- -->
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Log output level -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

Brief description

1. Directory configuration

 <springProperty scope="context" name="LOG_HOME" source="path.log"/>

Where application The configuration in YML is as follows:

path:
  log: e:/laowang/test

There are other configuration files in the actual project: application-dev.yml and application-test.yml yml,application-prod.yml,path.log is configured in each file with different values, rather than in the public configuration application YML, so that different log directories can be obtained according to the packaging rules.

In addition, why can't it pass square type Obtain take value , network upper or person officer square to of answer case yes , stay Allow implement in , l o g b a c k before And a p p l i c a t i o n . y m l plus load Yes , place with through too The answer given online or officially is that in the container, logback and application YML is loaded, so it passes The answer given online or officially is that in the container, logback and application YML is loaded, so you can't get the value through {}; At the same time, for the sake of insurance, logback Change the XML file name to logback - * * * XML, for example: logback spring xml.

2. Briefly introduce the current configuration

Two Appender s are configured, one to print to the console and the other to the file. Previously, the file was printed according to info, warn and errro, and the file was divided according to the log level. Now I don't feel it necessary to print one inside. I also scroll the log according to the previous method, first by day, and then by size

   <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/laowangtest.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <MaxFileSize>1kb</MaxFileSize>
<!--                <MaxFileSize>5MB</MaxFileSize>- -->
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

Several parameters:

(1) FileNamePattern, file directory;

(2) MaxHistory: log retention days;

(3) MaxFileSize: when to start rolling (adding) logs, remember that it is 5MB, and there are B behind it. Don't miss it.

2.2 effect

In order to show the effect, roll the size into 1kb and get a test method for verification

(1) Test method

@Api(value = "test")
@RestController
public class TestController {
    Logger logger = LoggerFactory.getLogger(TestController. class);

    @RequestMapping(value ="/logTest")
    @ResponseBody
    public  String logTest() {
        logger.info("Lao Wang is handsome, Lao Wang is handsome, Lao Wang is handsome");
        return "Lao Wang is a handsome man!";
    }
}

(2) Execution effect

browser:

Generate file:

Document content:

More information is available on the official account: "software old king". He is concerned about not losing his way. Software old Wang and his IT friends share some of their technical insights and life stories.

Keywords: Java Spring IDE Logback

Added by dhe on Sun, 19 Dec 2021 21:33:25 +0200