Recently want to use the project to generate multiple log files, on the Internet to check the relevant documents. Sorted out, took out has been shared by everyone:
Before I write, I think you already know how Logback is configured.
1, the configuration file add the following code:
<!--connection Statistics-->
<appender name= "Linkstat" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<fileNamePattern>log/linkstat/%d{yyyy-MM-dd}_%i.log</fileNamePattern>
<minIndex>0</minIndex>
<maxHistory>60</maxHistory>
<timebasedfilenamingandtriggeringpolicy
class= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" >
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<PATTERN>%D{YYYY-MM-DD}%d{hh:mm:ss}%msg%n</pattern>
</encoder>
</appender>
<!--a custom contactlogutil class in a Java project specifies that the Contactlogutil class log uses Linkstat generation-->
<logger name= "Com.targtime.dataTransfer.util.ContactLogUtil" >
<appender-ref ref= "Linkstat"/>
</logger>
2.Create class Contactloguti in Java
Package com.targtime.dataTransfer.util;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
public class Contactlogutil {
Private static final Logger log = Loggerfactory.getlogger (Contactlogutil.class);
public static void Contactlog (String msg) {
Log.info ("{}", msg);
}
}
3. Generate log
The log content is entered into the log file, and only the following code needs to be invoked:
Contactlogutil.contactlog ("More log file test");
If you want to generate another log file, you can then customize a Java class.
I hope to help you, if you have questions about the log configuration you can view: Logback configuration