“Log4j 的自定义 logger”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1iJ411H74S?p=17”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1iJ411H74S?p=17 | https://www.bilibili.com/video/BV1iJ411H74S?p=17 | ||
+ | |||
+ | === log4j.properties === | ||
+ | <syntaxhighlight lang="properties"> | ||
+ | # 指定 RootLogger 顶级父元素默认配置信息 | ||
+ | # 指定日志级别=trace,使用的 appender 为 console | ||
+ | log4j.rootLogger = trace,console | ||
+ | |||
+ | # 自定义 logger 对象设置 | ||
+ | log4j.logger.io.github.jihch = info,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 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Log4jTest.java === | ||
+ | <syntaxhighlight lang="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"); | ||
+ | |||
+ | // 日志级别 | ||
+ | logger.fatal("fatal"); // 严重错误,一般会造成系统崩溃并终止运行 | ||
+ | |||
+ | logger.error("error"); // 错误信息,不会影响系统运行 | ||
+ | logger.warn("warn"); // 警告信息,可能会发生问题 | ||
+ | logger.info("info"); // 运行信息,数据连接、网络连接、IO 操作等等 | ||
+ | logger.debug("debug"); // 调试信息,一般在开发中使用,记录程序变量参数传递信息等等 | ||
+ | |||
+ | logger.debug("trace"); // 追踪信息,记录程序所有的流程信息 | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === 控制台输出 === | ||
+ | <syntaxhighlight lang="console"> | ||
+ | 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]. | ||
+ | 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 for [io.github.jihch] with value=[info,file]. | ||
+ | log4j: Level token is [info]. | ||
+ | log4j: Category io.github.jihch set to INFO | ||
+ | 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 [file] to [/logs/log4j.log]. | ||
+ | log4j: Setting property [encoding] to [UTF-8]. | ||
+ | log4j: setFile called: /logs/log4j.log, true | ||
+ | log4j: setFile ended | ||
+ | log4j: Parsed "file" options. | ||
+ | log4j: Handling log4j.additivity.io.github.jihch=[null] | ||
+ | log4j: Finished configuring. | ||
+ | [INFO ]0 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:23) 2023-02-25 23:30:38.202 hello log4j | ||
+ | [FATAL ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:27) 2023-02-25 23:30:38.204 fatal | ||
+ | [ERROR ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:29) 2023-02-25 23:30:38.204 error | ||
+ | [WARN ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:30) 2023-02-25 23:30:38.204 warn | ||
+ | [INFO ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:31) 2023-02-25 23:30:38.204 info | ||
+ | |||
+ | Process finished with exit code 0 | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | === log4j.log === | ||
+ | <syntaxhighlight lang="console"> | ||
+ | [INFO ]0 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:23) 2023-02-25 23:30:38.202 hello log4j | ||
+ | [FATAL ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:27) 2023-02-25 23:30:38.204 fatal | ||
+ | [ERROR ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:29) 2023-02-25 23:30:38.204 error | ||
+ | [WARN ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:30) 2023-02-25 23:30:38.204 warn | ||
+ | [INFO ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:31) 2023-02-25 23:30:38.204 info | ||
+ | |||
+ | </syntaxhighlight> |
2023年2月25日 (六) 15:35的版本
https://www.bilibili.com/video/BV1iJ411H74S?p=17
log4j.properties
# 指定 RootLogger 顶级父元素默认配置信息
# 指定日志级别=trace,使用的 appender 为 console
log4j.rootLogger = trace,console
# 自定义 logger 对象设置
log4j.logger.io.github.jihch = info,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
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");
// 日志级别
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,console].
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 for [io.github.jihch] with value=[info,file].
log4j: Level token is [info].
log4j: Category io.github.jihch set to INFO
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 [file] to [/logs/log4j.log].
log4j: Setting property [encoding] to [UTF-8].
log4j: setFile called: /logs/log4j.log, true
log4j: setFile ended
log4j: Parsed "file" options.
log4j: Handling log4j.additivity.io.github.jihch=[null]
log4j: Finished configuring.
[INFO ]0 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:23) 2023-02-25 23:30:38.202 hello log4j
[FATAL ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:27) 2023-02-25 23:30:38.204 fatal
[ERROR ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:29) 2023-02-25 23:30:38.204 error
[WARN ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:30) 2023-02-25 23:30:38.204 warn
[INFO ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:31) 2023-02-25 23:30:38.204 info
Process finished with exit code 0
log4j.log
[INFO ]0 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:23) 2023-02-25 23:30:38.202 hello log4j
[FATAL ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:27) 2023-02-25 23:30:38.204 fatal
[ERROR ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:29) 2023-02-25 23:30:38.204 error
[WARN ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:30) 2023-02-25 23:30:38.204 warn
[INFO ]2 io.github.jihch.Log4jTest.testQuick(Log4jTest.java:31) 2023-02-25 23:30:38.204 info