Giter Site home page Giter Site logo

customjsonlogging's Introduction

Demo for Creating a custom JSON layout for Log4J2 (driven by SLF4j)

Purpose

To safely log events as JSON by giving the application total control over how data is logged.

Current Situation

  • Log config in every enviroment has to be changed as application logging changes.
  • Data loss if application and environment configuration are out-of-sync.
  • Exception information is not tied to error events
  • Exception information is not useful and takes a lot of space (expensive for Splunk)

Features

  • MDC kv pairs are added as first-level JSON fields without the need for additional logging configuration
  • Exceptions and stack traces are logged with the JSON event, allowing log parsers to associate the stack trace with the event.
  • Ability to define custom name-value pairs to add to logging
  • Ability to use different names for standard logging fields

Configuration

see log4j2.properties.

appender.myCustom.type = Console
appender.myCustom.name = myCustom
appender.myCustom.layout.type = ToddCustomJsonLayout
appender.myCustom.layout.environment = local dev
appender.myCustom.layout.pretty = false
  • Use whatever appender name. I used "myCustom"
  • The recommended type for 12-factor applications is Console
  • layout type is the glue that associates the appender with the java class file
  • the environment should probably be picked up through a placeholder, but it can be configured differently for different environments.
  • always use "pretty=false" when logging for an aggregator (e.g. Splunk, DataDog, Logstash, etc.)

Demo

  • Run the application LoggingDemo.
  • Verify two JSON log entries. One contains a stack trace. View in a JSON formatter or log aggregator, or set pretty=true.

Sample

The demo program uses a sample custom JSON layout ToddCustomJsonLayout. You will probably want to make your own customized as you see fit.

Annotation Wiring

There are two annotations that are required to configure this class as a logging layout:

@Plugin(name = "ToddCustomJsonLayout", category = "Core", elementType = "layout", printObject = true)
public class ToddCustomJsonLayout extends AbstractCustomJsonLayout {

...the class definition annotation allows you to define the appender in your log4j2 properties file appender.myCustom.layout.type = ToddCustomJsonLayout

    @PluginFactory
    public static ToddCustomJsonLayout createLayout(
            @PluginAttribute(value = "environment", defaultString = "unknown") String environment,
            @PluginAttribute(value = "pretty", defaultBoolean = false) boolean pretty
    ) {
        return new ToddCustomJsonLayout(environment, pretty);
    }

...the factory method annotation allows Log4J2 to construct the layout with the specified parameters from your log4j2 properties file

appender.myCustom.layout.environment = local dev
appender.myCustom.layout.pretty = false

Defining logger behavior:

There are three abstract methods from AbstractCustomJsonLayout that you must override:

  • protected Map<String, Object> mapLogEvent(LogEvent event)
  • protected Map<String, Object> logStackTrace(Throwable throwable)
  • protected Map<String, Object> logException(Throwable throwable)

mapLogEvent

Allows you to map fields from LogEvent to JSON. You can also add additional fields that are not in MDC. MDC fields are automagically added to the logging with no configuration required.

logStackTrace

Allows you to log a stack trace. The ConciseStackTraceLogger is probably fine for your needs, but you are free to plug in a different approach.

logException

Allows you to log the exception. The DefaultExceptionLogger recurses through exception causes and adds log entries for each cause. Again, you are free to plug in a different approach.

customjsonlogging's People

Watchers

 avatar  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.