Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-external-logger

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-external-logger - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

25

History.md
3.0.1 / 2017-06-30
==================
* Fixes error 'chalk[color] does not return function' when using custom (#61)
3.0.0 / 2017-05-16
==================
* use async function
* update bytes to 2.x
2.0.1 / 2017-01-12
==================
* add color for code 0
* bump deps
2.0.0 / 2015-11-19
==================
* add: support for koa 2
* remove: support for koa 1
1.2.2 / 2014-07-05

@@ -6,3 +29,3 @@ ==================

* fix: stop using octal literals for strict mode
1.2.1 / 2014-05-13

@@ -9,0 +32,0 @@ ==================

148

index.js
/**
* Module dependencies.
*/
'use strict'
var Counter = require('passthrough-counter');
var humanize = require('humanize-number');
var bytes = require('bytes');
var chalk = require('chalk');
var co = require('co');
const Counter = require('passthrough-counter')
const humanize = require('humanize-number')
const bytes = require('bytes')
const chalk = require('chalk')
/**
* TTY check for dev format.
*/
var isatty = process.stdout.isTTY;
/**
* Expose logger.
*/
module.exports = dev;
module.exports = dev

@@ -27,3 +21,4 @@ /**

var colorCodes = {
const colorCodes = {
7: 'magenta',
5: 'red',

@@ -33,4 +28,5 @@ 4: 'yellow',

2: 'green',
1: 'green'
};
1: 'green',
0: 'yellow'
}

@@ -41,25 +37,26 @@ /**

function dev(opts) {
function dev (opts) {
opts = opts || {};
opts.consoleEnabled = typeof opts.consoleEnabled === 'boolean' ?
opts.consoleEnabled = typeof opts.consoleEnabled === 'boolean' ?
opts.consoleEnabled : true;
opts.externalLogger = opts.externalLogger || null;
opts.externalLogger = typeof opts.externalLogger === 'function' ?
opts.externalLogger : null;
return function *logger(next) {
return async function logger (ctx, next) {
// request
var start = Date.now();
const start = Date.now()
if (opts.consoleEnabled) {
console.log(' ' + chalk.gray('<--')
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s'),
this.method,
this.originalUrl);
}
console.log(' ' + chalk.gray('<--') +
' ' + chalk.bold('%s') +
' ' + chalk.gray('%s'),
ctx.method,
ctx.originalUrl)
}
try {
yield next;
await next()
} catch (err) {
// log uncaught downstream errors
yield log(this, start, null, err, null, opts);
throw err;
await log(ctx, start, null, err, null, opts)
throw err
}

@@ -70,9 +67,9 @@

// only necessary if a content-length header is currently not set.
var length = this.response.length;
var body = this.body;
var counter;
if (null == length && body && body.readable) {
this.body = body
const length = ctx.response.length
const body = ctx.body
let counter
if (length == null && body && body.readable) {
ctx.body = body
.pipe(counter = Counter())
.on('error', this.onerror);
.on('error', ctx.onerror)
}

@@ -82,17 +79,14 @@

// whichever happens first.
var ctx = this;
var res = this.res;
const res = ctx.res
var onfinish = done.bind(null, 'finish');
var onclose = done.bind(null, 'close');
const onfinish = done.bind(null, 'finish')
const onclose = done.bind(null, 'close')
res.once('finish', onfinish);
res.once('close', onclose);
res.once('finish', onfinish)
res.once('close', onclose)
function done(event){
res.removeListener('finish', onfinish);
res.removeListener('close', onclose);
co(function*() {
yield log(ctx, start, counter ? counter.length : length, null, event, opts);
}).catch(function(err) { });
function done (event) {
res.removeListener('finish', onfinish)
res.removeListener('close', onclose)
log(ctx, start, counter ? counter.length : length, null, event, opts)
}

@@ -106,27 +100,38 @@ }

function* log(ctx, start, len, err, event, opts) {
async function log (ctx, start, len, err, event, opts) {
// get the status code of the response
var status = err
const status = err
? (err.status || 500)
: (ctx.status || 404);
: (ctx.status || 404)
// set the color of the status code;
var s = status / 100 | 0;
var color = colorCodes[s];
const s = status / 100 | 0
const color = colorCodes.hasOwnProperty(s) ? colorCodes[s] : 0
// get the human readable response length
var length;
// let length
// if (~[204, 205, 304].indexOf(status)) {
// length = ''
// } else if (len == null) {
// length = '-'
// } else {
// length = len
// }
let length, lenStr
if (~[204, 205, 304].indexOf(status)) {
length = '';
} else if (null == len) {
length = 0;
length = 0
lenStr = ''
} else if (len == null) {
length = 0
lenStr = '-'
} else {
length = len;
length = len
lenStr = bytes(len).toLowerCase()
}
var upstream = err ? chalk.red('xxx')
const upstream = err ? chalk.red('xxx')
: event === 'close' ? chalk.yellow('-x-')
: chalk.gray('-->');
: chalk.gray('-->')
var duration = Date.now() - start;
const duration = Date.now() - start

@@ -143,12 +148,12 @@ if (opts.externalLogger) {

};
yield opts.externalLogger(logObj);
await opts.externalLogger(logObj);
}
if (opts.consoleEnabled) {
console.log(' ' + upstream
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk[color]('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.gray('%s'),
console.log(' ' + upstream +
' ' + chalk.bold('%s') +
' ' + chalk.gray('%s') +
' ' + chalk[color]('%s') +
' ' + chalk.gray('%s') +
' ' + chalk.gray('%s'),
ctx.method,

@@ -158,3 +163,3 @@ ctx.originalUrl,

time(duration),
bytes(length));
lenStr)
}

@@ -169,7 +174,6 @@ }

function time(delta) {
delta = delta < 10000
function time (delta) {
return humanize(delta < 10000
? delta + 'ms'
: Math.round(delta / 1000) + 's';
return humanize(delta);
: Math.round(delta / 1000) + 's')
}

@@ -5,3 +5,3 @@ {

"repository": "https://github.com/sedenardi/koa-external-logger.git",
"version": "1.1.0",
"version": "2.0.0",
"keywords": [

@@ -17,21 +17,30 @@ "koa",

"scripts": {
"test": "mocha --harmony test.js"
"lint": "eslint --fix .",
"test": "mocha test.js"
},
"devDependencies": {
"chai": "^2.3.0",
"koa": "^0.21.0",
"koa-route": "^2.4.1",
"mocha": "^2.2.5",
"sinon": "^1.14.1",
"sinon-chai": "^2.7.0",
"supertest": "^1.0.1"
"chai": "^3.5.0",
"eslint": "^3.19.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"koa": "^2.2.0",
"koa-route": "^3.2.0",
"mocha": "^3.2.0",
"sinon": "^1.17.7",
"sinon-chai": "^2.8.0",
"supertest": "^2.0.1"
},
"license": "MIT",
"dependencies": {
"bytes": "1",
"chalk": "^1.0.0",
"humanize-number": "~0.0.1",
"passthrough-counter": "^1.0.0",
"co": "^4.6.0"
"bytes": "^2.5.0",
"chalk": "^1.1.3",
"humanize-number": "0.0.2",
"passthrough-counter": "^1.0.0"
},
"engines": {
"node": ">= 7.6.0"
}
}

@@ -6,2 +6,4 @@

___Notice: `koa-external-logger@2` supports `koa@2`; if you want to use this module with `koa@1`, please use `koa-external-logger@1`.___
```

@@ -21,3 +23,3 @@ <-- GET /

```js
[npm install koa-external-logger](https://www.npmjs.com/package/koa-external-logger)
$ npm install koa-external-logger
```

@@ -28,12 +30,12 @@

```js
var logger = require('koa-external-logger');
var koa = require('koa');
const logger = require('koa-logger')
const Koa = require('koa')
var app = koa();
const app = new Koa()
app.use(logger({
externalLogger: function*(logObj) {
//perform some action or yield to another generator/promise
externalLogger: async function(logObj) {
//perform some action or await to another promise
},
consoleEnabled: true
}));
consoleEnabled: process.env.NODE_ENV !== 'production'
}))
```

@@ -43,3 +45,3 @@

* `externalLogger` - optional - Generator function that takes a logging object as a parameter.
* `externalLogger` - optional - Async function/promise that takes a logging object as a parameter.
* `consoleEnabled` - optional, default: true - If set to false, nothing will be sent to console.log.

@@ -64,2 +66,2 @@

MIT
MIT
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc