Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dominhhai/koa-log4js
log4js-node supports Koa-middleware
https://github.com/dominhhai/koa-log4js
koa koa-log4js koa-middleware log4js-node logger-middleware
Last synced: about 1 month ago
JSON representation
log4js-node supports Koa-middleware
- Host: GitHub
- URL: https://github.com/dominhhai/koa-log4js
- Owner: dominhhai
- License: mit
- Created: 2016-04-20T00:43:48.000Z (over 8 years ago)
- Default Branch: v2.x
- Last Pushed: 2020-02-27T08:58:59.000Z (almost 5 years ago)
- Last Synced: 2024-10-31T08:52:20.409Z (about 1 month ago)
- Topics: koa, koa-log4js, koa-middleware, log4js-node, logger-middleware
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/koa-log4
- Size: 23.4 KB
- Stars: 82
- Watchers: 4
- Forks: 16
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-koa - koa-log4 - 基于log4js-node封装的中间件。 ![](https://img.shields.io/github/stars/dominhhai/koa-log4js.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-log4.svg?style=flat-square) (仓库 / 中间件)
README
# koa-log4js
A wrapper for [log4js-node](https://github.com/nomiddlename/log4js-node) which support [Koa](https://github.com/koajs/koa) logger middleware.
Log message is forked from Express (Connect) logger [file](https://github.com/nomiddlename/log4js-node/blob/master/lib/connect-logger.js).## Note
This branch is use to [Koa v2.x](https://github.com/koajs/koa/tree/v2.x).
To use Koa v0.x & v1.x, please check the [master](https://github.com/dominhhai/koa-log4js/tree/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](https://github.com/koajs/koa/tree/v2.x)___
```
$ npm i --save koa-log4
```## Usage
Config koa-log4js is same as the original [log4js-node](https://github.com/nomiddlename/log4js-node).### Normal log4js way
This way is same as the original [log4js-node](https://github.com/nomiddlename/log4js-node).```javascript
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.```javascript
const log4js = require('koa-log4')
app.use(log4js.koaLogger(log4js.getLogger("http"), { level: 'auto' }))
```There are more configuration options:
```javascript
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](https://github.com/dominhhai/koa-log4js-example/tree/v2.x) for full example with `Koa v2`.## Others
See [here](https://github.com/nomiddlename/log4js-node) for more info.