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

@jrh-works/log

v1.0.0

Published

Combined local and remote logging for Node.js applications.

Downloads

5

Readme

@jrh-works/log

Combined local and remote logging (via LogDNA) for Node.js applications.

Installation

npm install @jrh-works/log

The Configuration Function

Usage

const configureLogger = require('@jrh-works/log')

Syntax

configureLogger(options)

Arguments

| Name | Type | Description | | :-- | :-- | :-- | | options | Object: Options | Configures the loggers which are generated by the factory function. |

Returns

| Type | Description | | :-- | :-- | | Function | A factory function which can be used to create loggers. |

Exceptions

Throws a standard Error if all configuration options are not present.


The Options Object

| Property | Type | Description | | :-- | :-- | :-- | | application | String | The name of the application. | | key | String | The LogDNA API key. | | mode | String | The mode of the running application (i.e. production). When the mode is set to testing, remote logs are disabled. | | source | String | The source of the log line. | | fetch | Function: Fetch (optional) | A fetch implementation to be used for remote requests. Defaults to node-fetch. |


The Factory Function

Usage

const createLogger = require('@jrh-works/log')(options)
const log = createLogger('logger-one')

Syntax

createLogger(name)

Arguments

| Name | Type | Description | | :-- | :-- | :-- | | name | String | A name which will be prepended to output from this logger. |

Returns

| Type | Description | | :-- | :-- | | Function | A logging function. |

Exceptions

Throws a standard Error if a name is not present.

The Logging Function

Syntax

log(message, [metadata], notes)

Example Usage

const createLogger = require('@jrh-works/log')(options)
const log = createLogger('logger-one')

await log('Hello world!')

Arguments

| Name | Type | Description | | :-- | :-- | :-- | | message | String | A message to log. | | metadata | Object (optional) | Metadata to be included in the logging output. | | notes | Object: Notes (optional) | Notes to append to the log message. |

Returns

| Type | Description | | :-- | :-- | | Object: Promise | A preconfigured call to fetch. |

Exceptions

Throws a standard Error if called without a message.

Effects

  • Logs will appear in the local console and in LogDNA.
  • The log level will be info.

The Notes Object

| Attribute | Type | Description | | :-- | :-- | :-- | | [note] | Boolean | A note to be appended to the log message if the value is true. |

Example Usage
await log('My message.', null, {
  'dry run': true,
  'limit: 1': true,
  'production mode': false
})

// Logs:
// "My message. (dry run, limit: 1)"

Logging Errors

Syntax

log.error(messageOrError, [metadata])

Example Usage

const createLogger = require('@jrh-works/log')(options)
const log = createLogger('logger-two')

await log.error(new Error())

Arguments

| Name | Type | Description | | :-- | :-- | :-- | | messageOrError | String | Error | An error message or error object to be logged. | | metadata | Object (optional) | Metadata to be included in the logging output. |

Returns

| Type | Description | | :-- | :-- | | Object: Promise | A preconfigured call to fetch. |

Exceptions

Throws a standard Error if no message or error is present.

Effects

  • Logs will appear in the local console and in LogDNA.
  • When given a string, the error message will be logged with optional metadata.
  • When given an error object, the error message will be logged. The error object and stacktrace will be included in the metadata.
  • In all cases, the log level will be error.

Logging HTTP Requests

Usage

const createLogger = require('@jrh-works/log')(options)

// An example serverless function.
module.exports = async (request, response) => {
  const log = createLogger('logger-two')
  await log.request(request)
}

Syntax

log.request(request)

Arguments

| Name | Type | Description | | :-- | :-- | :-- | | request | Object: HTTP Request | A standard Node.js HTTP Request object. |

Returns

| Type | Description | | :-- | :-- | | Object: Promise | A preconfigured call to fetch. |

Exceptions

Throws a standard Error if no request is present.

Effects

  • Logs will appear in the local console and in LogDNA.
  • The URL and HTTP method will be logged (GET /hello) as the message, with the request body as metadata.
  • The log level will be info.

Composing Loggers

Syntax

log.append()

Arguments

| Name | Type | Description | | :-- | :-- | :-- | | name | String | A name which will be added to the previous logger's name. |

Returns

| Type | Description | | :-- | :-- | | Function | A logging function. |

Exceptions

Throws a standard Error if a name is not present.