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

hapi-logger

v2.1.0

Published

A Hapi plugin for writing logs via bunyan

Downloads

39

Readme

hapi-logger

Build Status Coverage Status Code Climate NPM version Dependency Status

NPM

A Hapi plugin for writing logs to stdout via bunyan

Bunyan is a great module for logging, but I like the flexibility that having tagged log messages gives me over your traditional log levels. Luckily Hapi already emits log events with tags, so we can add those tags to our log message. The current implementation of this module logs every message as "info" as far as bunyan is concerned, so you might as well ignore the "level" property on the log message. However, all the tags are included on the message, which should give enough context about the message.

Contributing

This module makes use of a Makefile for building/testing purposes. After obtaining a copy of the repo, run the following commands to make sure everything is in working condition before you start your work:

make install
make test

Before committing a change to your fork/branch, run the following commands to make sure nothing is broken:

make test
make test-cov

Don't forget to bump the version in the package.json using the semver spec as a guide for which part to bump. Submit a pull request when your work is complete.

Notes:

  • Please do your best to ensure the code coverage does not drop. If new unit tests are required to maintain the same level of coverage, please include those in your pull request.
  • Please follow the same coding/formatting practices that have been established in the module.

Installation

npm install hapi-logger

Usage

To install this plugin on your Hapi server, do something similar to this:

var Hapi = require('hapi');
var server = new Hapi.Server();

var hapiLoggerConfig = {};

server.register({ register: require('hapi-logger'), options: hapiLoggerConfig }, function(err) {
	if (err) {
		console.log('error', 'Failed loading plugin: hapi-logger');
	}
});

Plugin Options

name

The name of your application, or any other name you wish to have contained within your log message, for this pack of servers. Defaults to 'hapi-logger'

src

A flag that tells bunyan whether or not to include the location from where the log message originiated. Don't set this to true in production! Defaults to false

tags

The tags used to determine when a log message is logged. This value can contain both strings and arrays of strings. The items in the top level of the collection are OR'd, while any items in nested arrays are AND'd.

If the collection includes '*' then all messages will be logged. Defaults to ['*']

Examples

  1. Top-level strings only:
[
	'error',
	'info'
]

Means that log messages with either the tag 'error' OR 'info' will get logged.

  1. Nested strings only:
[
	[
		'error',
		'request'
	]
]

Means that log messages with the tags 'error' AND 'request' will get logged.

  1. Top-level and nested strings:
[
	'info',
	[
		'error',
		'request'
	]
]

Means that log messages with either the tag 'info' OR the tags 'error' AND 'request' will get logged.

ignoredTags

The tags used to determine when a log message is NOT logged. This value can contain both strings and arrays of strings. The items in the top level of the collection are OR'd, while any items in nested arrays are AND'd.

If the collection includes '*' then no messages will be logged. Defaults to []

Note: If a log message matches any of these tags, it will not be logged, even if it would have been because it matched values in tags.

Examples

  1. Top-level strings only:
[
	'error',
	'info'
]

Means that log messages with either the tag 'error' OR 'info' will not get logged.

  1. Nested strings only:
[
	[
		'error',
		'request'
	]
]

Means that log messages with the tags 'error' AND 'request' will not get logged.

  1. Top-level and nested strings:
[
	'info',
	[
		'error',
		'request'
	]
]

Means that log messages with either the tag 'info' OR the tags 'error' AND 'request' will not get logged.

shouldLog

A function that will receive the log data as a parameter and should return true if the message should be logged, or false if the message should not be logged. The tags and ignoredTags are evaulated first then this function will be executed. Defaults to null

Example

A Hapi route configured like this:

server.route({
	method: 'GET',
	path: '/test/{param}',
	handler: function(request, reply) {
		request.log(['get', 'testResource'], 'Some important info...');
		reply('Success!');
	}
});

would cause the following log message to be written (in addition to any other internal Hapi-related events) when a request is issued to the route:

{"name":"hapi-logger","hostname":"Me","pid":54705,"level":30,"tags":["get","testResource"],"req":{"id":"1408481983531-54705-47711","headers":{"user-agent":"curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5","host":"localhost:8080","accept":"*/*"},"method":"get","info":{"received":1408481983531,"remoteAddress":"127.0.0.1","remotePort":63014,"referrer":"","host":"localhost:8080"},"path":"/test/1234"},"msg":"Some important info...","time":"2014-08-19T20:59:43.542Z","v":0}

In addition to user-initiated request log events, this module will also listen for server log events, request events, and internalError events, and log those if not filtered by the configured tags or ignoredTags.

To provide a little more context about a log message, you can log messages like so:

server.log(['mytag'], { message: 'My log message', other: 'Some other data' });

By passing an object as the second paramter, you can pass along context with your message. If no message is detected a default message will be used. Here are the default messages for the corresponding Hapi events:

  • log: "Hapi Server Log"
  • request: "Hapi Server Request Log"

If an internalError event is received, then the log message will be the error message.

Version Compatibility

Currently compatible with: Hapi 16.x.x

  • 0.1.x - Hapi 6.x.x
  • 0.2.x - Hapi 7.x.x
  • 0.3.x - Hapi 8.x.x
  • 0.4.x - Hapi 11.x.x
  • 1.0.x - Hapi 16.x.x
  • 2.0.x - Hapi 16.x.x

License

The MIT License (MIT)