Skip to content

Commit

Permalink
Clean up cli logger args
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Sep 12, 2022
1 parent 80a1437 commit b5882f0
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 192 deletions.
5 changes: 3 additions & 2 deletions packages/cli/src/cmds/beacon/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise<vo
mkdir(beaconPaths.dbDir);

const abortController = new AbortController();
const logger = getCliLogger(args, beaconPaths, config);
const logger = getCliLogger(args, {defaultLogFile: "beacon.log"}, config);

onGracefulShutdown(async () => {
abortController.abort();
Expand All @@ -49,10 +49,11 @@ export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise<vo
const metricsRegistries: Registry[] = [];
const db = new BeaconDb({
config,
controller: new LevelDbController(options.db, {logger: logger.child({module: "db"})}),
controller: new LevelDbController(options.db, {metrics: null}),
});

await db.start();
logger.info("Connected to LevelDB database", {path: options.db.name});

// BeaconNode setup
try {
Expand Down
69 changes: 6 additions & 63 deletions packages/cli/src/cmds/beacon/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Options} from "yargs";
import {logFormats, LogLevel, LogLevels} from "@lodestar/utils";
import {beaconNodeOptions, paramsOptions, IBeaconNodeArgs} from "../../options/index.js";
import {defaultLogMaxFiles, ICliCommandOptions, ILogArgs} from "../../util/index.js";
import {logOptions} from "../../options/logOptions.js";
import {ICliCommandOptions, ILogArgs} from "../../util/index.js";
import {defaultBeaconPaths, IBeaconPaths} from "./paths.js";

interface IBeaconExtraArgs {
Expand All @@ -12,6 +12,10 @@ interface IBeaconExtraArgs {
checkpointSyncUrl?: string;
checkpointState?: string;
wssCheckpoint?: string;
beaconDir?: string;
dbDir?: string;
persistInvalidSszObjectsDir?: string;
peerStoreDir?: string;
}

export const beaconExtraOptions: ICliCommandOptions<IBeaconExtraArgs> = {
Expand Down Expand Up @@ -58,61 +62,7 @@ export const beaconExtraOptions: ICliCommandOptions<IBeaconExtraArgs> = {
type: "string",
group: "weak subjectivity",
},
};

export const logOptions: ICliCommandOptions<ILogArgs> = {
logLevel: {
choices: LogLevels,
description: "Logging verbosity level for emittings logs to terminal",
defaultDescription: LogLevel.info,
type: "string",
},

logFileLevel: {
choices: LogLevels,
description: "Logging verbosity level for emittings logs to file",
defaultDescription: LogLevel.debug,
type: "string",
},

logFileDailyRotate: {
description:
"Daily rotate log files, set to an integer to limit the file count, set to 0(zero) to disable rotation",
defaultDescription: defaultLogMaxFiles.toString(),
default: defaultLogMaxFiles,
type: "number",
},

logFormatGenesisTime: {
hidden: true,
description:
"Use epoch slot timestamp format, instead or regular timestamp. Must provide genesisTime to compute relative time",
type: "number",
},

logPrefix: {
hidden: true,
description: "Logger prefix module field with a string ID",
type: "string",
},

logFormat: {
hidden: true,
description: "Log format used when emitting logs to the terminal and / or file",
choices: logFormats,
type: "string",
},

logLevelModule: {
hidden: true,
description: "Set log level for a specific module by name: 'chain=debug' or 'network=debug,chain=debug'",
type: "array",
string: true,
coerce: (args: string[]) => args.map((item) => item.split(",")).flat(1),
},
};

export const beaconPathsOptions: ICliCommandOptions<IBeaconPaths> = {
beaconDir: {
description: "Beacon root directory",
defaultDescription: defaultBeaconPaths.beaconDir,
Expand Down Expand Up @@ -140,12 +90,6 @@ export const beaconPathsOptions: ICliCommandOptions<IBeaconPaths> = {
defaultDescription: defaultBeaconPaths.peerStoreDir,
type: "string",
},

logFile: {
description: "Path to output all logs to a persistent log file, use 'none' to disable",
defaultDescription: defaultBeaconPaths.logFile,
type: "string",
},
};

interface IENRArgs {
Expand Down Expand Up @@ -204,7 +148,6 @@ export type IBeaconArgs = IBeaconExtraArgs & ILogArgs & IBeaconPaths & IBeaconNo
export const beaconOptions: {[k: string]: Options} = {
...beaconExtraOptions,
...logOptions,
...beaconPathsOptions,
...beaconNodeOptions,
...paramsOptions,
...enrOptions,
Expand Down
16 changes: 10 additions & 6 deletions packages/cli/src/cmds/beacon/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import path from "node:path";
import {IGlobalArgs} from "../../options/index.js";
import {getGlobalPaths, IGlobalPaths} from "../../paths/global.js";

export type BeaconPaths = Partial<{
beaconDir: string;
peerStoreDir: string;
dbDir: string;
persistInvalidSszObjectsDir: string;
}>;

export interface IBeaconPaths {
beaconDir: string;
peerStoreDir: string;
dbDir: string;
persistInvalidSszObjectsDir: string;
logFile?: string;
}

/**
Expand All @@ -23,11 +29,11 @@ export interface IBeaconPaths {
* └── beacon.log
* ```
*/
// Using Pick<IGlobalArgs, "dataDir"> make changes in IGlobalArgs throw a type error here
export function getBeaconPaths(
args: Partial<IBeaconPaths> & Pick<IGlobalArgs, "dataDir">,
// Using Pick<IGlobalArgs, "dataDir"> make changes in IGlobalArgs throw a type error here
args: BeaconPaths & Pick<IGlobalArgs, "dataDir">,
network: string
): IBeaconPaths & IGlobalPaths {
): IGlobalPaths & Required<BeaconPaths> {
// Compute global paths first
const globalPaths = getGlobalPaths(args, network);

Expand All @@ -36,15 +42,13 @@ export function getBeaconPaths(
const dbDir = args.dbDir ?? path.join(beaconDir, "chain-db");
const persistInvalidSszObjectsDir = args.persistInvalidSszObjectsDir ?? path.join(beaconDir, "invalidSszObjects");
const peerStoreDir = args.peerStoreDir ?? path.join(beaconDir, "peerstore");
const logFile = args.logFile?.trim() !== "none" ? args.logFile ?? path.join(dataDir, "beacon.log") : undefined;

return {
...globalPaths,
beaconDir,
dbDir,
persistInvalidSszObjectsDir,
peerStoreDir,
logFile,
};
}

Expand Down
9 changes: 2 additions & 7 deletions packages/cli/src/cmds/lightclient/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import {fromHexString} from "@chainsafe/ssz";
import {getBeaconConfigFromArgs} from "../../config/beaconParams.js";
import {IGlobalArgs} from "../../options/index.js";
import {getCliLogger} from "../../util/index.js";
import {getBeaconPaths} from "../beacon/paths.js";
import {ILightClientArgs} from "./options.js";
import {getLightclientPaths} from "./paths.js";

export async function lightclientHandler(args: ILightClientArgs & IGlobalArgs): Promise<void> {
const {config, network} = getBeaconConfigFromArgs(args);
const {config} = getBeaconConfigFromArgs(args);

const beaconPaths = getBeaconPaths(args, network);
const lightclientPaths = getLightclientPaths(args, network);

const logger = getCliLogger(args, {...beaconPaths, logFile: lightclientPaths.logFile}, config);
const logger = getCliLogger(args, {defaultLogFile: "lightclient.log"}, config);
const {beaconApiUrl, checkpointRoot} = args;
const api = getClient({baseUrl: beaconApiUrl}, {config});
const {data: genesisData} = await api.beacon.getGenesis();
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/cmds/lightclient/options.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import {logOptions} from "../../options/logOptions.js";
import {ICliCommandOptions, ILogArgs} from "../../util/index.js";
import {beaconPathsOptions, logOptions} from "../beacon/options.js";
import {IBeaconPaths} from "../beacon/paths.js";

export type ILightClientArgs = ILogArgs & {
logFile: IBeaconPaths["logFile"];
beaconApiUrl: string;
checkpointRoot: string;
};

export const lightclientOptions: ICliCommandOptions<ILightClientArgs> = {
...logOptions,
logFile: beaconPathsOptions.logFile,
beaconApiUrl: {
description: "Url to a beacon node that support lightclient API",
type: "string",
Expand Down
27 changes: 0 additions & 27 deletions packages/cli/src/cmds/lightclient/paths.ts

This file was deleted.

7 changes: 3 additions & 4 deletions packages/cli/src/cmds/validator/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {IGlobalArgs} from "../../options/index.js";
import {YargsError, getDefaultGraffiti, mkdir, getCliLogger} from "../../util/index.js";
import {onGracefulShutdown, parseFeeRecipient, parseProposerConfig} from "../../util/index.js";
import {getVersionData} from "../../util/version.js";
import {getBeaconPaths} from "../beacon/paths.js";
import {getAccountPaths, getValidatorPaths} from "./paths.js";
import {IValidatorCliArgs, validatorMetricsDefaultOptions} from "./options.js";
import {getSignersFromArgs} from "./signers/index.js";
Expand All @@ -26,13 +25,13 @@ export async function validatorHandler(args: IValidatorCliArgs & IGlobalArgs): P
const doppelgangerProtectionEnabled = args.doppelgangerProtectionEnabled;
const valProposerConfig = getProposerConfigFromArgs(args);

const beaconPaths = getBeaconPaths(args, network);
const validatorPaths = getValidatorPaths(args, network);

const logger = getCliLogger(args, {...beaconPaths, logFile: validatorPaths.logFile}, config);
const logger = getCliLogger(args, {defaultLogFile: "validator.log"}, config);

const {version, commit} = getVersionData();
logger.info("Lodestar", {network, version, commit});
logger.info("Connecting to LevelDB database", {path: validatorPaths.validatorsDbDir});

const dbPath = validatorPaths.validatorsDbDir;
mkdir(dbPath);
Expand Down Expand Up @@ -79,7 +78,7 @@ export async function validatorHandler(args: IValidatorCliArgs & IGlobalArgs): P

const dbOps = {
config,
controller: new LevelDbController({name: dbPath}, {logger}),
controller: new LevelDbController({name: dbPath}, {metrics: null}),
};
const slashingProtection = new SlashingProtection(dbOps);

Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/cmds/validator/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {defaultOptions} from "@lodestar/validator";
import {logOptions} from "../../options/logOptions.js";
import {ensure0xPrefix, ICliCommandOptions, ILogArgs} from "../../util/index.js";
import {logOptions, beaconPathsOptions} from "../beacon/options.js";
import {IBeaconPaths} from "../beacon/paths.js";
import {keymanagerRestApiServerOptsDefault} from "./keymanager/server.js";
import {defaultAccountPaths, defaultValidatorPaths} from "./paths.js";

Expand All @@ -20,7 +19,6 @@ export const validatorMetricsDefaultOptions = {
export type IValidatorCliArgs = AccountValidatorArgs &
KeymanagerArgs &
ILogArgs & {
logFile: IBeaconPaths["logFile"];
validatorsDbDir?: string;
server: string;
force: boolean;
Expand Down Expand Up @@ -93,7 +91,6 @@ export const keymanagerOptions: ICliCommandOptions<KeymanagerArgs> = {
export const validatorOptions: ICliCommandOptions<IValidatorCliArgs> = {
...logOptions,
...keymanagerOptions,
logFile: beaconPathsOptions.logFile,

keystoresDir: {
hidden: true,
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/cmds/validator/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {IGlobalPaths, getGlobalPaths} from "../../paths/global.js";

export type IValidatorPaths = {
validatorsDbDir: string;
logFile?: string;
};

export type AccountPaths = {
Expand All @@ -31,12 +30,10 @@ export function getValidatorPaths(

const dataDir = globalPaths.dataDir;
const validatorsDbDir = args.validatorsDbDir ?? path.join(dataDir, "validator-db");
const logFile = args.logFile?.trim() !== "none" ? args.logFile ?? path.join(dataDir, "validator.log") : undefined;

return {
...globalPaths,
validatorsDbDir,
logFile,
};
}

Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/cmds/validator/slashingProtection/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import {IGlobalArgs} from "../../../options/index.js";
import {AccountValidatorArgs} from "../options.js";
import {getCliLogger, ILogArgs} from "../../../util/index.js";
import {getBeaconConfigFromArgs} from "../../../config/index.js";
import {getBeaconPaths} from "../../beacon/paths.js";
import {getValidatorPaths} from "../paths.js";
import {getGenesisValidatorsRoot, getSlashingProtection} from "./utils.js";
import {ISlashingProtectionArgs} from "./options.js";
import {getSlashingProtectionPaths} from "./paths.js";

/* eslint-disable no-console */

Expand Down Expand Up @@ -42,9 +40,8 @@ export const exportCmd: ICliCommand<
handler: async (args) => {
const {config, network} = getBeaconConfigFromArgs(args);

const beaconPaths = getBeaconPaths(args, network);
const {logFile} = getSlashingProtectionPaths(args, network, "export-");
const logger = getCliLogger(args, {...beaconPaths, logFile}, config);
// slashingProtection commands are fast so do not require logFile feature
const logger = getCliLogger(args, {defaultLogFile: "validator.log"}, config);

const {validatorsDbDir: dbPath} = getValidatorPaths(args, network);

Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/cmds/validator/slashingProtection/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import {IGlobalArgs} from "../../../options/index.js";
import {AccountValidatorArgs} from "../options.js";
import {getCliLogger, ILogArgs} from "../../../util/index.js";
import {getBeaconConfigFromArgs} from "../../../config/index.js";
import {getBeaconPaths} from "../../beacon/paths.js";
import {getValidatorPaths} from "../paths.js";
import {getGenesisValidatorsRoot, getSlashingProtection} from "./utils.js";
import {ISlashingProtectionArgs} from "./options.js";
import {getSlashingProtectionPaths} from "./paths.js";

/* eslint-disable no-console */

Expand Down Expand Up @@ -42,9 +40,8 @@ export const importCmd: ICliCommand<

handler: async (args) => {
const {config, network} = getBeaconConfigFromArgs(args);
const beaconPaths = getBeaconPaths(args, network);
const {logFile} = getSlashingProtectionPaths(args, network, "export-");
const logger = getCliLogger(args, {...beaconPaths, logFile}, config);
// slashingProtection commands are fast so do not require logFile feature
const logger = getCliLogger(args, {defaultLogFile: "validator.log"}, config);

const {validatorsDbDir: dbPath} = getValidatorPaths(args, network);

Expand Down

0 comments on commit b5882f0

Please sign in to comment.