“Log4j 的 FileAppender 配置”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
 
第271行: 第271行:
  
 
==== 注意 ====
 
==== 注意 ====
datePattern 中不能出现操作系统不允许出现在文件名中的非法字符
+
datePattern 中不能出现操作系统不允许出现在文件名中的非法字符,比如 Windows 中不能出现平时时间格式分割时分秒的冒号“:”

2023年2月25日 (六) 11:10的最新版本

https://www.bilibili.com/video/BV1iJ411H74S?p=15

FileAppender

log4j.properties

# 指定 RootLogger 顶级父元素默认配置信息
# 指定日志级别=trace,使用的 appender 为 console
log4j.rootLogger = trace,console,file
# 指定控制台日志输出的 appender
log4j.appender.console = org.apache.log4j.ConsoleAppender
# 指定消息格式 layout
log4j.appender.console.layout = org.apache.log4j.PatternLayout
# 指定消息格式的内容
log4j.appender.console.layout.ConversionPattern = [%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n




# 日志文件输出的 appender 对象
log4j.appender.file = org.apache.log4j.FileAppender
# 指定消息格式 layout
log4j.appender.file.layout = org.apache.log4j.PatternLayout
# 指定消息格式的内容
log4j.appender.file.layout.ConversionPattern = [%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n
# 指定日志文件的保存路径
log4j.appender.file.file = /logs/log4j.log
# 指定日志文件的字符集
log4j.appender.file.encoding = UTF-8

log4j.rootLogger = trace,console,file 为 rootLogger 追加了一个 name 为 file 的 appender

log4j.appender.file = org.apache.log4j.FileAppender 声明了一个 name 为 file 的 FileAppender 类型的 appender

log4j.appender.file.file = /logs/log4j.log 通过 OGNL 为 name 为 file 的 appender 指定了属性 file 的值为 “/logs/log4j.log”

log4j.appender.file.encoding = UTF-8 通过 OGNL 为 name 为 file 的 FileAppender 设置继承自父类 WriterAppender 的字符集编码属性 encoding 的值让日志文件不乱码

另外:原来在 Windows 上,log4j.appender.file.file = /logs/log4j.log 中的 “/logs/log4j.log”就指到了程序所在的磁盘根目录“E:\logs\log4j.log”

控制台:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@18b4aac2.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@18b4aac2 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@18b4aac2.
log4j: Using URL [file:/E:/record/2023/2/19/log4j-demo/target/classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/E:/record/2023/2/19/log4j-demo/target/classes/log4j.properties
log4j: Parsing for [root] with value=[trace,console,file].
log4j: Level token is [trace].
log4j: Category root set to TRACE
log4j: Parsing appender named "console".
log4j: Parsing layout options for "console".
log4j: Setting property [conversionPattern] to [[%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n].
log4j: End of parsing for "console".
log4j: Parsed "console" options.
log4j: Parsing appender named "file".
log4j: Parsing layout options for "file".
log4j: Setting property [conversionPattern] to [[%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n].
log4j: End of parsing for "file".
log4j: Setting property [encoding] to [UTF-8].
log4j: Setting property [file] to [/logs/log4j.log].
log4j: setFile called: /logs/log4j.log, true
log4j: setFile ended
log4j: Parsed "file" options.
log4j: Finished configuring.
[INFO      ]0 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:24) 2023-02-24 17:46:59.040 hello log4j
[FATAL     ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:27) 2023-02-24 17:46:59.042 fatal
[ERROR     ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:29) 2023-02-24 17:46:59.042 error
[WARN      ]3 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:30) 2023-02-24 17:46:59.043 warn
[INFO      ]3 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:31) 2023-02-24 17:46:59.043 info
[DEBUG     ]3 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:32) 2023-02-24 17:46:59.043 debug
[DEBUG     ]3 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:34) 2023-02-24 17:46:59.043 trace

Process finished with exit code 0

“E:\logs\log4j.log”:

