spring-cloud / spring-cloud-bus

Spring Cloud event bus
http://cloud.spring.io/spring-cloud-bus/
Apache License 2.0
409 stars 242 forks source link

When spring cloud bus is enable, logback can not write the log file that defined in appender #197

Closed SingKS8 closed 5 years ago

SingKS8 commented 5 years ago

This is the version that I have used. spring boot : 2.0.9.RELEASE spring cloud: Finchley.SR2

In my case, I have a filter that logging the request body and response body, like this:

public class ApiLogFilter extends OncePerRequestFilter {

     private static final Logger apiLogger = LoggerFactory.getLogger("apiLogger");

     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
         //.........
         apiLogger.info("log request body");//...
         filterChain.doFilter(requestWrapper, responseWrapper);
         apiLogger.info("log response body");//......
     }

}

And logback configuration like this:

    <appender name="api" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/api.log</fileNamePattern>
            <maxHistory>${maxHistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger -- %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="apiLogger" level="INFO" additivity="false">
        <appender-ref ref="Console" />
        <appender-ref ref="api" />
    </logger>

When my project was only a spring-boot-starter-web Or spring cloud bus was disable , apiLogger cloud write the log info in the log file. But if the project integrated spring cloud and spring cloud bus was enable, apiLogger cloud not the log info in the log file. And I have updated spring boot and spring cloud version,

spring boot : 2.1.5.RELEASE spring cloud: Greenwich.SR1

writing log file is still not working. After debugged, I found when spring cloud bus was enable, writing log file is not working in all http threads, wherever Filter or Controller, for example controller like this:

@RestController
public class TestApiController {
    private static final Logger apiLogger = LoggerFactory.getLogger("apiLogger");
    @GetMapping("/testApi")
    public String testApi() {
        apiLogger.info("some logs in controller");
        return "success ";
    }
}

apiLogger can not the log info in the log file.

Does it not recommend using the log file when use the spring cloud bus ??

spencergibb commented 5 years ago

My guess is it is not specific to spring-cloud-bus, but probably spring-cloud-commons. Can you try with that instead and see if the same problem persists?

SingKS8 commented 5 years ago

Thanks for helping. I reviewed the code, and found I missed a point, because someone designed logback.xml has some code like this:

    <if condition='isDefined("catalina.base")'>
        <then>
            <property name="log_dir" value="${catalina.base}/logs" />
        </then>
        <else>
            <if condition='isDefined("appPath")'>
                <then>
                    <property name="log_dir" value="${appPath}/logs" />
                </then>
                <else>
                    <property name="log_dir" value="./logs" />
                </else>
            </if>
        </else>
    </if>

Then I removed this condition and just set a property

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

It worked, apiLogger cloud the log info in the log file. Maybe spring-cloud-bus or spring-cloud-commons somewhere has a conflict of logback condition.

spencergibb commented 5 years ago

We don't do anything specifically with log back

SingKS8 commented 5 years ago

Yes spring cloud do nothing specifically with logback. And I found what has happened in my case. Thanks for helping.