1.0.4 • Published 5 months ago
@davidwells/smart-log v1.0.4
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-logUsage
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 stderrJSON 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
- Defer logs https://www.npmjs.com/package/console-watch
- https://github.com/mikaelvesavuori/mikrolog
- https://github.com/Kikobeats/debug-logfmt/
- Fast redact https://github.com/davidmarkclements/fast-redact
- https://github.com/DavidWells/redact-logs
Future enhancements
- Log buffering https://www.andmore.dev/blog/log-buffering/
- Redaction https://github.com/DavidWells/redact-logs