Skip to content

Logger

The Logger middleware takes care of injecting logs statement for each incoming request. It additionally injects MDC variables to the logger via request context. Here is an example of what a log message looks like

Interface

typescript
// the log message interface
export declare type Log = {
  mdc: {
    requestId: string
    accountId: string
  }
  message: string
  source: string
  line: number
  column: number
  name: string
  logLevel: string
  timestamp: string
  args: Record<string, unknown> | any
}

// the log levels
export enum LogLevel {
  DEBUG,
  INFO,
  WARN,
  ERROR,
}

// the logger interface
export declare interface Logger {
  debug: (message: string, ...rest: Record<string, unknown>[]) => void
  info: (message: string, ...rest: Record<string, unknown>[]) => void
  warn: (message: string, ...rest: Record<string, unknown>[]) => void
  error: (message: string, ...rest: Record<string, unknown>[]) => void
}

Usage

Here is how you can use the logger.

typescript
import { Logger, LoggerFactory } from '@coderpunktech/codedraw-core'

const logger: Logger = LoggerFactory.getLogger(context)

logger.debug('a message' , {foo: 'bar'})

You can set the logger level when installing the sdk. Only logs with a level that is equal or higher than the configured level will be printed. All of the codedraw packages are using the same logger under the hood. When you set the log level to debug the codedraw packages will logs will also be printed giving you a detailed description of what's happening under the hood.