import { LogLevel , LogService } from "matrix-bot-sdk" ; import util from "util" ; import winston from "winston" ; type MsgType = string | Error | any | { error ? : string ; } ; export default class LogWrapper { public static configureLogging ( level ) { const log = winston . createLogger ( { level , transports : [ new winston . transports . Console ( { format : winston . format . combine ( winston . format . timestamp ( { format : "HH:mm:ss:SSS" , } ) , winston . format . printf ( ( info ) => { return `${ info . level . toUpperCase ( ) } ${ info . timestamp } [${ info . module }] ${ info . message }` ; } ) ) , } ) , ] , } ) ; const getMessageString = ( messageOrObject ) => { messageOrObject = messageOrObject . flat ( ) ; const messageParts = [ ] ; messageOrObject . forEach ( ( obj ) => { if ( typeof ( obj ) === "string" ) { messageParts . push ( obj ) ; return ; } messageParts . push ( util . inspect ( obj ) ) ; } ) ; return messageParts . join ( " " ) ; } ; LogService . setLogger ( { info : ( module , ... messageOrObject ) => { if ( module . startsWith ( "MatrixLiteClient" ) ) { log . debug ( getMessageString ( messageOrObject ) , { module } ) ; return ; } log . info ( getMessageString ( messageOrObject ) , { module } ) ; } , warn : ( module , ... messageOrObject ) => { const error = messageOrObject [ 0 ] . error || messageOrObject [ 1 ] ?. body ?. error ; if ( error === "Room account data not found" ) { log . debug ( getMessageString ( messageOrObject ) , { module } ) ; return ; } log . warn ( getMessageString ( messageOrObject ) , { module } ) ; } , error : ( module , ... messageOrObject ) => { const error = messageOrObject [ 0 ] . error || messageOrObject [ 1 ] ?. body ?. error ; if ( error === "Room account data not found" ) { log . debug ( getMessageString ( messageOrObject ) , { module } ) ; return ; } log . error ( getMessageString ( messageOrObject ) , { module } ) ; } , debug : ( module , ... messageOrObject ) => { log . debug ( getMessageString ( messageOrObject ) , { module } ) ; } , trace : ( module , ... messageOrObject ) => { log . verbose ( getMessageString ( messageOrObject ) , { module } ) ; } , } ) ; LogService . setLevel ( LogLevel . fromString ( level ) ) ; LogService . info ( "LogWrapper" , "Reconfigured logging" ) ; } constructor ( private module ) { } param { string } module param { * [ ] } messageOrObject public debug ( ... messageOrObject ) { LogService . debug ( this . module , ... messageOrObject ) ; } param { * [ ] } messageOrObject public error ( ... messageOrObject ) { LogService . error ( this . module , ... messageOrObject ) ; } param { * [ ] } messageOrObject public info ( ... messageOrObject ) { LogService . info ( this . module , ... messageOrObject ) ; } param { * [ ] } messageOrObject public warn ( ... messageOrObject ) { LogService . warn ( this . module , ... messageOrObject ) ; } }