File

packages/server/src/modules/log4js/log4js.service.ts

Index

Properties
Methods

Constructor

constructor(options?: Configuration | string)
Parameters :
Name Type Optional
options Configuration | string Yes

Methods

debug
debug(message, context?: string)
Parameters :
Name Type Optional
message No
context string Yes
Returns : void
error
error(message, trace?: string, context?: string)
Parameters :
Name Type Optional
message No
trace string Yes
context string Yes
Returns : void
flushall
flushall(cb?: () => void)
Parameters :
Name Type Optional
cb function Yes
Returns : void
getLogger
getLogger(loggerName: string)
Parameters :
Name Type Optional Default value
loggerName string No "APP"
Returns : any
log
log(message, context?: string)
Parameters :
Name Type Optional
message No
context string Yes
Returns : void
warn
warn(message, context?: string)
Parameters :
Name Type Optional
message No
context string Yes
Returns : void

Properties

Private loggers
Type : Map<string | Logger>
import { getLogger, Logger, shutdown, configure, Configuration } from "log4js";
import { Inject, LoggerService, Injectable, Global } from "@nestjs/common";

import { LOG4JS_OPTION } from "./log4js.constants";
import { buildDefaultConfig } from "./log4js.default.config";

@Global()
@Injectable()
export class Log4jsService implements LoggerService {
    private loggers: Map<string, Logger>;
    constructor(@Inject(LOG4JS_OPTION) options?: Configuration | string) {
        this.loggers = new Map();
        if (typeof options === "string") {
            options = buildDefaultConfig(options);
        } else if (typeof options === "undefined") {
            options = buildDefaultConfig("all");
        }
        configure(options);
    }

    getLogger(loggerName = "APP") {
        let logger = this.loggers.get(loggerName);
        if (!logger) {
            logger = getLogger(loggerName);
            this.loggers.set(loggerName, logger);
        }
        return logger;
    }

    log(message: unknown, context?: string) {
        this.getLogger(context).info(message);
    }

    error(message: unknown, trace?: string, context?: string) {
        this.getLogger(context).error(message, trace);
    }

    warn(message: unknown, context?: string) {
        this.getLogger(context).warn(message);
    }

    debug(message: unknown, context?: string) {
        this.getLogger(context).debug(message);
    }

    flushall(cb?: () => void) {
        shutdown(() => {
            cb?.();
        });
    }
}

results matching ""

    No results matching ""