Created
October 5, 2020 18:51
-
-
Save Cepr0/86fb282a5697c5ba57623b51ade3d271 to your computer and use it in GitHub Desktop.
Confugure Logback with Logstash appender in Spring Boot application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pom.xml | |
------- | |
<!-- Used for conditional processing of Logback config --> | |
<!-- See: http://logback.qos.ch/manual/configuration.html#conditional--> | |
<dependency> | |
<groupId>org.codehaus.janino</groupId> | |
<artifactId>janino</artifactId> | |
<version>3.0.6</version> | |
</dependency> | |
logback-spring.xml | |
------------------ | |
<?xml version="1.0" encoding="UTF-8"?> | |
<configuration> | |
<include resource="org/springframework/boot/logging/logback/defaults.xml" /> | |
<include resource="org/springframework/boot/logging/logback/console-appender.xml" /> | |
<!-- Log file name (without extension), defaults to 'logs/log' --> | |
<!-- Based on Spring Boot 'logging.file' property --> | |
<!-- https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-configure-logback-for-logging --> | |
<property name="LOG_FILE" value="${LOG_PATH:-logs/log}" /> | |
<!-- Log file max size, defaults to 50MB --> | |
<property name="LOG_FILE_MAX_SIZE" value="${LOG_FILE_MAX_SIZE:-50MB}" /> | |
<!-- Log file appender with Logstash encoder--> | |
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | |
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | |
<level>INFO</level> | |
</filter> | |
<file>${LOG_FILE}.log</file> | |
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> | |
<fileNamePattern>${LOG_FILE}.%i.log.zip</fileNamePattern> | |
<minIndex>1</minIndex> | |
<maxIndex>1</maxIndex> | |
</rollingPolicy> | |
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> | |
<maxFileSize>${LOG_FILE_MAX_SIZE}</maxFileSize> | |
</triggeringPolicy> | |
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/> | |
</appender> | |
<!-- Define level aliases for some loggers based on Spring Boot 'logging.level' props --> | |
<!-- com.example must be replaced to project package --> | |
<springProperty scope="context" name="root" source="logging.level.root" defaultValue="warn" /> | |
<springProperty scope="context" name="example" source="logging.level.com.example" defaultValue="warn" /> | |
<springProperty scope="context" name="spring" source="logging.level.org.springframework" defaultValue="warn" /> | |
<springProperty scope="context" name="data" source="logging.level.org.springframework.data" defaultValue="warn" /> | |
<springProperty scope="context" name="web" source="logging.level.org.springframework.web" defaultValue="warn" /> | |
<springProperty scope="context" name="security" source="logging.level.org.springframework.security" defaultValue="warn" /> | |
<!-- Set up logging level for some loggers --> | |
<logger name="com.example" level="${example}" /> | |
<logger name="org.springframework" level="${spring}" /> | |
<logger name="org.springframework.data" level="${data}" /> | |
<logger name="org.springframework.web" level="${web}" /> | |
<logger name="org.springframework.security" level="${security}" /> | |
<!-- For 'local' profile - log to console and to file if 'log.file' property is set --> | |
<springProfile name="local"> | |
<root level="${root}"> | |
<appender-ref ref="CONSOLE" /> | |
<if condition='!property("LOG_PATH").isEmpty()'> | |
<then> | |
<appender-ref ref="FILE" /> | |
</then> | |
</if> | |
</root> | |
</springProfile> | |
<!-- For 'dev' and 'test' profiles - log to console and to file --> | |
<springProfile name="dev, test"> | |
<root level="${root}"> | |
<appender-ref ref="CONSOLE" /> | |
<appender-ref ref="FILE" /> | |
</root> | |
</springProfile> | |
<!-- For 'prod' and 'stage' profiles - log to file only --> | |
<springProfile name="prod, stage"> | |
<root level="${root}"> | |
<appender-ref ref="FILE" /> | |
</root> | |
</springProfile> | |
</configuration> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment