Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logback Filter not working. #45022

Closed
SageJustus opened this issue Apr 7, 2025 · 3 comments
Closed

Logback Filter not working. #45022

SageJustus opened this issue Apr 7, 2025 · 3 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@SageJustus
Copy link

SageJustus commented Apr 7, 2025

demo: https://github.com/SageJustus/question/tree/main/demo7

dependency

id("org.springframework.boot") version "3.4.4"

JpaFilter

package com.sage.local

import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply

class JpaFilter : Filter<ILoggingEvent>() {
    override fun decide(event: ILoggingEvent): FilterReply {
        val result = event.message.startsWith("Start")
        println(event.message)
        println(result)
        return if (result) FilterReply.DENY else FilterReply.NEUTRAL
    }
}

logback.xml

<configuration scan="true" debug="true">
    <!-- use Spring default values -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${CONSOLE_LOG_THRESHOLD}</level>
        </filter>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>${CONSOLE_LOG_CHARSET}</charset>
        </encoder>
    </appender>

    <appender name="Jpa" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="com.sage.local.JpaFilter"/>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>${CONSOLE_LOG_CHARSET}</charset>
        </encoder>
    </appender>

    <logger name="com.sage.local.Demo7ApplicationKt" level="info">
        <appender-ref ref="Jpa"/>
    </logger>

    <root level="error">
        <appender-ref ref="CONSOLE"/>
    </root>

</configuration>

Start the springboot project. output:

Starting Demo7ApplicationKt using Java 21.0.5 with PID 48596 (C:\JavaProjects\question\demo7\build\classes\kotlin\main started by Administrator in C:\JavaProjects\question\demo7)
true
2025-04-07T19:55:52.014+08:00  INFO 48596 --- [           main] com.sage.local.Demo7ApplicationKt        : Starting Demo7ApplicationKt using Java 21.0.5 with PID 48596 (C:\JavaProjects\question\demo7\build\classes\kotlin\main started by Administrator in C:\JavaProjects\question\demo7)

The returned FilterReply.DENY value has no effect.

When I use logback alone, the filter works well

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 7, 2025
@mhalbritter
Copy link
Contributor

Does it work if you use logback-spring.xml instead of logback.xml?

@mhalbritter mhalbritter added the status: waiting-for-feedback We need additional information before we can continue label Apr 7, 2025
@SageJustus
Copy link
Author

Does it work if you use logback-spring.xml instead of logback.xml?

@mhalbritter use logback-spring.xml not working.

JpaFilter#decide method has been run, just return value not working.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 7, 2025
@mhalbritter
Copy link
Contributor

I can't imagine this setup to be working on Logback without Spring Boot either. Your problem is that loggers are additive: https://logback.qos.ch/manual/configuration.html#cumulative

By default, appenders are cumulative: a logger will log to the appenders attached to itself (if any) as well as all the appenders attached to its ancestors. Thus, attaching the same appender to multiple loggers will cause logging output to be duplicated.

Your filter is working, it's just that the CONSOLE logger is printing the line again.

Do it like this and it works:

    <logger name="com.sage.local.Demo7ApplicationKt" level="info" additivity="false">
        <appender-ref ref="Jpa"/>
    </logger>

@mhalbritter mhalbritter added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Apr 7, 2025
@mhalbritter mhalbritter closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants