npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

logbook

v0.7.1

Published

Log all data passing through process.stdout and process.stderr

Downloads

8

Readme

Logbook

A simple, unobtrusive logger for Node.

Nothing fancy - logs all data that passes through process.stdout (console.log) and process.stderr (console.error).

Features

  • Simple
    • Keep using console
  • Intercepts process.stdout and process.stderr producing two log levels:
    • LOG
    • ERR
  • Log to:
  • Use Logbook Plugins and log to:

Quick Usage

Install:

npm install --save logbook

Then include in your entry file:

require('logbook').configure({
  console: {
    enabled: false
  },
  smtp: {
    enabled: true,
    username: '[email protected]',
    password: '...',
    to: ["[email protected]", "[email protected]"]
  }
});

Send an email on crash:

process.on('uncaughtException', function(err) {
  console.error('Oh noez, I\'ve crashed!!!!\n' + (err.stack || err));
});

Log to File

Disable console and log stdout stderr to log.txt and err.txt with timestamps:

require('logbook').configure({
  console: {
    enabled: false
  },
  file: {
    enabled: true
  }
});

Log to Loggly (Gen2)

Disable console and send stdout and stderr to Loggly (200MB/day free):

require('logbook').configure({
  console: {
    log: false,
    err: false
  },
  loggly: {
    log: true,
    err: true,
    customerToken: "abcd1234-1234-40bd-bddf-5ff562eb1cda",
    tags: ["my-app"]
  }
});

Log to SMTP (Email)

require('logbook').configure({
  smtp: {
    log: true,
    err: true,
    username: '[email protected]',
    password: '...',
    to: ["[email protected]", "[email protected]"]
  }
});

Again, only stderr is logged by default.

Log to all the thingsss

require('logbook').configure({
  console: {
    log: true,
    err: true,
  },
  file: {
    log: true,
    err: true,
    ...
  },
  loggly: {
    log: true,
    err: true,
    ...
  },
  smtp: {
    log: true,
    err: true,
    ...
  }
});

More Examples

See the examples directory for more

Default Configuration

With this default configuration, logbook will have no effect, console will function as expected.

<runFile('./show-defaults')>

{
  "console": {
    "timestamps": false,
    "typestamps": false,
    "log": true,
    "err": true
  },
  "file": {
    "timestamps": true,
    "typestamps": false,
    "log": false,
    "err": false,
    "logPath": "./log.txt",
    "errPath": "./err.txt"
  },
  "loggly": {
    "customerToken": null,
    "maxSockets": 10,
    "machineName": false,
    "tags": null,
    "meta": null,
    "log": false,
    "err": false
  },
  "smtp": {
    "username": null,
    "password": null,
    "from": "node-logbook",
    "to": [],
    "subject": "node-logbook",
    "machineName": true,
    "delay": 10000,
    "log": false,
    "err": false
  },
  "xmpp": {
    "jid": null,
    "password": null,
    "host": "talk.google.com",
    "port": 5222,
    "to": "*",
    "prefix": null,
    "machineName": false,
    "delay": 100,
    "log": false,
    "err": false
  }
}

Plugins

Logbook plugins are just node modules that export a send method:

exports.send = function(type, buffer) {
  type === 'log' || type === 'err';   //true
  buffer instanceof Buffer;           //true
};

logbook's parent folder (usually node_modules), is searched for plugins (other folders) with the name logbook-my-plugin-name. For example, to install both logbook and logbook-my-plugin-name, we can do npm install --save logbook-my-plugin-name, which will now allow logbook to see logbook-my-plugin-name as it will reside along side it in node_modules.

Warning Using console.``log/error in your plugin will cause an infinite loop. Instead use:

var helper = require("../logbook").helper;
helper.info("my logbook console message");

You can define a default configuration with

exports.config = {
  my: "defaults"
};

You can also create a configure function (to verify configuration or init modules):

exports.configure = function() {
  // check exports.config
};

This function is called when the user calls require("logbook").configure({ }).

See the XMPP plugin for reference.

Custom Log Handlers

Add custom log handlers with add()

require('logbook').configure({
  ...
}).add(function(type, buffer) {
  type === 'log' || type === 'err';   //true
  buffer instanceof Buffer;           //true
});

API

Configure loggers with configure()

All loggers have two options:

log (Boolean)

Enable or disable log events

err (Boolean)

Enable or disable error events


File

See defaults


Loggly

See defaults

Important: Uses Loggly Gen2

Each log will be in the form

{
  type: "log|err",
  msg: "...",
  date: unix-epoch,
  tags: ["...", "...", "log|err"]
}

Note: tags and meta may contain functions. These functions will be evalutated once for each log event. The result value must be stringifiable otherwise it will be discarded.

customerToken (String)

The token provided by loggly which identifies your account.

tags (Array)

The tags applied each log event. See Tags.

The log type (logbook-log|logbook-err) will always be appended to the tags array. Tip: Since there are no longer input tokens, differentiate application logs with tags. Add a 'my-app' tag and you can search for all of it's errors with tag:logbook-err AND tag:my-app.

meta (Object)

An object that will be merged into the object above. Will not override existing values.

maxSockets (Number)

The number of http agents to use when connecting to Loggly. Can be thought of as amount of concurrency, so beware of hitting Loggly too much as you may be seen as DOS attack. Hence, default is only 10.


SMTP

See defaults

Uses Nodemailer

Important: All properties not in the defaults will be used to create the SMTP options object which will be passed into createTransport("SMTP", options).

username password

Shorthand for auth: { user: username, pass: password }. See SMTP options object.

to required

An array of email addresses

delay

This is the delay (in milliseconds) before all acculated messages are concatenated and sent. This helps to prevent performance loss by batching messages.


Conceptual Overview

This module simply assigns a new functions to process.stdout|err.write to intercept all console.log()s and console.error()s.

Todo

  • Make a CLI looking for a logbook.json file used as config
    • Runs in place of node, though logbook script.js logs all stdout stderr
    • Auto restarts, logging restart events
  • Add tests

<license()>

MIT License

Copyright © 2014 Jaime Pillora <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.