Repository: microsoft/nni
File: ts/nni_manager/common/log.ts (TypeScript)

Code before:
        this.log(INFO, args);
    }

    public warning(...args: any[]): void {
        this.log(WARNING, args);
    }

    public error(...args: any[]): void {
        this.log(ERROR, args);
    }

    public critical(...args: any[]): void {
        this.log(CRITICAL, args);
    }

    public fatal(...args: any[]): void {
        this.log(FATAL, args);
    }

    private log(level: number, args: any[]): void {
        const logFile: Writable | undefined = (global as any).logFile;
        if (level < logLevel) {
            return;
        }

        const jsonTime = new Date().toJSON();
        const time = jsonTime.slice(0, 10) + ' ' + jsonTime.slice(11, 19);

        const levelName = levelNames.has(level) ? levelNames.get(level) : level.toString();

        const message = args.map(arg => (typeof arg === 'string' ? arg : util.inspect(arg))).join(' ');
        
        const record = `[${time}] ${levelName} (${this.name}) ${message}\n`;

        if (logFile === undefined) {
            console.log(record);
        } else {
            logFile.write(record);
        }
    }
}

export function getLogger(name: string = 'root'): Logger {
    let logger = loggers.get(name);
    if (logger === undefined) {
        logger = new Logger(name);
        loggers.set(name, logger);
    }
    return logger;
}


Review comment (suggestion):
This will print UTC time. In log we want local time.
The trick above has been tested in en_US and zh_CN locales, but there are hundreds of other locales over the world.
It would be nice if you can find a better method to do the converting.

Code after:
        this.log(INFO, args);
    }

    public warning(...args: any[]): void {
        this.log(WARNING, args);
    }

    public error(...args: any[]): void {
        this.log(ERROR, args);
    }

    public critical(...args: any[]): void {
        this.log(CRITICAL, args);
    }

    public fatal(...args: any[]): void {
        this.log(FATAL, args);
    }

    private log(level: number, args: any[]): void {
        const logFile: Writable | undefined = (global as any).logFile;
        if (level < logLevel) {
            return;
        }

        const zeroPad = (num: number): string => num.toString().padStart(2, '0');
        const now = new Date();
        const date = now.getFullYear() + '-' + zeroPad(now.getMonth() + 1) + '-' + zeroPad(now.getDate());
        const time = zeroPad(now.getHours()) + ':' + zeroPad(now.getMinutes()) + ':' + zeroPad(now.getSeconds());
        const datetime = date + ' ' + time;

        const levelName = levelNames.has(level) ? levelNames.get(level) : level.toString();

        const message = args.map(arg => (typeof arg === 'string' ? arg : util.inspect(arg))).join(' ');
        
        const record = `[${datetime}] ${levelName} (${this.name}) ${message}\n`;

        if (logFile === undefined) {
            console.log(record);
        } else {
            logFile.write(record);
        }
    }
}

export function getLogger(name: string = 'root'): Logger {
    let logger = loggers.get(name);
    if (logger === undefined) {
        logger = new Logger(name);
        loggers.set(name, logger);
    }