Giter Club home page Giter Club logo

elecpen's Introduction

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)

Param Description
writables One or an array of writable stream for logging
prefix The prefix of the record
dateFormat Date 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

Option Type Description
infoFile string, function File name for logging info and verbose
errFile string, function File name for logging error and warning
timestamp string, boolean Date format for logger or pass true to use the default format
append boolean If file exists, append new entries to it instead of truncating
logToConsole boolean Output 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

elecpen's People

Contributors

leozdgao avatar

Stargazers

Roger avatar

Watchers

James Cloos avatar  avatar

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.