javaspringlogginglogback

logback not working the same throughout the project


Full project on Github (check no-logs branch): https://github.com/BenVella/backend-java/tree/no-logs

Problem: Running in docker-compose for rabbitmq and postgres. I'm not getting logs showing up for whatever reason with one exception.

I imported BezKoder's auth system and he has one class with does a logger.error which does show:

@Component
public class AuthEntryPointJwt implements AuthenticationEntryPoint {

  private static final Logger logger = LoggerFactory.getLogger(AuthEntryPointJwt.class);

  @Override
  public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
      throws IOException, ServletException {
    logger.error("Unauthorized error: {}", authException.getMessage());

^ AuthEntryPointJwt in com.backend.security.jwt

I'm seeing the following entry when I trigger this error on purpose:

2025-03-24 19:49:34 2025-03-24T18:49:34.406Z ERROR 1 --- [order-taking-api] [nio-8080-exec-5] c.b.security.jwt.AuthEntryPointJwt       : Unauthorized error: Full authentication is required to access this resource

If it's of any use, this is under the com.backend.security package whereas my target logic is meant to fall under the com.backend.order package.

I setup my OrderController in much the same way, as far as I can see:

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    private final static Logger log = LoggerFactory.getLogger(OrderController.class);

    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
        log.info("OrderController initialized");
    }

    @PostMapping
    public ResponseEntity<String> createOrder(@RequestBody Order order) {
        System.console().printf("Received order: %s%n", order);
        log.info("Received order {}", order);
        log.error("An error occurred while processing the order");

^ OrderController in com.backend.order.controllers

Yes lots of pointless logs but I'm seeing none of them and was wondering if it mattered whatever I changed.

The Jwt logs worked before I even did any sort of configuration straight out of the gate.

But I did include a logback-spring.xml, updated application.properties to log down to TRACE and define a logfile and generally followed what's mentioned here:

https://www.baeldung.com/spring-boot-logging

But alas, with all the variants I tried, the security is able to log just fine, but the other order package I built never reports anything. I'm still able to access the necessary endpoints without issue for consumption.

Setup:

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

    <property name="LOGS" value="./logs" />

    <appender name="Console"
        class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/spring-boot-logger.log</file>
        <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <!-- LOG everything at INFO level -->
    <root level="info">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    <logger name="com.backend" level="trace" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>

</configuration>

^ logback-spring.xml (Took from baeldun and changed the last entry from com.baeldung to com.backend although I have no clue if that matters)

# Logging
logging.level.root=TRACE
logging.file.name=logs/app.log
logging.file.path=logs

^ application.properties


Solution

  • I just ran your code in github codespaces and the logback works like charme there. So do you get the logs when executing locally but not when running in docker?

    enter image description here