import { Container , Logger , LoggerOptions as WinstonLoggerOptions , transports } from 'winston' ; import { kibana , local } from './format' ; const format = process . env . VCAP_SERVICES ? kibana : local ; const loggerReference = 'sap-cloud-sdk-logger' ; const exceptionLoggerId = 'sap-cloud-sdk-exception-logger' ; const container = new Container ( ) ; const exceptionTransport = new transports . Console ( ) ; const customLogLevels = { } ; const DEFAULT_LOGGER__MESSAGE_CONTEXT = '__DEFAULT_LOGGER__MESSAGE_CONTEXT' ; let silent = false ; const moduleLogger = createLogger ( { package : 'util' , messageContext : 'cloud-sdk-logger' } ) ; function toggleMuteLoggers ( silence ) { silent = silence ; container . loggers . forEach ( logger => toggleSilenceTransports ( logger , silence ) ) ; } function toggleSilenceTransports ( logger , silence ) { logger . transports . forEach ( transport => ( transport . silent = silence ) ) ; } /**
 * Mute all logger output created by the SAP Cloud SDK Logger. This also applies to future loggers created. Useful for tests.
 */ export function muteLoggers ( ) { toggleMuteLoggers ( true ) ; } /**
 * Unmute all logger output created by the SAP Cloud SDK Logger. This also applies to future loggers created. Useful for tests.
 */ export function unmuteLoggers ( ) { toggleMuteLoggers ( false ) ; } /**
 * Default logger for the SAP Cloud SDK for unhandled exceptions.
 */ export const cloudSdkExceptionLogger = container . get ( exceptionLoggerId , { defaultMeta : { logger : loggerReference , test : 'exception' } , format , exceptionHandlers : [ exceptionTransport ] } ) ; /**
 * Disable logging of exceptions. Enabled by default.
 */ export function disableExceptionLogger ( ) { cloudSdkExceptionLogger . exceptions . unhandle ( ) ; } /**
 * Enable logging of exceptions. Enabled by default.
 */ export function enableExceptionLogger ( ) { disableExceptionLogger ( ) ; cloudSdkExceptionLogger . exceptions . handle ( exceptionTransport ) ; } param messageContext returns export function createLogger ( messageContext ? ) { const customFields = typeof messageContext === 'string' ? { messageContext } : { ... messageContext } ; const logger = container . get ( customFields . messageContext , { level : customLogLevels [ customFields . messageContext ] || customFields . level || container . options . level || 'info' , defaultMeta : { ... ( Object . entries ( customFields ) . length && { custom_fields : customFields } ) , logger : customFields . logger || loggerReference } , format , transports : [ new transports . Console ( ) ] } ) ; toggleSilenceTransports ( logger , silent ) ; return logger ; } param messageContext returns export function getLogger ( messageContext = DEFAULT_LOGGER__MESSAGE_CONTEXT ) { if ( container . has ( messageContext ) ) { return container . get ( messageContext ) ; } } param level param messageContextOrLogger export function setLogLevel ( level , messageContextOrLogger = DEFAULT_LOGGER__MESSAGE_CONTEXT ) { const messageContext = typeof messageContextOrLogger === 'string' ? messageContextOrLogger : getMessageContext ( messageContextOrLogger ) ; if ( messageContext ) { customLogLevels [ messageContext ] = level ; if ( container . has ( messageContext ) ) { const logger = container . get ( messageContext ) ; logger . level = level ; } } else if ( typeof messageContextOrLogger !== 'string' ) { moduleLogger . warn ( 'Setting log level for logger with unknown message context' ) ; messageContextOrLogger . level = level ; } } param level export function setGlobalLogLevel ( level ) { container . options . level = level ; container . loggers . forEach ( logger => { logger . level = level ; } ) ; } export function getGlobalLogLevel ( ) { return container . options . level ; } function getMessageContext ( logger ) { const loggerOptions = logger as WinstonLoggerOptions ; if ( loggerOptions && loggerOptions . defaultMeta && loggerOptions . defaultMeta . custom_fields ) { return loggerOptions . defaultMeta . custom_fields . messageContext ; } } /**
 * Npm log levels used for the SAP Cloud SDK logger.
 */ export type LogLevel = 'error' | 'warn' | 'info' | 'verbose' | 'debug' | 'silly' ; /**
 * Configurable logger options.
 */ export interface LoggerOptions { level ? : LogLevel ; logger ? : string ; } /**
 * Log message context for a logger with additional custom data.
 */ export interface MessageContextObj { messageContext ? : string ; [ key ] : any ; }