debug module using logfmt format
npm install logger-base




- Based on the popular debug module.
- Lazy level evaluation used logs levels.
- Level support: info, warn & error based from RFC 5424.
- Message formatting Heroku logfmt syntax.
- Colorized output via DEBUG_COLORS by default.
- debug.duration for measurement.
- ๐ Enterprise-grade security: Zero vulnerabilities with automated security validation.
logger-base maintains the highest security standards:
- โ
Zero known vulnerabilities (npm audit clean)
- โ
Automated security validation (pre-publish security checks)
- โ
Secure dependency management (version overrides for vulnerable packages)
- โ
Comprehensive security documentation (SECURITY.md)
Run security validation: npm run security:validate
``bash`
$ npm install logger-base --save
Given a code like this one:
`js
const debug = require('logger-base')('metascraper')
debug('retry', { url: 'https://kikobeats.com' })
debug.info('done', { time: Date.now() })
debug.warn('token expired', { timestamp: Date.now() })
debug.error('whoops', { message: 'expected number, got NaN' })`
You can:
- Allow all the levels: DEBUG=logger-base*DEBUG=",-metascraper:info" node example.js
- Discard specific levels:
Sometimes you need to log the duration of a function:
`js
const { setTimeout } = require('timers/promises')
const debug = require('logger-base')('metascraper')
const duration = debug.duration()
setTimeout(1001).then(() => duration.error('timeout!'))
setTimeout(1100).then(() => duration.info('success'))
`
You can customize the logger behavior with options:
`js${Math.round(ms)}ms
const debug = require('logger-base')('myapp', {
// Custom log levels
levels: ['info', 'warn', 'error', 'fatal'],
// Disable colors
colors: false,
// Custom duration format
durationFormat: (ms) => ,[${namespace}]
// Custom prefix
prefix: (namespace) => ,`
// Custom encoding (e.g., for structured logging)
encode: (obj) => JSON.stringify(obj)
})
#### Using Custom Encoding
`js${key}="${String(value)}"
const debug = require('logger-base')('myapp', {
encode: (obj) => {
// Convert object to your preferred format
return Object.entries(obj)
.map(([key, value]) => )
.join(' ')
}
})
debug('user action', { userId: 123, action: 'login' })
// Output: user action userId="123" action="login"
`
#### Custom Duration Format
`js${ms}ms
const debug = require('logger-base')('myapp', {
durationFormat: (ms) => {
if (ms < 1000) return ${(ms / 1000).toFixed(2)}s
if (ms < 60000) return ${(ms / 60000).toFixed(2)}m
return
}
})
const duration = debug.duration('operation')
// After 1500ms: duration=1.50s
`
#### env
Required
Type: string
The env variable name to use for enabling logging using DEBUG.
#### options
Type: object{}
Default:
Configuration options for the debug logger.
##### levels
Type: array['info', 'warn', 'error']
Default:
The log levels available. Each level will be accessible as a method on the debug instance (e.g., debug.info(), debug.warn(), debug.error()).
##### encode
Type: function
Default: The default logfmt encoder
Custom encoding function for converting objects to logfmt format. The function receives an object and should return a logfmt-formatted string.
`js${key}=${obj[key]}
const debug = require('logger-base')('myapp', {
encode: (obj) => {
// Custom encoding logic
return Object.keys(obj).map(key => ).join(' ')`
}
})
##### colors
Type: booleantrue
Default: (unless DEBUG_COLORS=false in environment)
Enable or disable colored output. When disabled, log messages will not include ANSI color codes.
`js`
const debug = require('logger-base')('myapp', {
colors: false
})
##### durationFormat
Type: functionpretty-ms
Default: formatter
Custom format function for duration formatting. The function receives a number (milliseconds) and should return a formatted string.
`js${ms}ms
const debug = require('logger-base')('myapp', {
durationFormat: (ms) => {
return `
}
})
##### prefix
Type: function
Default: The default prefix formatter
Custom prefix function for formatting log messages. The function receives the namespace (string) and color code (number), and should return a formatted prefix string.
`js[${namespace}]
const debug = require('logger-base')('myapp', {
prefix: (namespace, color) => {
return `
}
})
It returns a function will print the duration in the next call.
`js`
const duration = debug.duration('query')
const result = await db.query(query)
duration(result)
logger-base is committed to maintaining the highest security standards. We implement comprehensive security measures including:
bash
Run comprehensive security validation
npm run security:validateAudit all dependencies
npm run security:auditRun all security checks (audit + validation)
npm run security:check
``logger-base ยฉ Kiko Beats, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.
> kikobeats.com ยท GitHub Kiko Beats ยท X @Kikobeats