[INFO      ]0 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:24) 2023-02-24 17:46:59.040 hello log4j
[FATAL     ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:27) 2023-02-24 17:46:59.042 fatal
[ERROR     ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:29) 2023-02-24 17:46:59.042 error
[WARN      ]3 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:30) 2023-02-24 17:46:59.043 warn
[INFO      ]3 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:31) 2023-02-24 17:46:59.043 info
[DEBUG     ]3 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:32) 2023-02-24 17:46:59.043 debug
[DEBUG     ]3 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:34) 2023-02-24 17:46:59.043 trace




RollingFileAppender

按照文件大小进行拆分,超过一定的大小就会拆分成多个文件

log4j.properties

# 指定 RootLogger 顶级父元素默认配置信息
# 指定日志级别=trace,使用的 appender 为 console
log4j.rootLogger = trace,rollingFile
# 指定控制台日志输出的 appender
log4j.appender.console = org.apache.log4j.ConsoleAppender
# 指定消息格式 layout
log4j.appender.console.layout = org.apache.log4j.PatternLayout
# 指定消息格式的内容
log4j.appender.console.layout.conversionPattern = [%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n



# 按照文件大小拆分的 appender 对象
# 日志文件输出的 appender 对象
log4j.appender.rollingFile = org.apache.log4j.RollingFileAppender
# 指定消息格式 layout
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
# 指定消息格式的内容
log4j.appender.rollingFile.layout.conversionPattern = [%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n
# 指定日志文件保存路径
log4j.appender.rollingFile.file = /logs/log4j.log
# 指定日志文件的字符集
log4j.appender.rollingFile.encoding = UTF-8
# 指定日志文件内容的大小
log4j.appender.rollingFile.maxFileSize = 1MB
# 指定日志文件的数量
log4j.appender.rollingFile.maxBackupIndex = 10

Log4jTest.java

import org.apache.log4j.Logger;
import org.apache.log4j.helpers.LogLog;
import org.junit.Test;

public class Log4jTest {

    // 快速入门
    @Test
    public void testQuick() {

        // 开启 log4j 内置日志记录
        LogLog.setInternalDebugging(true);

        // 初始化配置信息,在入门案例中暂不使用配置文件
//        BasicConfigurator.configure();

        // 获取日志记录器对象
        Logger logger = Logger.getLogger(Log4jTest.class);

        // 日志记录输出
        logger.info("hello log4j");

        for (int i = 0; i < 10000; i++) {
            // 日志级别
            logger.fatal("fatal"); // 严重错误,一般会造成系统崩溃并终止运行

            logger.error("error"); // 错误信息,不会影响系统运行
            logger.warn("warn");   // 警告信息,可能会发生问题
            logger.info("info");   // 运行信息,数据连接、网络连接、IO 操作等等
            logger.debug("debug"); // 调试信息,一般在开发中使用,记录程序变量参数传递信息等等

            logger.debug("trace"); // 追踪信息,记录程序所有的流程信息
        }

    }

}
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@18b4aac2.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@18b4aac2 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@18b4aac2.
log4j: Using URL [file:/E:/record/2023/2/19/log4j-demo/target/classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/E:/record/2023/2/19/log4j-demo/target/classes/log4j.properties
log4j: Parsing for [root] with value=[trace,rollingFile].
log4j: Level token is [trace].
log4j: Category root set to TRACE
log4j: Parsing appender named "rollingFile".
log4j: Parsing layout options for "rollingFile".
log4j: Setting property [conversionPattern] to [[%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n].
log4j: End of parsing for "rollingFile".
log4j: Setting property [encoding] to [UTF-8].
log4j: Setting property [file] to [/logs/log4j.log].
log4j: Setting property [maxFileSize] to [1MB].
log4j: Setting property [maxBackupIndex] to [10].
log4j: setFile called: /logs/log4j.log, true
log4j: setFile ended
log4j: Parsed "rollingFile" options.
log4j: Finished configuring.
log4j: rolling over count=6134499
log4j: maxBackupIndex=10
log4j: Renaming file \logs\log4j.log to \logs\log4j.log.1
log4j: setFile called: /logs/log4j.log, false
log4j: setFile ended
log4j: rolling over count=1048604
log4j: maxBackupIndex=10
log4j: Renaming file \logs\log4j.log.1 to \logs\log4j.log.2
log4j: Renaming file \logs\log4j.log to \logs\log4j.log.1
log4j: setFile called: /logs/log4j.log, false
log4j: setFile ended
log4j: rolling over count=1048590
log4j: maxBackupIndex=10
log4j: Renaming file \logs\log4j.log.2 to \logs\log4j.log.3
log4j: Renaming file \logs\log4j.log.1 to \logs\log4j.log.2
log4j: Renaming file \logs\log4j.log to \logs\log4j.log.1
log4j: setFile called: /logs/log4j.log, false
log4j: setFile ended
log4j: rolling over count=1048604
log4j: maxBackupIndex=10
log4j: Renaming file \logs\log4j.log.3 to \logs\log4j.log.4
log4j: Renaming file \logs\log4j.log.2 to \logs\log4j.log.3
log4j: Renaming file \logs\log4j.log.1 to \logs\log4j.log.2
log4j: Renaming file \logs\log4j.log to \logs\log4j.log.1
log4j: setFile called: /logs/log4j.log, false
log4j: setFile ended
log4j: rolling over count=1048637
log4j: maxBackupIndex=10
log4j: Renaming file \logs\log4j.log.4 to \logs\log4j.log.5
log4j: Renaming file \logs\log4j.log.3 to \logs\log4j.log.4
log4j: Renaming file \logs\log4j.log.2 to \logs\log4j.log.3
log4j: Renaming file \logs\log4j.log.1 to \logs\log4j.log.2
log4j: Renaming file \logs\log4j.log to \logs\log4j.log.1
log4j: setFile called: /logs/log4j.log, false
log4j: setFile ended
log4j: rolling over count=1048638
log4j: maxBackupIndex=10
log4j: Renaming file \logs\log4j.log.5 to \logs\log4j.log.6
log4j: Renaming file \logs\log4j.log.4 to \logs\log4j.log.5
log4j: Renaming file \logs\log4j.log.3 to \logs\log4j.log.4
log4j: Renaming file \logs\log4j.log.2 to \logs\log4j.log.3
log4j: Renaming file \logs\log4j.log.1 to \logs\log4j.log.2
log4j: Renaming file \logs\log4j.log to \logs\log4j.log.1
log4j: setFile called: /logs/log4j.log, false
log4j: setFile ended

Process finished with exit code 0



DailyRollingFileAppender

按照时间规则对文件进行拆分

log4j.properties

# 指定 RootLogger 顶级父元素默认配置信息
# 指定日志级别=trace,使用的 appender 为 console
log4j.rootLogger = trace,dailyFile
# 指定控制台日志输出的 appender
log4j.appender.console = org.apache.log4j.ConsoleAppender
# 指定消息格式 layout
log4j.appender.console.layout = org.apache.log4j.PatternLayout
# 指定消息格式的内容
log4j.appender.console.layout.conversionPattern = [%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n



# 按照时间规则拆分的 appender 对象
log4j.appender.dailyFile = org.apache.log4j.DailyRollingFileAppender
# 指定消息格式 layout
log4j.appender.dailyFile.layout = org.apache.log4j.PatternLayout
# 指定消息格式的内容
log4j.appender.dailyFile.layout.conversionPattern = [%-10p]%r %l %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n
# 指定日志文件的保存路径
log4j.appender.dailyFile.file = /logs/log4j.log
# 指定日志文件的字符集
log4j.appender.dailyFile.encoding = UTF-8
# 指定日期拆分规则
log4j.appender.dailyFile.datePattern = '.'yyyy-MM-dd HH-mm-ss

运行两次程序,将生成文件“log4j.log”和“log4j.log.2023-02-25 19-05-44”,因为配置中设置了以秒为单位分割文件

注意

datePattern 中不能出现操作系统不允许出现在文件名中的非法字符,比如 Windows 中不能出现平时时间格式分割时分秒的冒号“:”