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

tp-logger

v1.1.0

Published

Supplement log messages with a Timestamp and caller's Path, with options for more.

Downloads

5

Readme

tp-logger

Build Status Dependency Status Test Coverage Code Climate

Supplement log messages with a Timestamp and caller's Path, with options for more.

Note: v1.0 broke support for Node.js version < 4.0; v1.1 brings the support back, recommended for maximum compatibility.

Install

$ npm install -save tp-logger

Usage

var logger = require('tp-logger')(/* optional options, see Options below */);

// Behaves the same as default console, where you can use string formatting
logger.log('lol: %d hours', 2);
// [2015-12-16T06:49:08+00:00][vanilla.js] lol: 2 hours

logger.info('dota');
// [2015-12-16T06:49:08+00:00][vanilla.js] dota

logger.warn('cs:go'); // added since 1.0
// [2015-12-16T06:49:08+00:00][vanilla.js] cs:go

logger.error(new Error('helldivers').stack);
// [2015-12-16T06:49:08+00:00][vanilla.js] Error: helldivers
//     at Object.<anonymous> (/Users/woozyking/proj/personal/tp-logger/examples/vanilla.js:9:14)
//     at Module._compile (module.js:425:26)
//     at Object.Module._extensions..js (module.js:432:10)
//     at Module.load (module.js:356:32)
//     at Function.Module._load (module.js:313:12)
//     at Function.Module.runMain (module.js:457:10)
//     at startup (node.js:138:18)
//     at node.js:974:3

If you don't mind your coworkers wondering why node.js console has suddenly become so awesome (or bad), you can also do:

// NOT Recommended
console = require('tp-logger')(/* optional options, see Options below */);
// ...

~~Since 1.0~~ Since 1.1 the underlying implementation has changed to an ~~ES6~~ ES5 style class that extends the Node.js native Class: Console, making it simpler to extend. See Class: TPLogger section for details.

var TPLogger = require('tp-logger').TPLogger;
var logger = new TPLogger(/* optional options, see Options below */);

console.log(logger instanceof TPLogger); // true
console.log(logger instanceof require('console').Console); // true

logger.logOnce({
  fullPath: true // change on the fly behavior
}, 'Log Message');
// [2015-12-16T06:49:08+00:00][/full/path/to/class.js] Log Message

logger.meta({
  pid: true // also supports on the fly opt change
});
// [2015-12-16T06:49:08+00:00][class.js][12345]

class TPLogger2 extends TPLogger {
  // your implementation, etc
}

Since 0.3 If you use express.js, you can also use the middleware provided to log errors passed on by request handlers in a centralize fashion:

var app = require('express')();
var loggerMw = require('tp-logger').middleware(/* same optional options supported, see Options below */);

app.get('/', function(req, res, next) {
  // handling request
  // ...
  // something went wrong, a wild error appeared
  return next(error);
});

// plug in our logger middleware to log handler passed errors
app.use(loggerMw);

Options

See them in action by running this example

var logger = require('tp-logger')({
  // stream for log() and info(). Not applicable for on the fly `Once` methods
  stdout: <writable stream instances. Default: process.stdout>,

  // stream for warn() and error(). Not applicable for on the fly `Once` methods
  stderr: <writable stream instances. Default: process.stderr>,

  // whether to use local time or UTC time
  local: <boolean. Default: false>,
  localTs: <Alias of 'local'. Since 0.2>,

  // timestamp format
  tsFormat: <string. See moment.js formatting documentation>,

  // whether to log full path of the caller or just caller filename
  fullPath: <boolean. Default: false. Since 0.2>,

  // whether to log with a supplement of [log|info|err]
  // enforced to true when stdout == stderr
  logType: <boolean. Default: true when stdout == stderr. Since 0.2>,

  // include process.pid
  pid: <boolean. Default: false. Since 1.0>
});

Class: TPLogger

Subclass of native Class: Console. Since 1.0.

new TPLogger([opt])

Creates a new TPLogger instance by passing optional options opt, see Options section for all available options.

logger.meta([opt][, method])

Generates meta info as string. Used as supplement before log messages. Since 1.0. Supports all Options except opt.stdout and opt.stderr. The method argument is only useful when opt.logType is true, or both constructor _opt.stdout and _opt.stderr point to the same writable stream.

logger.log|info|warn|error(...)

Behaves exactly like native Console, plus meta method supplied supplement before intended log messages.

logger.logOnce|infoOnce|warnOnce|errorOnce(opt, ...)

Behaves like TPLogger#log|info|warn|error, but the first argument must be an object and treated as on the fly options to override constructor options behaviors (opt.stdout and opt.stderr are ignored).