Solve the packaging problem of logback classic using testpipe

Time:2021-10-27

Logback classic uses the packaging of testpipe

Today, when using gradle to package, logback classic can’t be packaged.

Code for packaging logback classic:


// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

Because I used to use maven, compare the code of Maven:


<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
    <scope>test</scope>
</dependency>

It is indicated here that it is test, so it is speculated that test may lead to packaging failure, so remove test and use the general packaging method:


compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

Then the package succeeded.

Check the Internet. The scope is test, which means that the dependent project is only involved in testing related work, including the compilation and execution of test code. A typical example is JUnit.

Strangely, the logback classic package provides test compilation from the beginning to version 1.2.3. Why not the default compile?

Usage of logback and solutions to problems encountered

To generate log file information using logback.xml based on spring boot, you need to import dependencies in pom.xml first


<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.7</version>
</dependency>

Missing any one may lead to error reporting! Be sure to add the complete.

Then create the logback.xml file in Src / main / resources created by default, and add the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!-- Define the storage address of the log file -- >
<property name="LOG_HOME" value="logs" />
<!--  Console output -- >
<appender name="STDOUT">
<encoder>
<!-- Formatted output:% d represents the date,% thread represents the thread name,% - 5level: the level displays 5 characters from the left, width% MSG: log message,% n is the newline character -- >
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!--  Generate log files per day -- >
<appender name="FILE">
<rollingPolicy>
<!-- File name of log file output -- >
<FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log</FileNamePattern>
<!-- Log file retention days -- >
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<!-- Formatted output:% d represents the date,% thread represents the thread name,% - 5level: the level displays 5 characters from the left, width% MSG: log message,% n is the newline character -- >
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
<!-- The maximum size of the log file. If it coexists with the attribute of generating a file every day, once the program is not restarted, a new file will not be generated every day -- >
<!-- <triggeringPolicy>
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy> -->
</appender>

<!--  Log output level, specifying how to output the name -- > corresponding to the above appender
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>

You can use either absolute path or relative path to create the output file. Although it is recommended to use absolute path, you can also use relative path when the project needs it

The folder with relative path is created in the project root directory, and the log file is generated in this folder.

Level: used to set the print level, regardless of case: trace, debug, info, warn, error, all and off. It cannot be set to included or the synonym null. The default is debug.

If you want to limit the size of each log file, you can do this:

<rollingPolicy>
<!-- File name of log file output -- >  

<FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>    
<!-- Log file retention days -- >    
<MaxHistory>10</MaxHistory>    
<timeBasedFileNamingAndTriggeringPolicy>

<maxFileSize>10MB</maxFileSize>    
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>

Note the% I after the log file;

Modify < maxfilesize > 10MB < / maxfilesize > to limit the size of each log file.

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer.