Git Product home page Git Product logo

koa-log4js's Introduction

koa-log4js

A wrapper for log4js-node which support Koa logger middleware. Log message is forked from Express (Connect) logger file.

Note

This branch is use to Koa v2.x. To use Koa v0.x & v1.x, please check the master branch.

Installation

for koa v0.x & v1.x

$ npm i --save koa-log4@1

for koa v2.x

$ npm i --save koa-log4@2

The default logger is for koa v2.x

$ npm i --save koa-log4

Usage

Config koa-log4js is same as the original log4js-node.

Normal log4js way

This way is same as the original log4js-node.

const log4js = require('koa-log4')

const log = log4js.getLogger('index')
log.info('index do some awesome things')

Koa-middleware way

Similar to use Express (Connect) logger middleware.

const log4js = require('koa-log4')
app.use(log4js.koaLogger(log4js.getLogger("http"), { level: 'auto' }))

There are more configuration options:

const log4js = require("koa-log4")
app.use(log4js.koaLogger(
    // the logger to log to
    log4js.getLogger("http"),
    // the options object
    {
        // select the level all access logs will be set to, or use "auto" to choose depending on the status code (see next option)
        level: "auto",
        // if `level` is set to "auto" the default rule will map 200-299 to INFO, 300-399 to WARN and 400-599 to ERROR.
        // you can override this behavior by setting your own custom levelMapper.
        // (the example is the default implementation, do not copy unless you want to modify it)
        levelMapper: function(statusCode) {
            if (statusCode >= 400)
                return levels.ERROR
            if (statusCode >= 300)
                return levels.WARN
            return levels.INFO
        }
    }
))

Full Example

Check this repo for full example with Koa v2.

Others

See here for more info.

koa-log4js's People

Contributors

diegocrzt avatar dominhhai avatar madmuffin1 avatar minhhaido avatar sunilwang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

koa-log4js's Issues

AssertionError: app.use() requires a generator function

Hello!

I've been trying to get this to work in a fairly basic Koa app, and though I finally succeeded I had to make a hackish workaround to do so...

According to the example app and the README.md, this should work:

app.use(log4js.koaLogger(log4js.getLogger('http'), { level: 'auto' }));

However I was getting an error:

AssertionError: app.use() requires a generator function

So I browsed the source, did some googling, did some more source browsing and some tinkering which finally amounted to this:

app.use(function *(next) {
  let x = () => {return next};
  yield log4js.koaLogger(log4js.getLogger('http'), { level: 'auto' })(this, x);
});

It looks like the co.wrap() returns a promise? Console.log:

{ [Function: createPromise] __generatorFunction__: [Function] }

So I tried yielding to it with next, but the logger calls yield next() which then complains that next isn't a function. So I wrapped that too.

Am I missing something, or did this just not get updated from Koa 1.x yet? As it is, it works for me now and turns out to be less applicable than I thought to my particular project, but I figured I would open an issue to bring it to your attention anyway.

must have a property "appenders" of type object.

when i start my project.it throw new Error :must have a property "appenders" of type object. do you know how can i slove the problem? thank you very much.

Error: Problem with log4js configuration: ({ appenders:
[ { type: 'console', category: 'http' },
{ type: 'console', category: 'kafka' },
{ type: 'console', category: 'mysql' },
{ type: 'console', category: 'redis' },
{ type: 'console', category: 'info' } ] }) - must have a property "appenders" of type object.
at tests.forEach (/media/yanqi/娱乐/work_space/comments-writer-server/node_modules/_log4js@2.5.3@log4js/lib/configuration.js:43:15)

Re: Versions

Sorry to bother you again, but I have a suggestion which may solve the version issue:
If the 2.x branch is for Koa 2.x, would it make sense to add "koa": "^2.0.0" to the package.json dependencies? (And maybe even to the 1.x branch as "koa": "^1.0.0" for consistency?

update to "log4js": "1.1.0" ?

Hi:

now the dependency log4js is 0.6

Is there any plan to update to "log4js" to "1.1.0" ?

When I use

log4js.configure(GConfig.log4jsConfig, { reloadSecs: 300 })

got warning :
WARN] log4js - Ignoring configuration reload parameter for "object" configuration.

v2.2.0 could not work with typescript 2.2.1

Hi dominhhai,
After upgrading koa-log4js to 2.2.0, it could not work well as complaining:
\node_modules\.2.2.0@koa-log4\koa-logger.js:50 return async (ctx, next) => { ^ SyntaxError: Unexpected token ( at Object.exports.runInThisContext (vm.js:76:16) at Module._compile (module.js:542:28) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19)

typescript version as:
$ tsc -v Version 2.2.1

while v2.1.0 works well;
May u kindly help on it, thanks~

How to get request body with customize token?

The code support custom_tokens, but if the replacement is something like ctx.request.body,
I can't pass the option just like:

app.use(log4js.koaLogger(log4js.getLogger("http"), { level: 'auto',
    tokens: [
        { token: ':body', replacement: ctx.request.body}
    ],
    format: ':remote-addr - -' +
    ' ":method :url HTTP/:http-version"' +
    ' :status :content-length ":referrer"' +
    ' ":user-agent"' +
    ' :body'
}))

because ctx is not defined here.

I can use a wrapper, but is there any simple way to solve this problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.