Giter Site home page Giter Site logo

purescript-erl-logger's Introduction

Logger

Adding erl-logger to spago.dhall is all that's required for logger, as the module is bundled with OTP 21.0 by default.

While the API is still quite simplistic, it does at least have the ability for per-module logging to be configured as well as support up-front for structured logging, which means supplying filterable context as well as the inclusion of actual code context (module/function/etc). The API is a little less user-friendly up-front but this serves for a better experience in a production project.

Methods are exposed for the various log levels (info/debug/warning/notice/etc) that take in a metadata with all of the information for the logging call, as well as a report which describes the logging call. It is important to note that metadata is the primary purpose of logging and the report is secondary to that.

The underlying logger module is capable of much more than is exposed in the Purerl module and presently only supports the needs of logging styles requested by clients of the code so far. Pull requests and requests are obviously accepted.

let domain = (atom "the_domain" : nil)
    metadata = Logger.traceMetadata domain "This is the message for the log"
    report = {}
 _ <- Logger.info metadata report

This is obviously quite verbose, but in essence we end up with per-project helpers for the various domains present within the project that help us do the logging that we need across them.

domain :: List Atom
domain = (atom "acme") : (atom "project") : (atom "component") : nil

logInfo :: report. String -> { | report }->
logInfo = Logger.info <<< Logger.traceMetadata domain

logWarning :: forall report. String -> { | report }->
logWarning = Logger.warning <<< Logger.traceMetadata domain

The usage of which is then simply

logInfo "Something happened to this stream" { streamId: stream.id, nodeId: node.id }

Now this is actually not quite right, as we're stuffing data into the report that might best be in the metadata, instead we might want to consider building the custom metadata ourselves.

We can set this globally per process so that all logs from a single process will automatically have this metadata applied

-- During process initialisation (for example Gen.Init )
_ <- Logger.addLoggerContext { streamId: stream.id, nodeId: node.id }

-- A typical log call elsewhere
logInfo "Something happened to this stream" {}

Now the metadata will be supplied for every logging call in this process, and it will be metadata as opposed to report-data which is the correct place for it to be.

If we need to build our own metadata on a per-call basis, then this is slightly more involved as it needs merging with the underlying pre-filled in metadata (containing domain, type, text). We can either do some row-level magic with Purescript, or just supply all of this information ourselves as part of the logging call.

Logger.warning { domain, type: Logger.Trace, text: "Something happened to this stream", streamId: stream.id, nodeId: nodeId  } {}

And now we're back to the start again, calling Logger directly. Obviously it's possible to wrap this up in whatever way it most convenient for the size of the project, but the important thing is that the flexibility and power is there to do proper structured logging all the way down.

purescript-erl-logger's People

Contributors

srstrong avatar robashton avatar nwolverson avatar

Watchers

 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.