Skip to content

Commit

Permalink
chore: Add LOG_ELAPSED_TIME to track global time in debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Nov 20, 2024
1 parent a70795b commit ca97c58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions yarn-project/foundation/src/config/env_var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type EnvVar =
| 'L1_CHAIN_ID'
| 'L1_PRIVATE_KEY'
| 'L2_QUEUE_SIZE'
| 'LOG_ELAPSED_TIME'
| 'LOG_JSON'
| 'LOG_LEVEL'
| 'MNEMONIC'
Expand Down
10 changes: 9 additions & 1 deletion yarn-project/foundation/src/log/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function getLogLevel() {

export let currentLevel = getLogLevel();

const logElapsedTime = ['1', 'true'].includes(process.env.LOG_ELAPSED_TIME ?? '');
const firstTimestamp: number = Date.now();

function filterNegativePatterns(debugString: string): string {
return debugString
.split(',')
Expand Down Expand Up @@ -141,7 +144,12 @@ function logWithDebug(debug: debug.Debugger, level: LogLevel, msg: string, data?

msg = data ? `${msg} ${fmtLogData(data)}` : msg;
if (debug.enabled && LogLevels.indexOf(level) <= LogLevels.indexOf(currentLevel)) {
debug('[%s] %s', level.toUpperCase(), msg);
if (logElapsedTime) {
const ts = ((Date.now() - firstTimestamp) / 1000).toFixed(3);
debug('%ss [%s] %s', ts, level.toUpperCase(), msg);
} else {
debug('[%s] %s', level.toUpperCase(), msg);
}
}
}

Expand Down

0 comments on commit ca97c58

Please sign in to comment.