Giter Site home page Giter Site logo

k-groshev / springboot-fluentd-logging-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from patoi/springboot-fluentd-logging-example

0.0 1.0 0.0 55 KB

Spring Boot logging with logback, JSON logging to the standard out from a docker container.

License: MIT License

Java 93.80% Dockerfile 6.20%

springboot-fluentd-logging-example's Introduction

Example fluentd configuration for message routing

Spring Boot logging with logback, JSON logging to the standard out from a docker container.

Docker logging with docker fluentd logger settings, fluentd writes messages to the standard out.

Goal: you don't need to add fluent dependency to your code, just logging to standard output. You can route your log messages with dest: journal key, and it will be saved to journal database, any others will be saved to the log database.

  • mvn clean install

  • Start with docker-compose up -d --build

  • Log some message with curl -X GET http://localhost:4000/greeting

  • Tail fluentd log with docker logs --follow test_fluentd

  • Stop with CTRL-C and docker-compose down --remove-orphans

When you logging with dest key with journal value, then output wil be saved into the journal DB.

@Component
public class GreetingHandler {

    Journal journal;

    @Autowired
    public GreetingHandler(Journal journal) {
        this.journal = journal;
    }

    private static final org.slf4j.Logger Logger =
            org.slf4j.LoggerFactory.getLogger(GreetingHandler.class);

    @GetMapping("/greeting")
    Mono<ServerResponse> greeting(ServerRequest request) {
        journal.log("Message sent to journal DB");
        Logger.error("Something else is wrong here");
        return ServerResponse.ok().contentType(TEXT_PLAIN).body(fromObject("Hello World!"));
    }
}
@Component
public class Journal {

    private static final org.slf4j.Logger Logger =
            org.slf4j.LoggerFactory.getLogger(Journal.class);

    public void log(String message) {
        // fluentd config filter
        MDC.put("dest", "journal");
        Logger.info(message);
    }
}

Logging JSON

pom.xml

<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>4.10</version>
</dependency>

and add logback-spring.xml

<configuration>
  <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
  </appender>
  <logger name="jsonLogger" additivity="false" level="DEBUG">
    <appender-ref ref="consoleAppender"/>
  </logger>
  <root level="INFO">
    <appender-ref ref="consoleAppender"/>
  </root>
</configuration>

Advices

Turn off Spring Boot banner

spring.main.banner-mode=off

Remove unnecessary keys from the log

remove_keys ["@timestamp", "thread_name", "level_value", "@version"]

springboot-fluentd-logging-example's People

Contributors

patoi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.