【发布时间】:2020-08-02 02:03:46
【问题描述】:
长话短说:我无法在 Spring Boot 中获得自定义关卡。
使用 log4j 不是必需的;互联网基本上为我指明了这个方向。
我遇到的问题:
1 - 将 log4j2 添加到项目中
我已将 log4j2 启动器添加到依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
...并从 spring-boot-starter-parent 中排除 logback,如所有教程中所述:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
..然后我添加一个属性 logging.config: userstream/src/main/resources/log4j2.xml 指向我保存 log4j2 配置的位置。
2 配置 log4j2(我不知道 - 很复杂!)
我发现我从简单开始,因为我想要的只是一个名为“BUSINESS”的额外日志级别,用于记录不是 WARN 也不是 INFO 的事件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<CustomLevels>
<CustomLevel name="BUSINESS" intLevel="850" />
</CustomLevels>
</Configuration>
...(现在请帮助我 - 我需要在此处配置什么才能使其运行?)
3 调用它(应该像这样工作,但现在不行!)
private static final Logger LOGGER = LogManager.getLogger(UserStreamApplication.class);
....
LOGGER.log(Level.forName("BUSINESS", 850), "mystreamFunction called by user"+ uid);
几个小时以来一直在上下运行这个程序,每个配置看起来都不同,每个教程都有主观上不同的方法。根本不想弄乱 fileappenders 和 logformats 等。只想在我可以使用的 INFO 和 WARN 之间有一个级别。亲!
我正在向你们资深的 log4j 战士和 spring-boot 超级明星寻求帮助!
【问题讨论】:
-
PS:我读到有人可以使用 MARKERS 来实现类似的可用性(“标记”日志)。有什么想法/经验吗?
标签: java spring-boot logging log4j log4j2