Logger

Tạo log

createLogger()

Tạo logger tùy chỉnh.

Chữ ký

  • createLogger(options: LoggerOptions)

Kiểu

interface LoggerOptions
  extends Omit<winston.LoggerOptions, 'transports' | 'format'> {
  dirname?: string;
  filename?: string;
  format?: 'logfmt' | 'json' | 'delimiter' | 'console' | winston.Logform.Format;
  transports?: ('console' | 'file' | 'dailyRotateFile' | winston.transport)[];
}

Thông tin chi tiết

Thuộc tínhMô tả
dirnameThư mục xuất log
filenameTên file log
formatĐịnh dạng log
transportsPhương thức xuất log

createSystemLogger()

Tạo log vận hành hệ thống được in theo cách quy định. Tham khảo Log - Log hệ thống.

Chữ ký

  • createSystemLogger(options: SystemLoggerOptions)

Kiểu

export interface SystemLoggerOptions extends LoggerOptions {
  seperateError?: boolean; // print error seperately, default true
}

Thông tin chi tiết

Thuộc tínhMô tả
seperateErrorCó xuất log mức error riêng hay không

requestLogger()

Middleware log request và response của API.

app.use(requestLogger(app.name));

Chữ ký

  • requestLogger(appName: string, options?: RequestLoggerOptions): MiddewareType

Kiểu

export interface RequestLoggerOptions extends LoggerOptions {
  skip?: (ctx?: any) => Promise<boolean>;
  requestWhitelist?: string[];
  responseWhitelist?: string[];
}

Thông tin chi tiết

Thuộc tínhKiểuMô tảGiá trị mặc định
skip(ctx?: any) => Promise<boolean>Bỏ qua một số log request theo ngữ cảnh-
requestWhiteliststring[]Whitelist thông tin request được in log[ 'action', 'header.x-role', 'header.x-hostname', 'header.x-timezone', 'header.x-locale','header.x-authenticator', 'header.x-data-source', 'referer']
responseWhiteliststring[]Whitelist thông tin response được in log['status']

app.createLogger()

Định nghĩa

class Application {
  createLogger(options: LoggerOptions) {
    const { dirname } = options;
    return createLogger({
      ...options,
      dirname: getLoggerFilePath(this.name || 'main', dirname || ''),
    });
  }
}

Khi dirname là đường dẫn tương đối, file log sẽ được xuất vào thư mục có tên là tên ứng dụng hiện tại.

plugin.createLogger()

Cách dùng giống app.createLogger().

Định nghĩa

class Plugin {
  createLogger(options: LoggerOptions) {
    return this.app.createLogger(options);
  }
}

Cấu hình log

getLoggerLevel()

getLoggerLevel(): 'debug' | 'info' | 'warn' | 'error'

Lấy mức log đã cấu hình của hệ thống hiện tại.

getLoggerFilePath()

getLoggerFilePath(...paths: string[]): string

Lấy đường dẫn ghép dựa trên thư mục log đã cấu hình của hệ thống hiện tại.

getLoggerTransports()

getLoggerTransports(): ('console' | 'file' | 'dailyRotateFile')[]

Lấy phương thức xuất log đã cấu hình của hệ thống hiện tại.

getLoggerFormat()

getLoggerFormat(): 'logfmt' | 'json' | 'delimiter' | 'console'

Lấy định dạng log đã cấu hình của hệ thống hiện tại.

Xuất log

Transports

Các phương thức xuất có sẵn.

  • Transports.console
  • Transports.file
  • Transports.dailyRotateFile
import { Transports } from '@nocobase/logger';

const transport = Transports.console({
  //...
});

Tài liệu liên quan