1.0.0 • Published 4 years ago

@jrh-works/log v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
4 years ago

@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

NameTypeDescription
optionsObject: OptionsConfigures the loggers which are generated by the factory function.

Returns

TypeDescription
FunctionA factory function which can be used to create loggers.

Exceptions

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


The Options Object

PropertyTypeDescription
applicationStringThe name of the application.
keyStringThe LogDNA API key.
modeStringThe mode of the running application (i.e. production). When the mode is set to testing, remote logs are disabled.
sourceStringThe source of the log line.
fetchFunction: 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

NameTypeDescription
nameStringA name which will be prepended to output from this logger.

Returns

TypeDescription
FunctionA 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

NameTypeDescription
messageStringA message to log.
metadataObject (optional)Metadata to be included in the logging output.
notesObject: Notes (optional)Notes to append to the log message.

Returns

TypeDescription
Object: PromiseA 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

AttributeTypeDescription
noteBooleanA 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

NameTypeDescription
messageOrErrorString | ErrorAn error message or error object to be logged.
metadataObject (optional)Metadata to be included in the logging output.

Returns

TypeDescription
Object: PromiseA 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

NameTypeDescription
requestObject: HTTP RequestA standard Node.js HTTP Request object.

Returns

TypeDescription
Object: PromiseA 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

NameTypeDescription
nameStringA name which will be added to the previous logger's name.

Returns

TypeDescription
FunctionA logging function.

Exceptions

Throws a standard Error if a name is not present.

1.0.0

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago