Using Winston in typescript

I cannot figure out how to use the Winston registration module in typescript. I have an error when trying to set the log level, and one more when trying to register an error:

import * as logger from "winston";

logger.level = 'debug';
// [ts] Cannot assign to 'level' because it is a constant or a read-only property.

logger.error(new Error('test'));
// [ts] Argument of type 'Error' is not assignable to parameter of type 'string'.

I added both winstonand to my project @types/winston.


Edit: to complete Joshua's answer, it seems that by default winston logs ... is nowhere. You must add the transport for it to work:

import * as logger from "winston";

logger.configure({
    level: 'debug',
    transports: [
        new logger.transports.Console({
            colorize: true
        })
    ]
});
+4
source share
1 answer

Here are the type definitions for Winston:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/winston/index.d.ts

, const, level , const, :

declare const winston: winston.Winston;
export = winston;

, , configure ( , logger import Winston:

logger.configure({
    level: 'verbose',
    ...
})

( , configure), LoggerInstance, .

, Error, string. Winston.info :

info: LeveledLogMethod;

LeveledLogMethod:

interface LeveledLogMethod {
    (msg: string, callback: LogCallback): LoggerInstance;
    (msg: string, meta: any, callback: LogCallback): LoggerInstance;
    (msg: string, ...meta: any[]): LoggerInstance;
}
+4

Source: https://habr.com/ru/post/1681716/


All Articles