“JUL”的版本间的差异
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
− | + | [[JUL 入门]] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
https://www.bilibili.com/video/BV1iJ411H74S?p=5 | https://www.bilibili.com/video/BV1iJ411H74S?p=5 |
2023年2月23日 (四) 01:50的版本
https://www.bilibili.com/video/BV1iJ411H74S?p=5
日志级别
Integer.MAX_VALUE | OFF | 关闭日志 |
1000 | SEVERE | 十分严重 |
900 | WARNING | 警告 |
800 | INFO | 信息 |
700 | CONFIG | 配置信息 |
500 | FINE | 跟踪信息 |
400 | FINER | 跟踪信息 |
300 | FINEST | 跟踪信息 |
Integer.MIN_VALUE | ALL | 记录所有日志 |
可以为每个 logger 对象设置一个日志级别,每个级别都对应一个 int 值,大于等于这个值的日志就会被记录,小于这个值的日志就不会被记录
比如在一个类中,JULTest 中设置 logger 对象的日志级别是 OFF:
public void testQuick() {
//1、创建日志记录器对象
Logger logger = Logger.getLogger("io.github.jihch.JULTest");
logger.setLevel(Level.OFF);
//2、日志记录输出
logger.info("hello jul");
// 通用方法进行日志记录
logger.log(Level.INFO, "info msg");
// 通过占位符 方式输出变量值
String name = "jack";
Integer age = 18;
logger.log(Level.INFO, "用户信息:{0},{1}", new Object[]{name, age});
}
运行程序发现控制台没有日志记录输出
测试默认日志级别
// 日志级别
@Test
public void testLogLevel() {
//1、获取日志记录器对象
Logger logger = Logger.getLogger("io.github.jihch.JULTest");
//2、日志记录输出
logger.severe("severe");
logger.warning("warning");
logger.info("info");
logger.config("config");
logger.fine("fine");
logger.finer("finer");
logger.finest("finest");
}
二月 21, 2023 8:40:42 下午 io.github.jihch.JULTest testLogLevel
严重: severe
二月 21, 2023 8:40:42 下午 io.github.jihch.JULTest testLogLevel
警告: warning
二月 21, 2023 8:40:42 下午 io.github.jihch.JULTest testLogLevel
信息: info
说明 JUL 默认的日志级别是 info
https://www.bilibili.com/video/BV1iJ411H74S?p=6
硬编码配置
尝试1
// 日志级别
@Test
public void testLogConfig() {
//1、获取日志记录器对象
Logger logger = Logger.getLogger("io.github.jihch.JULTest");
// 关闭系统默认配置
logger.setUseParentHandlers(false);
// 创建 ConsolHandler
ConsoleHandler consoleHandler = new ConsoleHandler();
// 创建简单格式
SimpleFormatter simpleFormatter = new SimpleFormatter();
// 进行关联
consoleHandler.setFormatter(simpleFormatter);
logger.addHandler(consoleHandler);
// 配置日志具体级别
logger.setLevel(Level.ALL);
consoleHandler.setLevel(Level.ALL);
//2、日志记录输出
logger.severe("severe");
logger.warning("warning");
logger.info("info");
logger.config("config");
logger.fine("fine");
logger.finer("finer");
logger.finest("finest");
}
二月 21, 2023 8:55:14 下午 io.github.jihch.JULTest testLogConfig
严重: severe
二月 21, 2023 8:55:14 下午 io.github.jihch.JULTest testLogConfig
警告: warning
二月 21, 2023 8:55:14 下午 io.github.jihch.JULTest testLogConfig
信息: info
二月 21, 2023 8:55:14 下午 io.github.jihch.JULTest testLogConfig
配置: config
二月 21, 2023 8:55:14 下午 io.github.jihch.JULTest testLogConfig
详细: fine
二月 21, 2023 8:55:14 下午 io.github.jihch.JULTest testLogConfig
较详细: finer
二月 21, 2023 8:55:14 下午 io.github.jihch.JULTest testLogConfig
非常详细: finest
尝试2
// 日志级别
@Test
public void testLogConfig() throws IOException {
//1、获取日志记录器对象
Logger logger = Logger.getLogger("io.github.jihch.JULTest");
// 关闭系统默认配置
logger.setUseParentHandlers(false);
// 创建 ConsolHandler 控制台输出
ConsoleHandler consoleHandler = new ConsoleHandler();
// 创建简单格式
SimpleFormatter simpleFormatter = new SimpleFormatter();
// 进行关联
consoleHandler.setFormatter(simpleFormatter);
logger.addHandler(consoleHandler);
// 配置日志具体级别
logger.setLevel(Level.ALL);
consoleHandler.setLevel(Level.ALL);
// 场景 FileHandler 文件输出
FileHandler fileHandler = new FileHandler("E:\\record\\2023\\2\\21\\jul.log");
// 进行关联
fileHandler.setFormatter(simpleFormatter);
logger.addHandler(fileHandler);
//2、日志记录输出
logger.severe("severe");
logger.warning("warning");
logger.info("info");
logger.config("config");
logger.fine("fine");
logger.finer("finer");
logger.finest("finest");
}
console:
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
严重: severe
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
警告: warning
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
信息: info
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
配置: config
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
详细: fine
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
较详细: finer
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
非常详细: finest
"E:\record\2023\2\21\jul.log"
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
严重: severe
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
警告: warning
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
信息: info
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
配置: config
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
详细: fine
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
较详细: finer
二月 21, 2023 9:06:53 下午 io.github.jihch.JULTest testLogConfig
非常详细: finest
https://www.bilibili.com/video/BV1iJ411H74S?p=7
Logger 对象父子关系
// Logger 对象父子关系
@Test
public void testLogParent() {
Logger logger1 = Logger.getLogger("io.github.jihch");
Logger logger2 = Logger.getLogger("io.github");
// 测试
System.out.println(logger1.getParent() == logger2);
// 所有日志记录器的顶级父元素 LogManager$RootLogger,name ""
System.out.printf("logger2.getParent():%s, logger2.getParent().getName():%s\n", logger2.getParent(),
logger2.getParent().getName());
// 关闭默认配置
logger2.setUseParentHandlers(false);
// 创建 ConsolHandler 控制台输出
ConsoleHandler consoleHandler = new ConsoleHandler();
// 创建简单格式
SimpleFormatter simpleFormatter = new SimpleFormatter();
// 进行关联
consoleHandler.setFormatter(simpleFormatter);
logger2.addHandler(consoleHandler);
// 配置日志具体级别
logger2.setLevel(Level.ALL);
consoleHandler.setLevel(Level.ALL);
logger1.severe("severe");
logger1.warning("warning");
logger1.info("info");
logger1.config("config");
logger1.fine("fine");
logger1.finer("finer");
logger1.finest("finest");
}
子包默认继承父包;
不写默认继承 RootLogger
RootLogger 有默认的 Handler 和 Formatter
JUL 配置文件入门
https://www.bilibili.com/video/BV1iJ411H74S?p=8
默认配置文件调用
Logger.getLogger(String name) ->
Logger.demandLogger(String name, String resourceBundleName, Class<?> caller) ->
LogManager.getLogManager() ->
manager.ensureLogManagerInitialized() ->
owner.readPrimordialConfiguration()->
readConfiguration()->
f = new File(f, "logging.properties")
I:\Java\jdk1.8.0_101\jre\lib\logging.properties
加载自定义配置文件
logging.properties
handlers= java.util.logging.ConsoleHandler
.level= ALL
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
JULTest.java
//加载自定义配置文件
@Test
public void test01() throws IOException {
// 读取配置文件,通过类加载器
InputStream resourceAsStream = JULTest.class.getClassLoader().getResourceAsStream("logging.properties");
// 创建 LogManager
LogManager logManager = LogManager.getLogManager();
// 通过 LogManager 加载配置文件
logManager.readConfiguration(resourceAsStream);
// 创建日志记录器
Logger logger = Logger.getLogger("io.github.jihch");
logger.severe("severe");
logger.warning("warning");
logger.info("info");
logger.config("config");
logger.fine("fine");
logger.finer("finer");
logger.finest("finest");
}
二月 23, 2023 8:58:36 上午 io.github.jihch.JULTest test01
严重: severe
二月 23, 2023 8:58:36 上午 io.github.jihch.JULTest test01
警告: warning
二月 23, 2023 8:58:36 上午 io.github.jihch.JULTest test01
信息: info
二月 23, 2023 8:58:36 上午 io.github.jihch.JULTest test01
配置: config
二月 23, 2023 8:58:36 上午 io.github.jihch.JULTest test01
详细: fine
二月 23, 2023 8:58:36 上午 io.github.jihch.JULTest test01
较详细: finer
二月 23, 2023 8:58:36 上午 io.github.jihch.JULTest test01
非常详细: finest
添加 Handler
logging.properties
handlers= java.util.logging.ConsoleHandler,java.util.logging.FileHandler
.level= ALL
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
这样将在项目根目录下增加日志文件的输出
关键点是在 handlers 后面追加了下面的 FileHandler
JUL 配置文件详解
https://www.bilibili.com/video/BV1iJ411H74S?p=9
logging.properties
# RootLogger 顶级父元素指定的默认处理器为:ConsoleHandler
handlers= java.util.logging.FileHandler
# RootLogger 顶级父元素指定的默认日志级别为:ALL
.level= ALL
# 向日志文件输出的 Handler 对象
# 指定日志文件路径 java%u.log
java.util.logging.FileHandler.pattern = java%u.log
# 指定日志文件内容大小为 50000 条
java.util.logging.FileHandler.limit = 50000
# 指定日志文件数量
java.util.logging.FileHandler.count = 1
# 指定 Handler 对象日志消息格式对象
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# 向控制台输出的 Handler 对象
# 指定 Handler 对象的日志级别
java.util.logging.ConsoleHandler.level = ALL
# 指定 Handler 对象的日志消息格式对象
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# 指定 Handler 对象的字符集
java.util.logging.ConsoleHandler.encoding = UTF-8
# 指定日志消息格式
java.util.logging.SimpleFormatter.format = %4$s: %5$s [%1$tc]%n
更换向日志文件输出的 Handler 对象对应的日志消息格式对象
# 指定 Handler 对象日志消息格式对象
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java0.log
严重: severe [星期四 二月 23 09:30:36 CST 2023]
警告: warning [星期四 二月 23 09:30:37 CST 2023]
信息: info [星期四 二月 23 09:30:37 CST 2023]
配置: config [星期四 二月 23 09:30:37 CST 2023]
详细: fine [星期四 二月 23 09:30:37 CST 2023]
较详细: finer [星期四 二月 23 09:30:37 CST 2023]
非常详细: finest [星期四 二月 23 09:30:37 CST 2023]
指定以追加方式添加日志内容
java.util.logging.FileHandler.append = true
使用自定义 Logger
logging.properties
# 自定义 Logger 使用
io.github.jihch.handlers = java.util.logging.ConsoleHandler
io.github.jihch.level = CONFIG
# 关闭默认配置
io.github.jihch.useParentHandler = false