@qvac/logging
v0.2.0
Published
`logging` wraps any logger you supply and normalizes the interface.
Readme
logging
logging wraps any logger you supply and normalizes the interface.
Installation
npm install @qvac/loggingUsage
Import
import { QvacLogger } from '@qvac/logging'Create a Logger Wrapper
// Wrap an existing logger instance:
const log = new QvacLogger(myLogger)- With a provided logger:
- If
logger.getLevel()exists, its return value (case-insensitive) is used. - Else if
logger.level()exists, its return (case-insensitive) is used. - Else if
logger.levelis a string, it’s used (case-insensitive). - If none yield a valid level, falls back to
DEFAULT_LEVEL("info").
- If
- Without a logger:
- The wrapper starts in
OFFmode and silences all messages.
- The wrapper starts in
Log Levels
Constants available on QvacLogger.LOG_LEVELS:
console.log(QvacLogger.LOG_LEVELS)
// {
// OFF: 'off',
// ERROR: 'error',
// WARN: 'warn',
// INFO: 'info',
// DEBUG: 'debug'
// }Logging Methods
log.error(...args)log.warn(...args)log.info(...args)log.debug(...args)
Each method forwards to the corresponding function on the wrapped logger only if the message’s level is at‑or‑above the active threshold.
Changing Log Level
Note that changing the log level on the wrapper does not change the level of the underlying logger and vice versa.
If you need to change the log level you need to call method on both the wrapper and the underlying logger.
For example:
import * as logLevel from 'loglevel'
import { QvacLogger } from '@qvac/logging'
logLevel.setLevel('warn')
const log = new QvacLogger(logLevel)
console.log(log.getLevel()) // 'warn'
log.info('Info here') // skipped
log.error('Critical!') // forwarded to logLevel.error
logLevel.setLevel('debug') // change logLevel level
log.setLevel('debug') // change wrapper level
console.log(log.getLevel()) // 'debug'
log.debug('Debugging now!') // forwarded to logLevel.debugQuickstart Example
import * as logLevel from 'loglevel'
import { QvacLogger } from '@qvac/logging'
logLevel.setLevel('warn')
const log = new QvacLogger(logLevel)
console.log(log.getLevel()) // 'warn'
log.info('Info here') // skipped
log.error('Critical!') // forwarded to logLevel.errorSDK Integration Example
import Qvac from '@qvac/sdk'
import { QvacLogger } from '@qvac/logging'
import * as logLevel from 'loglevel'
logLevel.setLevel('debug')
const log = new QvacLogger(logLevel)
const qvac = new Qvac({ logger: log })
await qvac.start()Development
The package is written in TypeScript under src/ and compiled to dist/ with tsc.
After cloning, run npm install (its prepare script builds dist/); run npm run build
to recompile, npm run typecheck to type-check, and npm run test:unit for the tests.
Testing
Run unit tests with:
npm run test:unit