Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CloudWatch log and disable log on server itself with springboot

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>
like image 671
Vikki Avatar asked Jan 17 '26 23:01

Vikki


1 Answers

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.

like image 119
guest Avatar answered Jan 19 '26 15:01

guest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!