docs/docs/en/api/logger/logger.md
createLogger()Creates a custom logger.
createLogger(options: LoggerOptions)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)[];
}
| Property | Description |
|---|---|
dirname | Log output directory |
filename | Log file name |
format | Log format |
transports | Log output method |
createSystemLogger()Creates system runtime logs printed in a specified method. Refer to Logger - System Log
createSystemLogger(options: SystemLoggerOptions)export interface SystemLoggerOptions extends LoggerOptions {
seperateError?: boolean; // print error seperately, default true
}
| Property | Description |
|---|---|
seperateError | Whether to output error level logs separately |
requestLogger()Middleware for API request and response logging.
app.use(requestLogger(app.name));
requestLogger(appName: string, options?: RequestLoggerOptions): MiddewareTypeexport interface RequestLoggerOptions extends LoggerOptions {
skip?: (ctx?: any) => Promise<boolean>;
requestWhitelist?: string[];
responseWhitelist?: string[];
}
| Property | Type | Description | Default |
|---|---|---|---|
skip | (ctx?: any) => Promise<boolean> | Skips logging for certain requests based on the request context. | - |
requestWhitelist | string[] | Whitelist of request information to be printed in the log. | [ 'action', 'header.x-role', 'header.x-hostname', 'header.x-timezone', 'header.x-locale','header.x-authenticator', 'header.x-data-source', 'referer'] |
responseWhitelist | string[] | Whitelist of response information to be printed in the log. | ['status'] |
class Application {
createLogger(options: LoggerOptions) {
const { dirname } = options;
return createLogger({
...options,
dirname: getLoggerFilePath(this.name || 'main', dirname || ''),
});
}
}
When dirname is a relative path, the log files will be output to the directory named after the current application.
Usage is the same as app.createLogger().
class Plugin {
createLogger(options: LoggerOptions) {
return this.app.createLogger(options);
}
}
getLoggerLevel(): 'debug' | 'info' | 'warn' | 'error'
Gets the log level currently configured in the system.
getLoggerFilePath(...paths: string[]): string
Concatenates directory paths based on the log directory currently configured in the system.
getLoggerTransports(): ('console' | 'file' | 'dailyRotateFile')[]
Gets the log output methods currently configured in the system.
getLoggerFormat(): 'logfmt' | 'json' | 'delimiter' | 'console'
Gets the log format currently configured in the system.
Predefined output methods.
Transports.consoleTransports.fileTransports.dailyRotateFileimport { Transports } from '@nocobase/logger';
const transport = Transports.console({
//...
});