bahmutov / all-logs

Utility method to capture all server console logs including DEBUG=... module logs and util.debuglog

Home Page:https://glebbahmutov.com/blog/capture-all-the-logs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@bahmutov/all-logs renovate-app badge CircleCI

Goal

This module captures:

  • console.{log, warn, error} messages
  • all messages via debug module
  • all messages via util.debuglog

Use

npm install --save @bahmutov/all-logs

Then load this module before your CLI / server code. I suggest just requiring it before your normal module. For example, if normally you start your server like:

{
  "scripts": {
    "start": "node ./lib/server.js"
  }
}

Then you change it to simply be:

{
  "scripts": {
    "start": "node -r @bahmutov/all-logs ./lib/server.js"
  }
}

Note: when debug module is used in dependencies

This module tries to discover debug module in the current module's dependencies, and in all its immediate production dependencies. If it is possible to proxy (older versions of debug module are not compatible), then their logs will be intercepted and you will see debug logs from both your application and its immediate dependencies.

Message format

Each message object has type, namespace and the message text. It also contains a timestamp as ISO string. See src/index.d.ts for the exact interface definition.

Middleware

To expose captured log messages, use the included middleware

if (global.messages) {
  require('@bahmutov/all-logs/middleware/express')(app)
}
const http = require('http')
const logs = require('@bahmutov/all-logs/middleware/http')
http.createServer((req, res) => {
  if (logs(req, res) === true) {
    return
  }
  // your normal handler
})

Which adds GET|POST /__messages__ endpoint

See more

Development

Renovate

We are using RenovateBot to keep dependencies up to date. Find the settings in renovate.json file. Note that we ignore module debug since we are testing this module with older versions in subfolders.

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2019

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2019 Gleb Bahmutov <gleb.bahmutov@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Utility method to capture all server console logs including DEBUG=... module logs and util.debuglog

https://glebbahmutov.com/blog/capture-all-the-logs/


Languages

Language:JavaScript 100.0%