“Log4j 的内置日志记录”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1iJ411H74S?p=13”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
− | https://www.bilibili.com/video/BV1iJ411H74S?p=13 | + | https://www.bilibili.com/video/BV1iJ411H74S?p=13<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: End of parsing for "console". | ||
+ | log4j: Parsed "console" options. | ||
+ | log4j: Finished configuring. | ||
+ | INFO - hello log4j | ||
+ | FATAL - fatal | ||
+ | ERROR - error | ||
+ | WARN - warn | ||
+ | INFO - info | ||
+ | DEBUG - debug | ||
+ | DEBUG - trace | ||
+ | </syntaxhighlight> |
2023年2月24日 (五) 07:42的最新版本
https://www.bilibili.com/video/BV1iJ411H74S?p=13
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: End of parsing for "console".
log4j: Parsed "console" options.
log4j: Finished configuring.
INFO - hello log4j
FATAL - fatal
ERROR - error
WARN - warn
INFO - info
DEBUG - debug
DEBUG - trace