import { ServerExtType } from '@hapi/hapi' ; import Podium from '@hapi/podium' ; import { Config } from '../../../../legacy/server/config' ; import { setupLogging } from '../../../../legacy/server/logging' ; import { LogLevel , LogRecord } from '../../logging' ; import { LegacyVars } from '../../types' ; export const metadataSymbol = Symbol ( 'log message with metadata' ) ; export function attachMetaData ( message , metadata = { } ) { return { [ metadataSymbol ] : { message , metadata , } , } ; } const isEmptyObject = ( obj ) => Object . keys ( obj ) . length === 0 ; function getDataToLog ( error , metadata , message ) { if ( error ) return error ; if ( ! isEmptyObject ( metadata ) ) return attachMetaData ( message , metadata ) ; return message ; } interface PluginRegisterParams { plugin : { register : ( server : LegacyLoggingServer , options : PluginRegisterParams [ 'options' ] ) => Promise < void > ; } ; options : LegacyVars ; } param level function getLegacyLogLevel ( level ) { const logLevel = level . id . toLowerCase ( ) ; if ( logLevel === 'warn' ) { return 'warning' ; } if ( logLevel === 'trace' ) { return 'debug' ; } return logLevel ; } internal export class LegacyLoggingServer { public connections = [ ] ; public events : Podium = new Podium ( [ 'log' , 'request' , 'response' ] ) ; private onPostStopCallback ? : ( ) => void ; constructor ( legacyLoggingConfig ) { const config = { logging : { ... legacyLoggingConfig , events : { ... legacyLoggingConfig . events , ops : '__no-ops__' , } , } , ops : { interval : 2147483647 } , } ; setupLogging ( this , Config . withDefaultSchema ( config ) ) ; } public register ( { plugin : { register } , options } ) { return register ( this , options ) ; } public log ( { level , context , message , error , timestamp , meta = { } } ) { const { tags = [ ] , ... metadata } = meta ; this . events . emit ( 'log' , { data : getDataToLog ( error , metadata , message ) , tags : [ getLegacyLogLevel ( level ) , ... context . split ( '.' ) , ... tags ] , timestamp : timestamp . getTime ( ) , } ) ; } public stop ( ) { if ( this . onPostStopCallback !== undefined ) { this . onPostStopCallback ( ) ; } } public ext ( eventName , callback ) { if ( eventName === 'onPostStop' ) { this . onPostStopCallback = callback ; } } public expose ( ) { } }