Logging utility with different log levels with timestamp options.
-
Installation
Install the Logger package using npm:
npm install swiftlet-log
Install the Logger package using yarn:
yarn add swiftlet-log
Install the Logger package using pnpm:
pnpm add swiftlet-log
-
Import and Create Logger Instance
Import the Logger class and create an instance:
import { Logger, LogLevel } from 'swiftlet-log' const logger = new Logger({ level: LogLevel.DEBUG, timestamp: true })
-
Log Messages
Use the different log level methods to log messages:
logger.debug('This is a debug message.') logger.info('This is an info message.') logger.warning('This is a warning message.') logger.error('This is an error message.') logger.trace('This is a trace message.') logger.fatal('This is a fatal message.')
-
Middleware
import { Logger, LogLevel, LogMiddleware } from 'swiftlet-log' const logger = new Logger({ level: LogLevel.DEBUG, timestamp: true }) const customPrefixMiddleware: LogMiddleware = (ctx, next) => { ctx.message = `[CUSTOM PREFIX] ${ctx.message}` next(ctx.message, ctx.level) } logger.use(customPrefixMiddleware) logger.info('This is an info message.') // 输出: [CUSTOM PREFIX] [INFO]: This is an info message.
-
Customization
-
Adjust log level:
logger.setLogLevel(LogLevel.INFO)
-
Enable or disable timestamp:
logger.enableTimestamp() logger.disableTimestamp()
-
Add or remove log listeners:
const customListener: LogListener = (level, message) => { // Your custom log listener logic } logger.addLogListener(customListener) logger.removeLogListener(customListener)
DEBUG
: Detailed debugging information.INFO
: General information about system operation.WARNING
: Indicates a potential problem.ERROR
: Indicates a more serious problem.TRACE
: Very detailed tracing information.FATAL
: A very severe error that may lead to application termination.