java - logback not working the same throughout the project - Stack Overflow

Full project on Github (check no-logs branch): Problem:Running in docker-compose for rabbitmq and po

Full project on Github (check no-logs branch):

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:

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

Full project on Github (check no-logs branch): https://github/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/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

Share Improve this question asked Mar 24 at 19:14 LuponiusLuponius 992 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744232174a4564306.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信