import Logger , { levelFromName } from "bunyan" ; import bformat from "bunyan-format" ; import filteringHttpLogsStream from "../util/filteringHttpLogsStream" ; import { LoggerWithTarget , wrapLogger } from "probot/lib/wrap-logger" ; import { Request } from "express" ; const outputMode = process . env . MICROS_ENV ? "json" : "short" ; export const FILTERING_FRONTEND_HTTP_LOGS_MIDDLEWARE_NAME = "frontend-log-middleware" ; const LOG_STREAM = filteringHttpLogsStream ( FILTERING_FRONTEND_HTTP_LOGS_MIDDLEWARE_NAME , bformat ( { outputMode , levelInString : true } ) ) ; const requestSerializer = ( req ) => ( ! req || ! req . socket ) ? req : { method : req . method , url : req . originalUrl || req . url , path : req . path , headers : req . headers , remoteAddress : req . socket . remoteAddress , remotePort : req . socket . remotePort , body : req . body } ; const errorSerializer = ( err ) => ( ! err || ! err . stack ) ? err : { ... err , response : Logger . stdSerializers . res ( err . response ) , request : requestSerializer ( err . request ) , stack : getFullErrorStack ( err ) } ; const getFullErrorStack = ( ex ) => { let ret = ex . stack || ex . toString ( ) ; if ( ex . cause && typeof ( ex . cause ) === "function" ) { const cex = ex . cause ( ) ; if ( cex ) { ret += "\nCaused by: " + getFullErrorStack ( cex ) ; } } return ret ; } ; const logLevel = process . env . LOG_LEVEL || "info" ; const globalLoggingLevel = levelFromName [ logLevel ] || Logger . INFO ; const logger = wrapLogger ( Logger . createLogger ( { name : "root-logger" , stream : LOG_STREAM , level : globalLoggingLevel , serializers : { err : errorSerializer , res : Logger . stdSerializers . res , req : requestSerializer , } } ) ) ; export const overrideProbotLoggingMethods = ( probotLogger ) => { probotLogger . streams . pop ( ) ; probotLogger . addStream ( { type : "stream" , stream : LOG_STREAM , closeOnExit : false , level : globalLoggingLevel } ) ; } ; export const getLogger = ( name ) : LoggerWithTarget => { return logger . child ( { name } ) ; } ; const consoleLogger = getLogger ( "console" ) ; console . debug = consoleLogger . debug . bind ( consoleLogger ) ; console . error = consoleLogger . error . bind ( consoleLogger ) ; console . log = consoleLogger . info . bind ( consoleLogger ) ; console . warn = consoleLogger . warn . bind ( consoleLogger ) ; export default logger ;