Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhhai committed Feb 23, 2017
2 parents 74bc88d + 3ee3af5 commit 217f0fa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 33 deletions.
92 changes: 60 additions & 32 deletions koa-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 }
Expand Down Expand Up @@ -87,14 +87,14 @@ function getKoaLogger (logger4js, options) {
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 {
Expand All @@ -108,11 +108,17 @@ 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,
* content: a replace function with `ctx`
* }
* ]
* @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) {
Expand All @@ -123,34 +129,56 @@ 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))
};
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))
}

/**
* Return formatted log line.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"log4js": "^0.6.35"
},
"devDependencies": {
"standard": "^6.0.8"
"standard": "^8.5.0"
}
}

0 comments on commit 217f0fa

Please sign in to comment.