2.0.5 • Published 5 years ago

@noonesstudios/koa-logger v2.0.5

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

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

PropertyTypeInner PropertiesExampleDefaultDescription
logDirstring-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
logObject{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
painterFunction-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.
customPathsObject{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
requestFormatstring-requestFormat: --> userId method url anythingYouWant--> method url ip ua reqLengthOverride 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
responseFormatstring-responseFormat: <-- status message error anythingYouWant<-- method url ip ua status - time error messageSame as ^
hardLogFormatstring-hardLogFormat: ip ua url method anythingYouWant errormethod url ip ua status - time errorSame 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.

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago