swiftlet-log
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

swiftlet-log

Logging utility with different log levels with timestamp options.

Usage

  1. 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
  2. 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 })
  3. 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.')
  4. 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.
  5. 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)

Log Levels

  • 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.

Package Sidebar

Install

npm i swiftlet-log

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

11.8 kB

Total Files

7

Last publish

Collaborators

  • gavinbirkhoff