Winston 自定义日志级别打字稿定义

Winston custom log levels typescript definitions

我正在开发节点应用程序并使用 Typescript。 我有温斯顿 3。 在我的代码中,我添加了自定义日志级别;

const myCustomLevels = {
    levels: {
        data: 10,
        protocol: 9,
        debug: 8,
        info: 7,
        notice: 6,
        note: 5,
        warn: 4,
        error: 3,
        crit: 2,
        alert: 1,
        emerg: 0,
    }
}

然后

const logger = winston.createLogger({
    level: data,
    levels: myCustomLevels.levels,
    format: winston.format.combine(
        winston.format.json()
    ),
    transports: [new winston.transports.Console()],
});

我需要帮助的问题是当我使用记录器时 Typescript 抱怨。

logger.protocol({});

在这种情况下,类型是 const logger: winston.Logger 并且 ts 表示 [ts] Property 'protocol' does not exist on type 'Logger'. [2339]。 Typescript 不知道我的水平。

如何更正此问题以便 tsc 知道我在记录器上的级别?

遗憾的是,当前编写的定义不允许自定义日志级别。最简单的解决方案是将返回的记录器转换为一个记录器和一个包含额外方法的类型的交集。我们可以使用映射类型 Recordkeyof

根据您的 const 声明创建此类型
import * as winston from 'winston'
const logger = winston.createLogger({
    level: data,
    levels: myCustomLevels.levels,
    format: winston.format.combine(
        winston.format.json()
    ),
    transports: [new winston.transports.Console()],
}) as winston.Logger & Record<keyof typeof myCustomLevels['levels'], winston.LeveledLogMethod>;

logger.protocol({})

请注意,我试过扩充现有的模块定义,但因为 createLogger 被声明为具有内联函数签名的变量,我们无法通过扩充来真正扩展它。

如果这对您来说是一个普遍问题,您可以编写一个保留级别的通用函数:

function createWinstonLogger<T>(options?: Pick<winston.LoggerOptions, Exclude<keyof winston.LoggerOptions, 'levels'>> & { levels: T }) {
    return winston.createLogger(options) as winston.Logger & Record<keyof T, winston.LeveledLogMethod>
}
const logger = createWinstonLogger({
    level: data,
    levels: myCustomLevels.levels,
    format: winston.format.combine(
        winston.format.json()
    ),
    transports: [new winston.transports.Console()],
});
logger.protocol({})