import Transport from 'winston-transport' ; import { format , transports } from 'winston' ; import { ILoggerProvider } from './ILoggerProvider' ; import _ from 'lodash' ; export default class BaseLoggerProvider implements ILoggerProvider { getTransports ( isProd ) { const { combine , timestamp , printf } = format ; const consoleTransportFormatters = [ timestamp ( ) , printf ( ( options ) => { const time = options . timestamp ; const msg = options . message ; let level = options . level . toUpperCase ( ) ; let user = options ?. user ?. email || '' ; let location = options . location || '' ; let formattedMsg ; if ( isProd ) { const clusterName = options . cluster || '' ; const datastoreType = options . datastoreType || '' ; formattedMsg = JSON . stringify ( { time , level , user , location , cluster : clusterName , datastoreType , message : msg , } ) ; } else { level = _ . padStart ( level , 5 ) ; user = this . truncateAndPad ( user , 24 , false ) ; location = this . truncateAndPad ( location , 30 , true ) ; formattedMsg = `${ time } [${ level }] [${ user }] [${ location }] ${ msg }` ; } return formattedMsg ; } ) , ] ; return [ new transports . Console ( { format : combine ( ... consoleTransportFormatters ) , } ) , ] ; } param msg param padding param ellipsisAtStart private private truncateAndPad ( msg , padding , ellipsisAtStart = false ) { let result = msg ; if ( msg . length >= padding ) { if ( ellipsisAtStart ) { result = `...${ msg . slice ( - padding + 3 ) }` ; } else { result = _ . truncate ( msg , { length : padding } ) ; } } result = _ . padStart ( result , padding ) ; return result ; } }