import is from '@sindresorhus/is' ; import * as bunyan from 'bunyan' ; import { nanoid } from 'nanoid' ; import cmdSerializer from './cmd-serializer' ; import configSerializer from './config-serializer' ; import errSerializer from './err-serializer' ; import { RenovateStream } from './pretty-stdout' ; import type { BunyanRecord , Logger } from './types' ; import { ProblemStream , withSanitizer } from './utils' ; let logContext = process . env . LOG_CONTEXT ?? nanoid ( ) ; let curMeta = { } ; const problems = new ProblemStream ( ) ; const stdout = { name : 'stdout' , level : ( process . env . LOG_LEVEL as bunyan . LogLevel ) || 'info' , stream : process . stdout , } ; if ( process . env . LOG_FORMAT !== 'json' ) { const prettyStdOut = new RenovateStream ( ) as any ; prettyStdOut . pipe ( process . stdout ) ; stdout . stream = prettyStdOut ; stdout . type = 'raw' ; } const bunyanLogger = bunyan . createLogger ( { name : 'renovate' , serializers : { body : configSerializer , cmd : cmdSerializer , config : configSerializer , migratedConfig : configSerializer , originalConfig : configSerializer , presetConfig : configSerializer , oldConfig : configSerializer , newConfig : configSerializer , err : errSerializer , } , streams : [ stdout , { name : 'problems' , level : 'warn' as bunyan . LogLevel , stream : problems as any , type : 'raw' , } , ] . map ( withSanitizer ) , } ) ; const logFactory = ( level ) : any => ( p1 , p2 ) : void => { if ( p2 ) { bunyanLogger [ level ] ( { logContext , ... curMeta , ... p1 } , p2 ) ; } else if ( is . string ( p1 ) ) { bunyanLogger [ level ] ( { logContext , ... curMeta } , p1 ) ; } else { bunyanLogger [ level ] ( { logContext , ... curMeta , ... p1 } ) ; } } ; const loggerLevels = [ 'trace' , 'debug' , 'info' , 'warn' , 'error' , 'fatal' , ] ; export const logger = { } as any ; loggerLevels . forEach ( ( loggerLevel ) => { logger [ loggerLevel ] = logFactory ( loggerLevel ) ; } ) ; export function setContext ( value ) { logContext = value ; } export function getContext ( ) { return logContext ; } export function setMeta ( obj ) { curMeta = { ... obj } ; } export function addMeta ( obj ) { curMeta = { ... curMeta , ... obj } ; } export function removeMeta ( fields ) { Object . keys ( curMeta ) . forEach ( ( key ) => { if ( fields . includes ( key ) ) { delete curMeta [ key ] ; } } ) ; } export function addStream ( stream ) { bunyanLogger . addStream ( withSanitizer ( stream ) ) ; } export function levels ( name , level ) { bunyanLogger . levels ( name , level ) ; } export function getProblems ( ) { return problems . getProblems ( ) ; } export function clearProblems ( ) { return problems . clearProblems ( ) ; }