@@ -75,52 +75,64 @@ export function createHyperlanePinoLogger(
7575 logLevel : LevelWithSilent ,
7676 logFormat : LogFormat ,
7777) {
78+ const createFallbackLogger = ( ) =>
79+ pino ( {
80+ level : logLevel ,
81+ name : 'hyperlane' ,
82+ formatters : {
83+ // Remove pino's default bindings of hostname but keep pid
84+ bindings : ( defaultBindings ) => ( { pid : defaultBindings . pid } ) ,
85+ } ,
86+ hooks : {
87+ logMethod ( inputArgs , method , level ) {
88+ // Pino has no simple way of setting custom log shapes and they
89+ // recommend against using pino-pretty in production so when
90+ // pretty is enabled we circumvent pino and log directly to console
91+ if (
92+ logFormat === LogFormat . Pretty &&
93+ level >= pino . levels . values [ logLevel ]
94+ ) {
95+ // eslint-disable-next-line no-console
96+ console . log ( ...inputArgs ) ;
97+ // Then return null to prevent pino from logging
98+ return null ;
99+ }
100+ return method . apply ( this , inputArgs ) ;
101+ } ,
102+ } ,
103+ } ) ;
104+
78105 // In development, pino-pretty is used for a better dev experience,
79106 // but only if the log format is 'pretty'. This allows for JSON logs
80107 // in development as well if explicitly configured.
81108 if (
82109 process . env . NODE_ENV === 'development' &&
83110 logFormat === LogFormat . Pretty
84111 ) {
85- return pino ( {
86- level : logLevel ,
87- transport : {
88- target : 'pino-pretty' ,
89- options : {
90- colorize : true ,
91- translateTime : 'SYS:standard' ,
92- ignore : 'pid,hostname' ,
112+ try {
113+ return pino ( {
114+ level : logLevel ,
115+ transport : {
116+ target : 'pino-pretty' ,
117+ options : {
118+ colorize : true ,
119+ translateTime : 'SYS:standard' ,
120+ ignore : 'pid,hostname' ,
121+ } ,
93122 } ,
94- } ,
95- } ) ;
123+ } ) ;
124+ } catch ( err ) {
125+ const fallbackLogger = createFallbackLogger ( ) ;
126+ fallbackLogger . warn (
127+ err ,
128+ 'Could not initialize pino-pretty, falling back to built-in pretty logger' ,
129+ ) ;
130+ return fallbackLogger ;
131+ }
96132 }
97133
98134 // In production (or other envs), use the original hook-based logger
99- return pino ( {
100- level : logLevel ,
101- name : 'hyperlane' ,
102- formatters : {
103- // Remove pino's default bindings of hostname but keep pid
104- bindings : ( defaultBindings ) => ( { pid : defaultBindings . pid } ) ,
105- } ,
106- hooks : {
107- logMethod ( inputArgs , method , level ) {
108- // Pino has no simple way of setting custom log shapes and they
109- // recommend against using pino-pretty in production so when
110- // pretty is enabled we circumvent pino and log directly to console
111- if (
112- logFormat === LogFormat . Pretty &&
113- level >= pino . levels . values [ logLevel ]
114- ) {
115- // eslint-disable-next-line no-console
116- console . log ( ...inputArgs ) ;
117- // Then return null to prevent pino from logging
118- return null ;
119- }
120- return method . apply ( this , inputArgs ) ;
121- } ,
122- } ,
123- } ) ;
135+ return createFallbackLogger ( ) ;
124136}
125137
126138export function ethersBigNumberSerializer ( key : string , value : any ) : any {
0 commit comments