“JUL 配置文件入门”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1iJ411H74S?p=8 === 默认配置文件调用 === Logger.getLogger(String name) -> Logger.demandLogger(String name, String resour…”的新页面) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的1个中间版本) | |||
第23行: | 第23行: | ||
=== 加载自定义配置文件 === | === 加载自定义配置文件 === | ||
− | src/main/resources/logging.properties<syntaxhighlight lang="properties"> | + | |
+ | ==== src/main/resources/logging.properties ==== | ||
+ | <syntaxhighlight lang="properties"> | ||
handlers= java.util.logging.ConsoleHandler | handlers= java.util.logging.ConsoleHandler | ||
第40行: | 第42行: | ||
java.util.logging.ConsoleHandler.level = ALL | java.util.logging.ConsoleHandler.level = ALL | ||
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter | java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter | ||
− | </syntaxhighlight>JULTest.java<syntaxhighlight lang="java"> | + | </syntaxhighlight> |
+ | |||
+ | ==== JULTest.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
//加载自定义配置文件 | //加载自定义配置文件 | ||
@Test | @Test | ||
− | public void | + | public void testLogProperties() throws IOException { |
// 读取配置文件,通过类加载器 | // 读取配置文件,通过类加载器 | ||
InputStream resourceAsStream = JULTest.class.getClassLoader().getResourceAsStream("logging.properties"); | InputStream resourceAsStream = JULTest.class.getClassLoader().getResourceAsStream("logging.properties"); | ||
第62行: | 第67行: | ||
logger.finest("finest"); | logger.finest("finest"); | ||
} | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | 二月 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 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === 添加 Handler === | ||
+ | logging.properties<syntaxhighlight lang="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 | ||
+ | </syntaxhighlight>这样将在项目根目录下增加日志文件的输出 | ||
+ | |||
+ | 关键点是在 handlers 后面追加了下面的 FileHandler |
2023年2月23日 (四) 03:28的最新版本
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
加载自定义配置文件
src/main/resources/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 testLogProperties() 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