Giter Club home page Giter Club logo

lorekeeper's Introduction

Lorekeeper

Build Status

LoreKeeper contains a highly optimized JSON logger. It outputs messages as JSON and let the users to add their own customized fields. When used without extra fields it outputs 20% faster than the standard Logger for messages not longer than one line of text.

Installation

Add this line to your application's Gemfile:

gem 'lorekeeper', '~> 1.7'

And then execute:

bundle

Usage

Normal logging methods

LoreKeeper::JSONLogger API is compatible with the stdlib's Logger's.

logger.error("This is a message")

Will output:

{
  "message": "This is a message",
  "timestamp": "1970-01-01T00:00:00.000+0100",
  "level": "debug"
}

Timestamps use ISO8601. Messages are JSON escaped so the total result is JSON parseable.

Log Levels

Method Name JSON Property
debug "level": "debug"
info "level": "info"
warn "level": "warning"
error "level": "error"
fatal "level": "fatal"

Adding keys to the output

Keys can be added to the output at any moment. These keys will be output in each message till they are removed again. If you want to output keys in only one message use the *_with_data method instead.

Keys can be added using the add_fields method which accepts a hash:

logger.add_fields("role" => "backend")
logger.error("This is a message")
logger.warn("This is another message")

Will output:

{
  "message": "This is a message",
  "timestamp": "1970-01-01T00:00:00.000+0100",
  "level": "error",
  "role": "backend"
}
{
  "message": "This is another message",
  "timestamp": "1970-01-01T00:00:00.000+0100",
  "level": "warning",
  "role": "backend"
}

Because of speed purposes the JSON dumping is done as simply as possible. If you provide a hash of keys like:

{ key: 'value' }

The output will include:

{ ":key": "value" }

Logging methods with data

All methods (info, debug, etc.) have a *_with_data equivalent: info_with_data, debug_with_data, etc. These methods accept and extra hash to add it to the JSON.

logger.error_with_data('message', { data1: 'Extra data', data2: 'Extra data2' })

Will output:

{
  "message": "This is a message",
  "timestamp": "1970-01-01T00:00:00.000+0100",
  "level": "debug",
  "data": {
    "data1": "Extra data",
    "data2": "Extra data2"
  }
}

Logging exceptions

There is a method to help you log exceptions in an standard way.

rescue => e
  logger.exception(e)
end

Will output:

{
  "message": "#{e.message}",
  "timestamp": "1970-01-01T00:00:00.000+0100",
  "level": "error",
  "exception": "<exception name>",
  "stack": [
    "<stacktraceline1>",
    "<stacktraceline2>"
  ]
}

This method also accepts a custom message, data and log level.

rescue => e
  logger.exception(e, "custom msg!", { some: { data: 123 } }, :warn)
end

Will output:

{
  "message": "custom msg!",
  "timestamp": "1970-01-01T00:00:00.000+0100",
  "level": "warning",
  "data": {
    ":some": {
      ":data": 123
    }
  },
  "exception": "<exception name>",
  "stack": [
    "<stacktraceline1>",
    "<stacktraceline2>"
  ]
}

License

The gem is available as open source under the terms of the MIT License.

lorekeeper's People

Contributors

jfeltesse-mdsol avatar jordipolo avatar masongup-mdsol avatar ssteeg-mdsol avatar

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.