Skip to content

Commit

Permalink
feat: JSON logging
Browse files Browse the repository at this point in the history
Adds a LOG_JSON env var that disables the foundation logger and replaces
it with a winston json logger.
  • Loading branch information
spalladino committed Aug 21, 2024
1 parent d86577c commit b53e240
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
8 changes: 7 additions & 1 deletion yarn-project/aztec/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createPXERpcServer } from '@aztec/pxe';
import { Command } from 'commander';
import http from 'http';

import { setupConsoleJsonLog } from '../logging.js';
import { createSandbox } from '../sandbox.js';
import { github, splash } from '../splash.js';
import { aztecStartOptions } from './aztec_start_options.js';
Expand Down Expand Up @@ -36,6 +37,11 @@ export function injectAztecCommands(program: Command, userLog: LogFn, debugLogge
startCmd.helpInformation = printAztecStartHelpText;

startCmd.action(async options => {
// setup json logging
if (['1', 'true', 'TRUE'].includes(process.env.LOG_JSON ?? '')) {
setupConsoleJsonLog();
}

// list of 'stop' functions to call when process ends
const signalHandlers: Array<() => Promise<void>> = [];
let services: ServerList = [];
Expand Down Expand Up @@ -112,7 +118,7 @@ export function injectAztecCommands(program: Command, userLog: LogFn, debugLogge

const httpServer = http.createServer(app.callback());
httpServer.listen(options.port);
userLog(`Aztec Server listening on port ${options.port}`);
debugLogger.info(`Aztec Server listening on port ${options.port}`);
}
});

Expand Down
29 changes: 24 additions & 5 deletions yarn-project/aztec/src/logging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onLog } from '@aztec/foundation/log';
import { onLog, setLevel } from '@aztec/foundation/log';

import * as path from 'path';
import * as process from 'process';
Expand All @@ -10,7 +10,7 @@ const CURRENT_LOG_FILE_NAME = 'aztec.debug.log';
const LOG_DIR = 'log';

/** Creates a winston logger that logs everything to a local rotating file */
function createWinstonLogger() {
function createWinstonLocalFileLogger() {
// See https://www.npmjs.com/package/winston-daily-rotate-file#user-content-options
const transport: DailyRotateFile = new DailyRotateFile({
filename: 'aztec-%DATE%.debug.log',
Expand All @@ -30,15 +30,34 @@ function createWinstonLogger() {
});
}

/** Creates a winston logger that logs everything to stdout in json format */
function createWinstonJsonStdoutLogger() {
return winston.createLogger({
level: process.env.LOG_LEVEL ?? 'info',
transports: [new winston.transports.Console({ format: format.combine(format.timestamp(), format.json()) })],
});
}

/**
* Hooks to all log statements and outputs them to a local rotating file.
* @returns Output log name.
*/
export function setupFileDebugLog() {
const logger = createWinstonLogger();
onLog((level, namespace, message, data) => {
logger.log({ ...data, level, namespace, message });
const logger = createWinstonLocalFileLogger();
onLog((level, module, message, data) => {
logger.log({ ...data, level, module, message });
});
const workdir = process.env.HOST_WORKDIR ?? process.cwd();
return path.join(workdir, LOG_DIR, CURRENT_LOG_FILE_NAME);
}

/**
* Silences the foundation stdout logger and funnels all logs through a winston JSON logger.
*/
export function setupConsoleJsonLog() {
const logger = createWinstonJsonStdoutLogger();
setLevel('silent');
onLog((level, module, message, data) => {
logger.log({ ...data, level, module, message });
});
}
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 @@ -78,6 +78,7 @@ export type EnvVar =
| 'PROVER_TEST_DELAY_MS'
| 'TX_PROVIDER_NODE_URL'
| 'TXE_PORT'
| 'LOG_JSON'
| 'BOT_PXE_URL'
| 'BOT_PRIVATE_KEY'
| 'BOT_RECIPIENT_ENCRYPTION_SECRET'
Expand Down
7 changes: 6 additions & 1 deletion yarn-project/foundation/src/log/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const DefaultLogLevel = process.env.NODE_ENV === 'test' ? ('silent' as const) :
export type LogLevel = (typeof LogLevels)[number];

const envLogLevel = process.env.LOG_LEVEL?.toLowerCase() as LogLevel;
export const currentLevel = LogLevels.includes(envLogLevel) ? envLogLevel : DefaultLogLevel;
export let currentLevel = LogLevels.includes(envLogLevel) ? envLogLevel : DefaultLogLevel;

const namespaces = process.env.DEBUG ?? 'aztec:*';
debug.enable(namespaces);
Expand Down Expand Up @@ -65,6 +65,11 @@ export function onLog(handler: LogHandler) {
logHandlers.push(handler);
}

/** Overrides current log level. */
export function setLevel(level: LogLevel) {
currentLevel = level;
}

/**
* Logs args to npm debug if enabled or log level is debug, console.error otherwise.
* @param debug - Instance of npm debug.
Expand Down

0 comments on commit b53e240

Please sign in to comment.