1

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!

1
  • PS: i read that one can use MARKERS to achieve a similiar usability ('tagging' logs).. Any thoughts/experience? Commented Apr 19, 2020 at 18:40

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

hey @rgoers, thank you already alot. i now have the Level working, but as it comes its only visible setting root level BUSINESS which (for reasons i can only speculate about) bring TRACE and DEBUG logs with it. It shoulnd't do so, as 850 should sit inbetween WARN and INFO. Any idea?
regarding the markers i am looking at this currently, as it might fit the requirement better. thank you for the hint!
Looking at StandardLevel, a value of 850 should cause everything to be logged. TRACE is 600, DEBUG is 500, INFO is 400, WARN is 300, ERROR is 200. FATAL is 100 and OFF is 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.