import { serializeError } from "serialize-error" ; export enum LogLevel { DEBUG = 0 , ERROR , NONE } export interface Logger { debug ( message ) : void ; error ( message ) : void ; } let logger = console ; let logLevel = LogLevel . ERROR ; export function setLogger ( customLogger ) { logger = customLogger ; } export function setLogLevel ( level ) { logLevel = level ; } export function getLogLevel ( ) { return logLevel ; } export function logDebug ( message , metadata ? , error ? ) { if ( logLevel > LogLevel . DEBUG ) { return ; } emitLog ( logger . debug , "debug" , message , metadata , error ) ; } export function logError ( message , metadata ? , error ? ) { if ( logLevel > LogLevel . ERROR ) { return ; } emitLog ( logger . error , "error" , message , metadata , error ) ; } function emitLog ( outputter , status , message , metadata ? , error ? ) { message = `datadog:${ message }` ; let output = { status , message } ; if ( metadata instanceof Error && error === undefined ) { error = metadata ; metadata = undefined ; } if ( metadata !== undefined ) { output = { ... output , ... metadata } ; } if ( error !== undefined ) { const errorInfo = serializeError ( error ) ; output = { ... output , ... errorInfo } ; } outputter ( JSON . stringify ( output ) ) ; }