GithubHelp home page GithubHelp logo

lazy-logger's Introduction

lazy-logger

A logger that will store logs in memory until you are ready to write them to STDOUT.

A couple other things to note.

  • When you call write or flush to actually print the logs, it will use " -- " as the delimiter by default instead of newline
  • It will also replace newlines in the resulting message with " "

The purpose of removing all newlines is to get all logs on a single line. Cloudwatch breaks logs up by newline. So it becomes difficult to associate your logs with a particular request.

So, here is an example of how this is useful:

const LazyLogger = require('./src/lib')

const db = {
    read: () => Promise.resolve([1,2,3,4]),
    doThings: () => Promise.resolve({
        msg: 'Successfully wrote 4 records'
    })
}

const go = async () => {
    let logger = LazyLogger()
    const request = {id: 1234}
    try {
        logger.info(`RequestID: ${request.id}`)

        const things = await db.read()
        logger.info(`Read ${things.length} records from the db`)

        const result = await db.doThings(things)
        logger.info(`DB Result: ${JSON.stringify(result)}`)

        intentional.error()
    } catch (e) {
        logger.error(e)
    } finally {
        logger.write() // writes `INFO:RequestID: 1234 -- INFO:Read 4 records from the db -- INFO:DB Result: {"msg":"Successfully wrote 4 records"} -- ERROR:ReferenceError: intentional is not defined      at go (/Users/asdf/lazy-logger/example.js:20:9)`
    }
}
go()

Then in CloudWatch you will only have a single log entry for this request:

INFO:RequestID: 1234 -- INFO:Read 4 records from the db -- INFO:DB Result: {"msg":"Successfully wrote 4 records"} -- ERROR:ReferenceError: intentional is not defined      at go (/Users/asdf/lazy-logger/example.js:20:9)

If you need to log deeper than the top level, then I would recommend passing the logger down to any function that you want to log.

lazy-logger's People

Contributors

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