koa-logger.js
function getKoaLogger (logger4js, options) {
  if (typeof options === 'object') {
    options = options || {}
  } else if (options) {
    options = { format: options }
  } else {
    options = {}
  }

  let thislogger = logger4js
  let level = levels.getLevel(options.level, levels.INFO)
  let fmt = options.format || DEFAULT_FORMAT
  let nolog = options.nolog ? createNoLogCondition(options.nolog) : null

  return async (ctx, next) => {
    // mount safety
    if (ctx.request._logging) {
      await next()
      return
    }

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

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

      // 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
        if (options.level === 'auto') {
          level = levels.INFO
          if (code >= 300) level = levels.WARN
          if (code >= 400) level = levels.ERROR
        } else {
          level = levels.getLevel(options.level, levels.INFO)
        }
      }

      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
      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)) {
        let combinedTokens = assembleTokens(ctx, options.tokens || [])
        if (typeof fmt === 'function') {
          let line = fmt(ctx, function (str) {
            return format(str, combinedTokens)
          })
          if (line) thislogger.log(level, line)
        } else {
          thislogger.log(level, format(fmt, combinedTokens))
        }
      }
    } else {
      // ensure next gets always called
      await next()
    }
  }
}
function getKoaLogger (logger4js, options) {
  if (typeof options === 'object') {
    options = options || {}
  } else if (options) {
    options = { format: options }
  } else {
    options = {}
  }

  let thislogger = logger4js
  let level = levels.getLevel(options.level, levels.INFO)
  let fmt = options.format || DEFAULT_FORMAT
  let nolog = options.nolog ? createNoLogCondition(options.nolog) : null

  return async (ctx, next) => {
    // mount safety
    if (ctx.request._logging) {
      await next()
      return
    }

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

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

      // 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
        if (options.level === 'auto') {
          level = levels.INFO
          if (code >= 300) level = levels.WARN
          if (code >= 400) level = levels.ERROR
        } else {
          level = levels.getLevel(options.level, levels.INFO)
        }
      }

      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
      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)) {
        let combinedTokens = assembleTokens(ctx, options.tokens || [])
        if (typeof fmt === 'function') {
          let line = fmt(ctx, function (str) {
            return format(str, combinedTokens)
          })
          if (line) thislogger.log(level, line)
        } else {
          thislogger.log(level, format(fmt, combinedTokens))
        }
      }
    } else {
      // ensure next gets always called
      await next()
    }
  }
}
function getKoaLogger ( logger4js , options ) { if ( typeof options === 'object' ) { options = options || { } } else if ( options ) { options = { format : options } } else { options = { } } let thislogger = logger4js let level = levels . getLevel ( options . level , levels . INFO ) let fmt = options . format || DEFAULT_FORMAT let nolog = options . nolog ? createNoLogCondition ( options . nolog ) : null return async ( ctx , next ) => { // mount safety if ( ctx . request . _logging ) { await next ( ) return } // nologs if ( nolog && nolog . test ( ctx . originalUrl ) ) { await next ( ) return } if ( thislogger . isLevelEnabled ( level ) || options . level === 'auto' ) { let start = new Date ( ) let writeHead = ctx . response . writeHead // flag as logging ctx . request . _logging = true // 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 if ( options . level === 'auto' ) { level = levels . INFO if ( code >= 300 ) level = levels . WARN if ( code >= 400 ) level = levels . ERROR } else { level = levels . getLevel ( options . level , levels . INFO ) } } 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 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 ) ) { let combinedTokens = assembleTokens ( ctx , options . tokens || [ ] ) if ( typeof fmt === 'function' ) { let line = fmt ( ctx , function ( str ) { return format ( str , combinedTokens ) } ) if ( line ) thislogger . log ( level , line ) } else { thislogger . log ( level , format ( fmt , combinedTokens ) ) } } } else { // ensure next gets always called await next ( ) } } }
Log requests with the given `options` or a `format` string.
Use for Koa v1

Options:

- `format`        Format string, see below for tokens
- `level`         A log4js levels instance. Supports also 'auto'

Tokens:

- `:req[header]` ex: `:req[Accept]`
- `:res[header]` ex: `:res[Content-Length]`
- `:http-version`
- `:response-time`
- `:remote-addr`
- `:date`
- `:method`
- `:url`
- `:referrer`
- `:user-agent`
- `:status`

@param {String|Function|Object} format or options
@return {Function}
@api public
Log requests with the given options or a format string . Use for Koa v1