synffryd
synffryd

Reputation: 191

Logs in Spring Boot not showing in terminal

I have a project in Spring Boot where I am adding logs with the @Slf4j annotation, but I can't get them to show up in the terminal when starting the project.

This is the log: log.info("totalVacations: " + totalVacations);

When I run the command mvn spring-boot: run, in terminal I can't see the message.

Upvotes: 5

Views: 24322

Answers (2)

Richard
Richard

Reputation: 722

Very likely you are missing the configuration of the appender, if you are also using LOG4J and you don't already have it, you can create a file named: log4j2.xml within the application's class path, and then create some basic configuration like:

<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.baeldung*" at TRACE level -->
        <Logger name="com.baeldung" level="trace"></Logger>
    </Loggers>

</Configuration> 

The key here is to set an appender to the console so that the logs are pushed there, and then at the logger section establish which level are you trying to get at the console appender, according to your example that should be INFO

If you are using just plain SLF4J with Logback go to src/main/resources/application.properties and add the following line:

logging.level.com=INFO

The ".com" is referencing the package to where this will apply, in this case is everything inside src/main/java/com.

and at the logback-spring.xml try this basic config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="your.name" level="INFO" additivity="false">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </logger>
 </configuration>

You can find a very nice tutorial with more info here:

Upvotes: 7

Vinodh Thimmisetty
Vinodh Thimmisetty

Reputation: 80

Check whether @Slf4J import is from Lombok or some other library. I'm sure lombok @Slf4J default provides lowercase log and not LOG.

Upvotes: 1

Related Questions