0.2.5 • Published 9 years ago

elecpen v0.2.5

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

ElecPen

Build Status npm version

We take logger as a function to receive a record and output to a stream, and elecPen is a simple logger creator.

API

elecPen(writable, prefix, dateFormat)

ParamDescription
writablesOne or an array of writable stream for logging
prefixThe prefix of the record
dateFormatDate format for logger or pass true to use the default format

Default logger

A set of logger is provided by a default logger creator which provide some useful logger like info or error.

Options

OptionTypeDescription
infoFilestring, functionFile name for logging info and verbose
errFilestring, functionFile name for logging error and warning
timestampstring, booleanDate format for logger or pass true to use the default format
appendbooleanIf file exists, append new entries to it instead of truncating
logToConsolebooleanOutput the message to console, it will be default to true in dev mode

Methods

  • logger.verbose
  • logger.info
  • logger.warning
  • logger.error

Example

You can you a set of default logger:

// use default logger
const opts = {
  infoFile: 'info.log',  // record info and verbose
  errFile: 'err.log', // record error and warning
  timestamp: true,
  append: true // default to true
}
const http = requrie('http')
const logger = require('elecpen').defaultLogger(opts)

http.createServer((req, res) => {
  logger.info('Recieve a request. Path: %s', req.path)
  res.end('Hello world.')
})
.listen(4000, _ => {
  logger.info('Server listening...')
})

Dynamic file name is supported, and it is useful to record log by separated file.

const opts = {
  infoFile () {
    const now = new Date()
    return `log-${now.getFullYear()}-${now.getMonth() + 1}`
  },
  errFile: 'err.log'
}
const logger = require('elecpen').defaultLogger(opts)
logger.info('Hello World!')

Or create you own logger:

const fs = require('fs')
const elecpen = require('elecpen')
const recorder = elecpen.streamRecorder()

const log = function (msg) {
  // dynamic stream
  const stream = recorder(
    _ => `log-${Date.now()}.log`,
    name => fs.createWriteStream(name, { flags: 'a' })
  )
  stream && elecpen(stream, 'Message', timestamp)(msg)
}
log('Hello World!')

License

MIT

0.2.5

9 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago