Skip to content

Commit

Permalink
use native aa
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhhai committed Feb 23, 2017
1 parent 504f5fe commit 74bc88d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
25 changes: 12 additions & 13 deletions koa-logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const co = require('co')
const levels = require('log4js').levels
const DEFAULT_FORMAT = ':remote-addr - -' +
' ":method :url HTTP/:http-version"' +
Expand Down Expand Up @@ -48,27 +47,27 @@ function getKoaLogger (logger4js, options) {
var fmt = options.format || DEFAULT_FORMAT
var nolog = options.nolog ? createNoLogCondition(options.nolog) : null

return co.wrap(function *(ctx, next) {
return async (ctx, next) => {
// mount safety
if (ctx.request._logging) return yield next()
if (ctx.request._logging) return await next()

// nologs
if (nolog && nolog.test(ctx.originalUrl)) return yield next()
// nologs
if (nolog && nolog.test(ctx.originalUrl)) return await next()
if (thislogger.isLevelEnabled(level) || options.level === 'auto') {
var start = new Date()
var writeHead = ctx.response.writeHead

// flag as logging
// flag as logging
ctx.request._logging = true

// proxy for statusCode.
// proxy for statusCode.
ctx.response.writeHead = function (code, headers) {
ctx.response.writeHead = writeHead
ctx.response.writeHead(code, headers)
ctx.response.__statusCode = code
ctx.response.__headers = headers || {}

// status code response level handling
// status code response level handling
if (options.level === 'auto') {
level = levels.INFO
if (code >= 300) level = levels.WARN
Expand All @@ -78,10 +77,10 @@ function getKoaLogger (logger4js, options) {
}
}

yield next()
// hook on end request to emit the log entry of the HTTP request.
await next()
// hook on end request to emit the log entry of the HTTP request.
ctx.response.responseTime = new Date() - start
// status code response level handling
// status code response level handling
if (ctx.res.statusCode && options.level === 'auto') {
level = levels.INFO
if (ctx.res.statusCode >= 300) level = levels.WARN
Expand All @@ -100,9 +99,9 @@ function getKoaLogger (logger4js, options) {
}
} else {
// ensure next gets always called
yield next()
await next()
}
})
}
}

/**
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-log4",
"version": "2.0.2",
"version": "2.2.0",
"description": "log4js-node support Koa-middleware",
"main": "index.js",
"scripts": {
Expand All @@ -22,7 +22,6 @@
},
"homepage": "https://github.com/dominhhai/koa-log4js#readme",
"dependencies": {
"co": "^4.6.0",
"log4js": "^0.6.35"
},
"devDependencies": {
Expand Down

0 comments on commit 74bc88d

Please sign in to comment.