Giter Club home page Giter Club logo

onsdigital.dp-logging-v2's Introduction

dp-logging-v2

Examples

Initialize the logger:

    // Create a new Logback logger instance
    Logger logger = LoggerFactory.getLogger("com.github.onsdigital.logging.v2.example");

    // Initialize the log lib passing in the logger and the EventSerializer impl you wish to use.
    LoggerConfig.init(logger, new JacksonEventSerializer(true));

Logging an event using the out of the box SimpleEvent logger

    new SimpleEvent("hello.world")
            .httpMethod("GET")
            .httpPath("/hello")
            .log("request received");

Outputs:

{
  "created" : 1548236505866,
  "namespace" : "hello.world",
  "http" : {
    "path" : "/hello",
    "method" : "GET"
  },
  "event" : "request received"
}

Extending BaseEvent to add custom fields & objects to the log event:

 class ComplexEvent extends BaseEvent<ComplexEvent> {

        // specify a namespace
        public ComplexEvent(String namespace) {
            super(namespace);
        }

        // give the nested object a namespace so it will not clash with other apps.
        @JsonProperty("hello.world.data")
        private Map<String, Object> data = new HashMap<>();


        // add set methods to add your required fields
        public ComplexEvent user(String name) {
            this.data.put("name", name);
            return this;
        }

        public ComplexEvent collectionID(String id) {
            this.data.put("collectionID", id);
            return this;
        }
    }

Logging an event using the customised event type:

    new ComplexEvent("hello.world")
            .httpMethod("GET")
            .httpPath("/hello")
            .user(req.params(":name"))
            .collectionID(req.params(":collectionID"))
            .log("request received");

Outputs:

{
  "created" : 1548236618285,
  "namespace" : "hello.world",
  "http" : {
    "path" : "/hello",
    "method" : "GET"
  },
  "event" : "request received",
  "hello.world.data" : {
    "name" : "dave",
    "collectionID" : "666"
  }
}

onsdigital.dp-logging-v2's People

Contributors

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