[英]Custom log level in spring boot using log4j2
Long story short: i can't get a custom level to work in spring boot.长话短说:我无法在 spring 引导中获得自定义级别。
using log4j is not a requirement;不要求使用 log4j; Internet basically pointed me in this direction.
互联网基本上将我指向了这个方向。
The issues i have:我遇到的问题:
I've added log4j2 starter to the depdencencies:我已将 log4j2 starter 添加到 depdencencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
...and excluded logback from spring-boot-starter-parent, as mentioned in all tutorials: ...并从 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>
..then i add a property logging.config: userstream/src/main/resources/log4j2.xml
pointing to where i keep the log4j2 config. ..然后我添加一个属性
logging.config: userstream/src/main/resources/log4j2.xml
指向我保存log4j2配置的位置。
i figured out i start simple, as what i want is just ONE extra loglevel called "BUSINESS", to log events that are not WARN but also not INFO:我发现我开始很简单,因为我想要的只是一个名为“BUSINESS”的额外日志级别,用于记录不是 WARN 也不是 INFO 的事件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<CustomLevels>
<CustomLevel name="BUSINESS" intLevel="850" />
</CustomLevels>
</Configuration>
... ( now HELP me please - what do i need to configure here to get it running? ) ...(现在请帮助我 -我需要在此处配置什么才能使其运行? )
private static final Logger LOGGER = LogManager.getLogger(UserStreamApplication.class);
....
LOGGER.log(Level.forName("BUSINESS", 850), "mystreamFunction called by user"+ uid);
Been running this up-and-down since hours , every config looks different and each tutorial has an subjectively different approach.从几个小时以来一直在上下运行,每个配置看起来都不同,每个教程都有主观上不同的方法。 Don't want to mess with fileappenders and logformats etc at all.
根本不想弄乱 fileappenders 和 logformats 等。 Just want to have an level between INFO and WARN that i can use.
只想在我可以使用的 INFO 和 WARN 之间有一个级别。 KISS!
吻!
Im asking you veteran log4j warriors and spring-boot superstars for help!我请各位老将 log4j 勇士和 spring-boot 超级巨星帮忙!
Item 1 looks correct except that if your logging configuration is going to be inside your application named log4j2.xml then you don't need to add logging.config to your configuration.项目 1 看起来是正确的,除非您的日志配置将位于名为 log4j2.xml 的应用程序中,那么您不需要将 logging.config 添加到您的配置中。 If you do it should be referenced using a classpath url as in logging.config: classpath:log4j2-myapp.xml.
如果这样做,则应使用类路径 url 引用它,如 logging.config 中所示:classpath:log4j2-myapp.xml。
For item 2 you didn't specify where you want it to log to.对于第 2 项,您没有指定要登录的位置。 The simplest configuration would be
最简单的配置是
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<CustomLevels>
<CustomLevel name="BUSINESS" intLevel="850" />
</CustomLevels>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
The logging as you have coded it looks correct.您编码的日志记录看起来是正确的。 That said, "Business" is an odd name for a level as levels typically are for the relative importance of the event.
也就是说,“业务”对于一个级别来说是一个奇怪的名称,因为级别通常是针对事件的相对重要性。 Business sounds more like a Marker that could possibly apply across multiple levels.
业务听起来更像是一个可能适用于多个级别的标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.