java项目如何使用logback日志框架
解决方法:
1.maven依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
2.核心方法
/**
* 初始化日志配置
*/
private static void initLoggerConfig() {
File file = new File(getProjectPath() + "logback.xml");
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator joranConfigurator = new JoranConfigurator();
joranConfigurator.setContext(loggerContext);
loggerContext.reset();
try {
joranConfigurator.doConfigure(file);
} catch (Exception e) {
e.printStackTrace();
}
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
}
public static String getProjectPath() {
String path = App.class.getProtectionDomain().getCodeSource().getLocation().getPath();
if (path.endsWith(".jar")) {
path = path.substring(0, path.lastIndexOf("/"));
}
if (!path.endsWith("/")) {
path += "/";
}
return path;
}
3.在src\main\resources下新建logback.xml文件,内容:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<!-- 控制台输出 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%lo{10}] [%t] [%p] - %m%n</pattern>
</encoder>
<target>System.err</target>
</appender>
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="file—info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/info/info.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>3</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%d [%lo] [%t] [%p] - %m%n</pattern>
</encoder>
</appender>
<!-- 时间滚动输出 level为 WARN 日志 -->
<appender name="file—warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/warn/warn.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>7</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%d [%lo] [%t] [%p] - %m%n</pattern>
</encoder>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="file—error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/error/error.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%d [%lo] [%t] [%p] - %m%n</pattern>
</encoder>
</appender>
<logger name="io.netty" level="INFO"/>
<logger name="org.mongodb" level="INFO"/>
<root level="INFO">
<appender-ref ref="stdout"/>
<appender-ref ref="file—info"/>
<appender-ref ref="file—warn"/>
<appender-ref ref="file—error"/>
</root>
</configuration>
4.用法
@Slf4j
public class RedisService {
private static void getNoticeTaskToQueue() {
log.info("删除语音数据");
}
}
补充:idea不能使用log的方法:http://www.yayihouse.com/yayishuwu/chapter/1752