-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global log options to CLI (#3519)
### Description - Add global log options to CLI - Improve consistency with agent log options - Other minor log-related cleanup ### Related issues Fixes #3499 ### Backward compatibility Yes because the new log options haven't yet published
- Loading branch information
Showing
14 changed files
with
185 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hyperlane-xyz/cli': patch | ||
--- | ||
|
||
Add --log and --verbosity settings to CLI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
// Should be used instead of referencing process directly in case we don't | ||
// run in node.js | ||
export function safelyAccessEnvVar(name: string) { | ||
export function safelyAccessEnvVar(name: string, toLowerCase = false) { | ||
try { | ||
return process.env[name]; | ||
return toLowerCase ? process.env[name]?.toLowerCase() : process.env[name]; | ||
} catch (error) { | ||
return undefined; | ||
} | ||
} | ||
|
||
export function envVarToBoolean(value: any) { | ||
if (typeof value === 'boolean') return value; | ||
if (typeof value === 'string') return value.toLowerCase() === 'true'; | ||
if (typeof value === 'number') return value !== 0; | ||
return !!value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,98 @@ | ||
import { LevelWithSilent, pino } from 'pino'; | ||
|
||
import { envVarToBoolean, safelyAccessEnvVar } from './env'; | ||
|
||
let logLevel: LevelWithSilent = 'info'; | ||
const envLogLevel = safelyAccessEnvVar('LOG_LEVEL')?.toLowerCase(); | ||
if (envLogLevel && pino.levels.values[envLogLevel]) { | ||
logLevel = envLogLevel as LevelWithSilent; | ||
} | ||
// For backwards compat and also to match agent level options | ||
else if (envLogLevel === 'none' || envLogLevel === 'off') { | ||
logLevel = 'silent'; | ||
} | ||
|
||
export const isLogPretty = envVarToBoolean(safelyAccessEnvVar('LOG_PRETTY')); | ||
|
||
export const rootLogger = pino({ | ||
level: logLevel, | ||
name: 'hyperlane', | ||
formatters: { | ||
// Remove pino's default bindings of hostname but keep pid | ||
bindings: (defaultBindings) => ({ pid: defaultBindings.pid }), | ||
}, | ||
hooks: { | ||
logMethod(inputArgs, method, level) { | ||
// Pino has no simple way of setting custom log shapes and they | ||
// recommend against using pino-pretty in production so when | ||
// pretty is enabled we circumvent pino and log directly to console | ||
if (isLogPretty && level >= pino.levels.values[logLevel]) { | ||
// eslint-disable-next-line no-console | ||
console.log(...inputArgs); | ||
// Then return null to prevent pino from logging | ||
return null; | ||
} | ||
return method.apply(this, inputArgs); | ||
import { LevelWithSilent, Logger, pino } from 'pino'; | ||
|
||
import { safelyAccessEnvVar } from './env'; | ||
|
||
// Level and format here should correspond with the agent options as much as possible | ||
// https://docs.hyperlane.xyz/docs/operate/config-reference#logfmt | ||
|
||
// A custom enum definition because pino does not export an enum | ||
// and because we use 'off' instead of 'silent' to match the agent options | ||
export enum LogLevel { | ||
Debug = 'debug', | ||
Info = 'info', | ||
Warn = 'warn', | ||
Error = 'error', | ||
Off = 'off', | ||
} | ||
|
||
let logLevel: LevelWithSilent = | ||
toPinoLevel(safelyAccessEnvVar('LOG_LEVEL', true)) || 'info'; | ||
|
||
function toPinoLevel(level?: string): LevelWithSilent | undefined { | ||
if (level && pino.levels.values[level]) return level as LevelWithSilent; | ||
// For backwards compat and also to match agent level options | ||
else if (level === 'none' || level === 'off') return 'silent'; | ||
else return undefined; | ||
} | ||
|
||
export function getLogLevel() { | ||
return logLevel; | ||
} | ||
|
||
export enum LogFormat { | ||
Pretty = 'pretty', | ||
JSON = 'json', | ||
} | ||
let logFormat: LogFormat = LogFormat.JSON; | ||
const envLogFormat = safelyAccessEnvVar('LOG_FORMAT', true) as | ||
| LogFormat | ||
| undefined; | ||
if (envLogFormat && Object.values(LogFormat).includes(envLogFormat)) | ||
logFormat = envLogFormat; | ||
|
||
export function getLogFormat() { | ||
return logFormat; | ||
} | ||
|
||
// Note, for brevity and convenience, the rootLogger is exported directly | ||
export let rootLogger = createHyperlanePinoLogger(logLevel, logFormat); | ||
|
||
export function getRootLogger() { | ||
return rootLogger; | ||
} | ||
|
||
export function configureRootLogger( | ||
newLogFormat: LogFormat, | ||
newLogLevel: LogLevel, | ||
) { | ||
logFormat = newLogFormat; | ||
logLevel = toPinoLevel(newLogLevel) || logLevel; | ||
rootLogger = createHyperlanePinoLogger(logLevel, logFormat); | ||
return rootLogger; | ||
} | ||
|
||
export function setRootLogger(logger: Logger) { | ||
rootLogger = logger; | ||
return rootLogger; | ||
} | ||
|
||
export function createHyperlanePinoLogger( | ||
logLevel: LevelWithSilent, | ||
logFormat: LogFormat, | ||
) { | ||
return pino({ | ||
level: logLevel, | ||
name: 'hyperlane', | ||
formatters: { | ||
// Remove pino's default bindings of hostname but keep pid | ||
bindings: (defaultBindings) => ({ pid: defaultBindings.pid }), | ||
}, | ||
}, | ||
}); | ||
hooks: { | ||
logMethod(inputArgs, method, level) { | ||
// Pino has no simple way of setting custom log shapes and they | ||
// recommend against using pino-pretty in production so when | ||
// pretty is enabled we circumvent pino and log directly to console | ||
if ( | ||
logFormat === LogFormat.Pretty && | ||
level >= pino.levels.values[logLevel] | ||
) { | ||
// eslint-disable-next-line no-console | ||
console.log(...inputArgs); | ||
// Then return null to prevent pino from logging | ||
return null; | ||
} | ||
return method.apply(this, inputArgs); | ||
}, | ||
}, | ||
}); | ||
} |