Coder Social home page Coder Social logo

koa2-bunyan-server's Introduction

koa2-bunyan-server

This is a demo server based on koa2 and bunyan. It shows how we do structured logging at Snyk.

How

Structured logging on request handling applies the following properties:

  • Logs are printed to stdout in single-line JSON form.
  • Logs include standard fields imposed by bunyan (i.e. level).
  • Requests are logged upon arrival and response.
  • All logs for a specific request share an identical requestId.
  • Errors on responses are logged with their stack trace.

But why, really?

We use 90% identical code in our microservices at Snyk. It is a result of many iterations by several authors, aimed at making our services easy to monitor, observe, and generally understand.

Use it yourself

Clone and run npm install.

To run locally and get human-readable logs, run node . | ./node_modules/.bin/bunyan.

From a separate shell, run the following commands and observe the server logs:

curl localhost:8000/?duration=555
curl localhost:8000/?duration=123\&error=yesPlease

Your output will be similar to:

[2018-01-27T15:41:38.250Z]  INFO: demo-service/57906 on your-host: Server started (startupDurationMs=121, port=8000)
[2018-01-27T15:41:41.283Z]  INFO: demo-service/57906 on your-host: Handling request (requestId=828934e0-e092-40a9-8de3-c7a6e37be70a, originalUrl=/?duration=555, method=GET)
[2018-01-27T15:41:41.283Z]  INFO: demo-service/57906 on your-host: Going to sleep for a while (requestId=828934e0-e092-40a9-8de3-c7a6e37be70a, sleepDurationInMs=555)
[2018-01-27T15:41:41.846Z]  INFO: demo-service/57906 on your-host: Woke up! (requestId=828934e0-e092-40a9-8de3-c7a6e37be70a, sleepDurationInMs=555)
[2018-01-27T15:41:41.847Z]  INFO: demo-service/57906 on your-host: Reply sent (requestId=828934e0-e092-40a9-8de3-c7a6e37be70a, originalUrl=/?duration=555, method=GET, duration=566, status=200)
[2018-01-27T15:41:41.870Z]  INFO: demo-service/57906 on your-host: Handling request (requestId=59ac8cd1-6246-4a35-9ee6-4928fc97c457, originalUrl=/?duration=123&error=yesPlease, method=GET)
[2018-01-27T15:41:41.870Z]  INFO: demo-service/57906 on your-host: Going to sleep for a while (requestId=59ac8cd1-6246-4a35-9ee6-4928fc97c457, sleepDurationInMs=123)
[2018-01-27T15:41:41.998Z]  INFO: demo-service/57906 on your-host: Woke up! (requestId=59ac8cd1-6246-4a35-9ee6-4928fc97c457, sleepDurationInMs=123)
[2018-01-27T15:41:41.999Z] ERROR: demo-service/57906 on your-host: Reply sent (requestId=59ac8cd1-6246-4a35-9ee6-4928fc97c457, originalUrl=/?duration=123&error=yesPlease, method=GET, duration=130, status=501)
    --
    error: {
      "name": "NotImplementedError",
      "message": "BOOM!",
      "stack": "NotImplementedError: BOOM!\n    at module.exports (.../koa2-bunyan-server/src/controllers/sleep.js:15:17)\n    at <anonymous>",
      "sleptInMs": 123
    }

License

See LICENSE

Further reading

Push your k8s pods' JSON logs to a managed ELK stack with fluentd-logzio-kubernetes

koa2-bunyan-server's People

Contributors

adrukh avatar

Stargazers

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