Java实现日志输出到文件中
public void consoleLog() throws IOException {
File file = new File("E:\\es.log");
FileWriter fileWriter = null;
if (!file.exists()) {
file.createNewFile();
}
try {
fileWriter = new FileWriter(file, true);
for (int i = 0; i < 100; i++) {
fileWriter.write("log: " + i + "\r\n");
}
}catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter != null) {
fileWriter.close();
}
}
}
配置文件logback-spring.xml
xmlns="http://ch.qos.logback/xml/ns/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback">
value="%d{yyyy-MM-dd HH:mm:ss.SS,America/Los_Angeles} %-5level %logger{36} %msg%n"/>
${LOG_PATTERN}
class="ch.qos.logback.core.rolling.RollingFileAppender">
${LOG_FILE_CONSOLE}
${LOG_PATTERN}
${LOG_FILE_CONSOLE}.%d{yyyy-MM-dd}.%i
5MB
5
25MB
class="ch.qos.logback.core.rolling.RollingFileAppender">
${LOG_FILE_SYS}
${LOG_PATTERN}
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
${LOG_FILE_SYS}.%d{yyyy-MM-dd}.%i
5MB
5
25MB
class="ch.qos.logback.core.rolling.RollingFileAppender">
${LOG_FILE_ERROR}
${LOG_PATTERN}
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
${LOG_FILE_ERROR}.%d{yyyy-MM-dd}.%i
5MB
5
25MB
class="ch.qos.logback.core.rolling.RollingFileAppender">
${LOG_FILE_EXCEPTION}
${LOG_PATTERN}
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
${LOG_FILE_EXCEPTION}.%d{yyyy-MM-dd}.%i
50MB
10
500MB
yml文件配置日志级别
logging:
level:
root: info
com.newegg.im.keyword.dao: debug