1.0.3 • Published 5 years ago
write-line v1.0.3
write-line
Note: wrap streamroller that roll over when they reach a maximum size, or a date/time.
PS: if you are deploying your node server with pm2, you have to install
pm2-intercom
like thatpm2 install pm2-intercom
如果该插件对您的开发有所帮助,请五星好评哦!^^ ^^ ^_^
Table of contents
Installation
npm install --save write-line
# or
cnpm install --save write-line
Usage
const Koa = require('koa');
const writeLine = require('write-line');
// './write-line.json' will turn into `${process.cwd()}/write-line.json`
writeLine.configure('./write-line.json');
const writer = writeLine.getWriter('app');
const app = new Koa();
app.use((ctx) => {
writer.write({
'user-agent': ctx.headers['user-agent'],
'remote-addr': ctx.ip,
method: ctx.method,
url: ctx.href || ctx.originalUrl || ctx.url,
status: ctx.status
});
});
const server = app.listen(3000);
server.on('listening', () => {
console.log('The server is running at port 3000');
});
writeLine.configure( opts )
Param | Type | Description |
---|---|---|
opts | Object | Set a JSON Object as configuration for write-line |
opts | String | Set a JSON file path as configuration for write-line |
writeLine.getFactory()
writeLine.getWriter( name )
Param | Type | Description |
---|---|---|
name | String | A name of writer instance |
writeLine.addLayout( name, serializerGenerator )
Param | Type | Description |
---|---|---|
name | String | A name of serializer generator |
serializerGenerator | Function | serialize content when you call function write |
writeLine.addAppender( name, config )
Param | Type | Description |
---|---|---|
name | String | A name of writer instance |
config | Object | A options of new appender |
How to configure:
{
"appenders": {
"http": { // writeLine.getWriter('http'), Any name if you want
"type": "dateFile", // file or fileSync or dateFile
"filename": "./logs/http.log",
"pattern": "yyyy-MM-dd", // It is available when the type was set "dateFile"
"layout": {
"type": "stringify" // if you write a JSON string
},
"daysToKeep": 2, // if you wanna back up two files
"keepFileExt": true, // if set false, "http.log.yyyy-MM-dd" or "http.log.1"
"alwaysIncludePattern": true, // if you want the filename to includes pattern like "http.yyyy-MM-dd.log"
}
},
"pm2": true, // work in pm2 environment
"pm2InstanceVar": "NODE_APP_INSTANCE" // default NODE_APP_INSTANCE
}