3
votes

In my springboot application, I configure to write logs to AWS CloudWatch, but the application also generates a log file log on the server itself in the folder /var/log/, now the log file is even larger than 19G

How can I disable the log in the server itself, and only write logs to CloudWatch?

The following is my current logback-spring.xml configuration. Any ideas will appreciate. Thanks in advance.

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

    <configuration>

    <include resource="org/springframework/boot/logging/logback/base.xml" />

    <springProperty scope="context" name="ACTIVE_PROFILE" source="spring.profiles.active" />

    <property name="clientPattern" value="payment" />  

    <logger name="org.springframework">

    <level value="INFO" />

    </logger>

    <logger name="com.payment">

    <level value="INFO" />

    </logger>

    <logger name="org.springframework.ws.client.MessageTracing.sent">

    <level value="TRACE" />

    </logger>

    <logger name="org.springframework.ws.client.MessageTracing.received">

    <level value="TRACE" />

    </logger>

    <logger name="org.springframework.ws.server.MessageTracing">

    <level value="TRACE" />

    </logger>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

    <layout class="ch.qos.logback.classic.PatternLayout">

    <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [${HOSTNAME}:%thread] %-5level%replace([${clientPattern}] ){'\[\]\s',''}%logger{50}: %msg%n

    </pattern>

    </layout>

    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">

    <level>TRACE</level>

    </filter>

    </appender>

    <springProfile name="local,dev">

    <root level="INFO">

    <appender-ref ref="CONSOLE" />

    </root>

    </springProfile>

    <springProfile name="prod,uat">

    <timestamp key="date" datePattern="yyyy-MM-dd" />

    <appender name="AWS_SYSTEM_LOGS" class="com.payment.hybrid.log.CloudWatchLogsAppender">

    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">

    <level>TRACE</level>

    </filter>

    <layout>

    <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [${HOSTNAME}:%thread] %-5level%replace([${clientPattern}] ){'\[\]\s',''}%logger{50}:

    %msg%n

    </pattern>

    </layout>

    <logGroupName>${ACTIVE_PROFILE}-hybrid-batch</logGroupName>

    <logStreamName>HybridBatchLog-${date}</logStreamName>

    <logRegionName>app-northeast</logRegionName>

    </appender>

    <appender name="ASYNC_AWS_SYSTEM_LOGS" class="ch.qos.logback.classic.AsyncAppender">

    <appender-ref ref="AWS_SYSTEM_LOGS" />

    </appender>

    <root level="INFO">

    <appender-ref ref="ASYNC_AWS_SYSTEM_LOGS" />

    <appender-ref ref="CONSOLE" />

    </root>

    </springProfile>

    </configuration>
1
Not familiar with Springboot but as far as I know about Cloudwatch log, it requires a local file present on your machine. So you better of keep you current setting and set up a log rotation.congbaoguier
I removed <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> & <appender-ref ref="CONSOLE" />Vikki

1 Answers

1
votes

The most likely fix is to remove this line:

<appender-ref ref="CONSOLE" />

I say "most likely" because this is just writing output to the console. Which means that there's something else that redirects the output to /var/log/whatever, probably in the startup script for your application.

It's also possible that the included default file, org/springframework/boot/logging/logback/base.xml, because this file defines a file appender. I don't know if the explicit <root> definition will completely override or simply update the included default, but unless you know you need the default I'd delete the <include> statement.

If you need to recover space from the existing logfile, you can truncate it:

sudo truncate -s 0 /var/log/WHATEVER

Deleting it is not the correct solution, because it won't actually be removed until the application explicitly closes it (which means restarting your server).

As one of the commenters suggested, you can use logrotate to prevent the on-disk file from getting too large.

But by far the most important thing you should do is read the Logback documentation.