@noonesstudios/koa-logger

2.0.5 • Public • Published

install size

Inspired by

koa-logger morgan

Install

npm i @noonesstudios/koa-logger

Placement

const koa = require('koa');
const app = new koa();
const logger = require('@noonesstudios/koa-logger'); // initialize it once
const errorHandler = require('./path-to-your-error-handler');
const routes = require('./path-to-your-routes');
app.use(errorHandler);
app.use(logger); // place it here. if any error occurs, `logger` logs it and rethrows it up the chain, while exposing ctx.error = error
...
...
app.use(routes);

CONFIG FILE

By default, logger looks for a .js file named koa-logger.config.js in app root. and merges it overridingly, if it exists, with a bunch of defaults, which help it run without any configuration whatsoever.

The function utilizes the native npm command npm root so hopefully you have npm installed =)

You can specify a different filename to look for by appending an argument: --koa-logger-filename=yourfilename.js

API

Property Type Inner Properties Example Default Description
logDir string - logDir: './logs' - Choose where you would like to save any "hard" logs. if the provided directory does not yet exist, logger will create it for you
log Object {errors: boolean} log: {errors: true} {} Events you wish to log to your hard drive. Will only work if logDir is provided. Will not crash otherwise
painter Function - painter: require('chalk') - A module to use colors with if you would like to colorize your req/res logs. Hard logs are always set to be saved without them.
customPaths Object {anythingYouWant: string / Object: {path: string, str: string, formatter: Function, color: string / Function}} customPaths: {userId: 'state.id', requestBytes: { path: 'request.length', formatter: v => require('bytes')(v), color: 'red' }} - Declare / Override paths which should be found in the ongoing ctx. Once you declare them, you can use them in requestFormat, responseFormat and hardLogFormat
requestFormat string - requestFormat: --> userId method url anythingYouWant --> method url ip ua reqLength Override default request format. You can use custom props if declared in customPaths. Props must be separated by spaces. If a prop was not declared in customPaths, it will be logged as is. e.g url - method will be logged as /users - GET
responseFormat string - responseFormat: <-- status message error anythingYouWant <-- method url ip ua status - time error message Same as ^
hardLogFormat string - hardLogFormat: ip ua url method anythingYouWant error method url ip ua status - time error Same as ^, no colors (ansi) attached.

Default Paths

method: 'method',
url: 'originalUrl',
ip: 'ip',
ua: 'headers.user-agent',
status: 'status',
message: 'body.message',
time: 'state.delta',
error: 'error.message'

Example

// koa-logger.config.js or your-custom-filename.js in root dir 

const path = require('path');
const chalk = require('chalk');

module.exports = {
  logDir: path.join(__dirname, './logs'),
  log: {
    errors: true
  },
  painter: chalk,
  customPaths: {
    reqIndicator: {
       str: '-->',
       color: 'gray'
    },
    resIndicator: {
       str: (ctx) => ctx.status < 400 ? '<--' : 'xxx',
       color: (prop, ctx) => ctx.status >= 400 ? 'red' : 'gray'
    },
    userId: {
       path: 'state.user.id',
       formatter: v => v ? v : '[NO_ID]',
       color: 'blue'
    },
  },
  requestFormat: 'reqIndicator userId method url',
  responseFormat: 'resIndicator userId method url - time message error',
  hardLogFormat: 'userId ua method url ::: error'
}

Future

Depends on requests if any.

Package Sidebar

Install

npm i @noonesstudios/koa-logger

Weekly Downloads

0

Version

2.0.5

License

MIT

Unpacked Size

8.52 kB

Total Files

4

Last publish

Collaborators

  • noonesstudios