1.0.4 • Published 5 months ago

@davidwells/smart-log v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Smart Logs

A utility package for intelligent logging.

Types of logs

  • Debug logs
    • These are instrumented through the code and kept in for prod but sit inert until DEBUG is set.
    • Useful in dev and prod
  • Runtime user facing logs
    • These are the user facing logs that show to users unless SILENT is set
  • Programmatic logs
    • These are the logs that CLIs can pipe out to downstream programs or AI tools

Features

  • Conditional logging based on command line flags
  • Support for JSON output format
  • Silent mode support

Installation

npm install @davidwells/smart-log

Usage

Basic Logging

const { _console } = require('@davidwells/smart-log')

// Basic logging
_console.log('Hello world') // Logs to stdout by default

// With --silent flag: No output
// With --json flag: Logs to stderr

JSON Logging

const { _console, logJson } = require('@davidwells/smart-log')

const data = { foo: 'bar', baz: 123 }

// Logs JSON only when --json flag is present
_console.json(data, 'Data:')

// Force JSON logging regardless of flags
logJson(data, 'Data:', true)

Command Line Flags

  • --json: Output logs in JSON format to stderr
  • --silent: Suppress all logging output
  • --debug: Scoped debug logging

Environment Variables

  • IS_TESTING_ENV: When set, logs are directed to stderr

API

_console(message, ...args)

Logs a message to stdout by default, or stderr when --json flag is present.

logJson(data, label, bypass)

Logs JSON data with optional label. Only logs when --json flag is present unless bypass is true.

If --json flag is set, the JSON logs will be output to stdout for usage with downstream tools like jq

logStdErr(message, ...args)

Directly log to stderr.

logStdOut(message, ...args)

Directly log to stdout.

Constants

  • JSON_FLAG: '--json'
  • SILENT_FLAG: '--silent'

Example

const { _console } = require('@davidwells/smart-log')

// Basic logging
_console.log('Starting process...')

// JSON logging
_console.json('User data:', {
  id: 123,
  name: 'John Doe'
})

License

MIT

Other logging utils

Future enhancements

1.0.2

5 months ago

1.0.1

6 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.0

2 years ago