From a582174f3fbfc3dde3bdb50296c55e9c89f76d30 Mon Sep 17 00:00:00 2001 From: dominhhai Date: Tue, 15 Nov 2016 10:54:56 +0900 Subject: [PATCH 1/3] be standard --- koa-logger.js | 90 +++++++++++++++++++++++++++++---------------------- package.json | 4 +-- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/koa-logger.js b/koa-logger.js index 12c2614..84a10c8 100644 --- a/koa-logger.js +++ b/koa-logger.js @@ -35,7 +35,7 @@ const DEFAULT_FORMAT = ':remote-addr - -' + */ function getKoaLogger (logger4js, options) { - if (typeof options == 'object') { + if (typeof options === 'object') { options = options || {} } else if (options) { options = { format: options } @@ -52,23 +52,23 @@ function getKoaLogger (logger4js, options) { // mount safety if (ctx.request._logging) return yield next() - // nologs + // nologs if (nolog && nolog.test(ctx.originalUrl)) return yield 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 @@ -79,23 +79,23 @@ function getKoaLogger (logger4js, options) { } yield next() - // hook on end request to emit the log entry of the HTTP request. + // 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 if (ctx.res.statusCode >= 400) level = levels.ERROR } if (thislogger.isLevelEnabled(level)) { - var combined_tokens = assemble_tokens(ctx, options.tokens || []) + var combinedTokens = assembleTokens(ctx, options.tokens || []) if (typeof fmt === 'function') { var line = fmt(ctx, function (str) { - return format(str, combined_tokens) + return format(str, combinedTokens) }) if (line) thislogger.log(level, line) } else { - thislogger.log(level, format(fmt, combined_tokens)) + thislogger.log(level, format(fmt, combinedTokens)) } } } else { @@ -109,11 +109,11 @@ function getKoaLogger (logger4js, options) { * Adds custom {token, replacement} objects to defaults, overwriting the defaults if any tokens clash * * @param {Koa Context} ctx - * @param {Array} custom_tokens [{ token: string-or-regexp, replacement: string-or-replace-function }] + * @param {Array} customTokens [{ token: string-or-regexp, replacement: string-or-replace-function }] * @return {Array} */ -function assemble_tokens (ctx, custom_tokens) { - var array_unique_tokens = function (array) { +function assembleTokens (ctx, customTokens) { + var arrayUniqueTokens = function (array) { let a = array.concat() for (let i = 0; i < a.length; ++i) { for (let j = i + 1; j < a.length; ++j) { @@ -124,34 +124,48 @@ function assemble_tokens (ctx, custom_tokens) { } return a } - var default_tokens = [] - default_tokens.push({ token: ':url', replacement: ctx.originalUrl }) - default_tokens.push({ token: ':protocol', replacement: ctx.protocol }) - default_tokens.push({ token: ':hostname', replacement: ctx.hostname }) - default_tokens.push({ token: ':method', replacement: ctx.method }) - default_tokens.push({ token: ':status', replacement: ctx.response.status || - ctx.response.__statusCode || ctx.res.statusCode }) - default_tokens.push({ token: ':response-time', replacement: ctx.response.responseTime }) - default_tokens.push({ token: ':date', replacement: new Date().toUTCString() }) - default_tokens.push({ token: ':referrer', replacement: ctx.headers.referer || '' }) - default_tokens.push({ token: ':http-version', replacement: ctx.req.httpVersionMajor + '.' + ctx.req.httpVersionMinor }) - default_tokens.push({ token: ':remote-addr', replacement: ctx.headers['x-forwarded-for'] || ctx.ip || ctx.ips || - (ctx.socket && (ctx.socket.remoteAddress || (ctx.socket.socket && ctx.socket.socket.remoteAddress))) }) - default_tokens.push({ token: ':user-agent', replacement: ctx.headers['user-agent'] }) - default_tokens.push({ token: ':content-length', replacement: (ctx.response._headers && ctx.response._headers['content-length']) || - (ctx.response.__headers && ctx.response.__headers['Content-Length']) || - ctx.response.length || '-' }) - default_tokens.push({ token: /:req\[([^\]]+)\]/g, replacement: function (_, field) { - return ctx.headers[field.toLowerCase()] - } }) - default_tokens.push({ token: /:res\[([^\]]+)\]/g, replacement: function (_, field) { - return ctx.response._headers + var defaultTokens = [] + defaultTokens.push({ token: ':url', replacement: ctx.originalUrl }) + defaultTokens.push({ token: ':protocol', replacement: ctx.protocol }) + defaultTokens.push({ token: ':hostname', replacement: ctx.hostname }) + defaultTokens.push({ token: ':method', replacement: ctx.method }) + defaultTokens.push({ + token: ':status', + replacement: ctx.response.status || ctx.response.__statusCode || ctx.res.statusCode + }) + defaultTokens.push({ token: ':response-time', replacement: ctx.response.responseTime }) + defaultTokens.push({ token: ':date', replacement: new Date().toUTCString() }) + defaultTokens.push({ token: ':referrer', replacement: ctx.headers.referer || '' }) + defaultTokens.push({ token: ':http-version', replacement: ctx.req.httpVersionMajor + '.' + ctx.req.httpVersionMinor }) + defaultTokens.push({ + token: ':remote-addr', + replacement: ctx.headers['x-forwarded-for'] || ctx.ip || ctx.ips || + (ctx.socket && (ctx.socket.remoteAddress || (ctx.socket.socket && ctx.socket.socket.remoteAddress))) + }) + defaultTokens.push({ token: ':user-agent', replacement: ctx.headers['user-agent'] }) + defaultTokens.push({ + token: ':content-length', + replacement: (ctx.response._headers && ctx.response._headers['content-length']) || + (ctx.response.__headers && ctx.response.__headers['Content-Length']) || + ctx.response.length || '-' + }) + defaultTokens.push({ + token: /:req\[([^\]]+)\]/g, + replacement: function (_, field) { + return ctx.headers[field.toLowerCase()] + } + }) + defaultTokens.push({ + token: /:res\[([^\]]+)\]/g, + replacement: function (_, field) { + return ctx.response._headers ? (ctx.response._headers[field.toLowerCase()] || ctx.response.__headers[field]) : (ctx.response.__headers && ctx.response.__headers[field]) - } }) + } + }) - return array_unique_tokens(custom_tokens.concat(default_tokens)) -}; + return arrayUniqueTokens(customTokens.concat(defaultTokens)) +} /** * Return formatted log line. diff --git a/package.json b/package.json index 7b043c3..5ac37fe 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,9 @@ "homepage": "https://github.com/dominhhai/koa-log4js#readme", "dependencies": { "co": "^4.6.0", - "log4js": "^0.6.35" + "log4js": "^1.0.1" }, "devDependencies": { - "standard": "^6.0.8" + "standard": "^8.5.0" } } From 4ad7e2f7e3eda602f86ba38a9bd83d84c61cd794 Mon Sep 17 00:00:00 2001 From: dominhhai Date: Thu, 24 Nov 2016 15:39:32 +0900 Subject: [PATCH 2/3] add `content` as replacement function with `ctx` --- koa-logger.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/koa-logger.js b/koa-logger.js index 84a10c8..4646847 100644 --- a/koa-logger.js +++ b/koa-logger.js @@ -109,7 +109,13 @@ function getKoaLogger (logger4js, options) { * Adds custom {token, replacement} objects to defaults, overwriting the defaults if any tokens clash * * @param {Koa Context} ctx - * @param {Array} customTokens [{ token: string-or-regexp, replacement: string-or-replace-function }] + * @param {Array} customTokens [ + * { + * token: string-or-regexp, + * replacement: string-or-replace-function, + * content: a replace function with `ctx` + * } + * ] * @return {Array} */ function assembleTokens (ctx, customTokens) { @@ -164,6 +170,14 @@ function assembleTokens (ctx, customTokens) { } }) + customTokens = customTokens.map(function (token) { + if (token.content && typeof token.content === 'function') { + token.replacement = token.content(ctx) + delete token.content + } + return token + }) + return arrayUniqueTokens(customTokens.concat(defaultTokens)) } From 3ee3af59389210a5f54775ffbc9e586f76bb0f4c Mon Sep 17 00:00:00 2001 From: dominhhai Date: Thu, 24 Nov 2016 15:39:50 +0900 Subject: [PATCH 3/3] 2.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5ac37fe..ca40289 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "koa-log4", - "version": "2.0.2", + "version": "2.1.0", "description": "log4js-node support Koa-middleware", "main": "index.js", "scripts": {