1.0.3 • Published 5 years ago

write-line v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

write-line

npm package

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 that pm2 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 )

ParamTypeDescription
opts ObjectSet a JSON Object as configuration for write-line
opts StringSet a JSON file path as configuration for write-line

writeLine.getFactory()

writeLine.getWriter( name )

ParamTypeDescription
name StringA name of writer instance

writeLine.addLayout( name, serializerGenerator )

ParamTypeDescription
name StringA name of serializer generator
serializerGenerator Functionserialize content when you call function write

writeLine.addAppender( name, config )

ParamTypeDescription
name StringA name of writer instance
config ObjectA 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
}

Examples

Follow below links to learn more

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.3

6 years ago