简体   繁体   中英

Custom log level in spring boot using log4j2

Long story short: i can't get a custom level to work in spring boot.

using log4j is not a requirement; Internet basically pointed me in this direction.

The issues i have:

1 - Adding log4j2 to the project

I've added log4j2 starter to the 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:

<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.

2 Configuring log4j2 (as i have no idea - complicated!)

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:

<?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? )

3 Calling it (should work like this, but right now doesn't!)

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. Just want to have an level between INFO and WARN that i can use. KISS!

Im asking you veteran log4j warriors and spring-boot superstars for help!

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. If you do it should be referenced using a classpath url as in logging.config: classpath:log4j2-myapp.xml.

For item 2 you didn't specify where you want it to log to. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM