How to use log4j2 with spring boot actuators

Viewed 802

I'm try to providing logs from log4j2 to Spring boot actuator. Unfortunately, i achieved only how to change log level for ROOT logger. Maybe someone know how to connect actuators with log4j2?

2 Answers

I found solution in another way. No matter how you config log4j2, because spring boot admin in /logging actuator read log file. Consequently, i must write path to this file.

It works only if you have logback, spring write to file in path

application.yml

...
logging:
  path: ./path/to/file
...

Finally i found solution for log4j2:

application.yml

...
endpoints:
  logfile:
    external-file: logs/custom.log
...

It's works only if you rid off logging.* properties

Found it in this one answer

This link might help.

A simple log4j2 configuration can look like the following:

<?xml version="1.0" encoding="UTF-8"?>

<Configuration>
<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout
            pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
    </Console>

    <RollingFile name="RollingFile"
        fileName="./logs/spring-boot-logger-log4j2.log"
        filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
        <PatternLayout>
            <pattern>%d %p %C{1.} [%t] %m%n</pattern>
        </PatternLayout>
        <Policies>
            <!-- rollover on startup, daily and when the file reaches 10 MegaBytes-->
            <OnStartupTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="10 MB" />
            <TimeBasedTriggeringPolicy />
        </Policies>
    </RollingFile>
</Appenders>

<Loggers>
    <!-- LOG everything at INFO level -->
    <Root level="info">
        <AppenderRef ref="Console" />
        <AppenderRef ref="RollingFile" />
    </Root>

    <!-- LOG "com.example*" at TRACE level -->
    <Logger name="com.example" level="trace"></Logger>
</Loggers>

</Configuration>

If you are using such a file only the Loggers defined in the -tag will be shown at the actuator endpoint. So if you want to see more Loggers, you have to add them in this file.

Related