2.0.0 • Published 7 months ago
@capybaracode/logger v2.0.0
logger
Simple logging
Usage
There are two ways to use the logger. The first is to use the default settings and the second is to use the custom settings. However, in both configurations, the logger module monitors the environment variable below.
Environment | Default | Description |
---|---|---|
LOG_LEVEL | info | Values accepted:off - disabledtrace - all logsdebug - only debug, info, warn and error logsinfo - only info, warn and error logswarn - only the warn and error logserror - only the error logs |
LOG_FILE_DIR | logs | Path where the logs will be saved |
LOG_FILE_MODE | off | Values accepted:off - disabledjoin - all logs together in a single file.split - split logs into files |
LOG_CONSOLE_MODE | on | Values accepted:off - disabledon - log enabledlocal - pretty log for local terminal |
Using
Simple Use
import logger from '@letsdelivery/logger'
logger.trace('Log level trace')
// TRACE {"event":"Log level trace","location":"file:///index.ts:2:"}
logger.debug({ data: 'Log level debug' })
// DEBUG {"event":{"data":"Log level debug"},"location":"file:///index.ts:3:"}
logger.info(['log level info'])
// INFO {"event":["log level info"],"location":"file:///index.ts:4:"}
logger.warn(new Error('Warn'))
// WARN {"event":{},"location":"file:///index.ts:5:"}
logger.error(new URL('https://error.log'))
// ERROR {"event":"https://error.log/","location":"file:///index.ts:6:"}
Using an Identifier
If you want to add an identifier to your log, you can set the requestId in the global log, from then on all new logs will be displayed with this identifier.
import logger from '@letsdelivery/logger'
logger.setRequestId('myrequestid')
logger.info(['log level info'])
// INFO myrequestid {"event":["log level info"],"location":"file:///index.ts:4:"}
for additional identifiers use metadata by key and value
import logger from '@letsdelivery/logger'
logger.setMetadata({ id: 'myrequestid' })
logger.info(['log level info'])
// INFO {"event":["log level info"],"metadata":{"id":"myrequestid"},"location":"file:///index.ts:4:"}