From 8970a22fdd0aa8d8ed12d879e771f27583e4d857 Mon Sep 17 00:00:00 2001 From: zah Date: Wed, 15 Jul 2020 16:15:55 +0300 Subject: [PATCH] Add a non-optional Json file log for the beacon node as stopgap measure (#1322) * Add a non-optional Json file log for the beacon node as stopgap measure * Fix the build with -d:testnet_servers_image --- Makefile | 1 + beacon_chain/beacon_node.nim | 2 +- beacon_chain/beacon_node.nim.cfg | 1 + beacon_chain/conf.nim | 8 ++++++++ beacon_chain/nimbus_binary_common.nim | 25 +++++++++++++++++++++---- beacon_chain/validator_client.nim | 2 +- beacon_chain/validator_client.nim.cfg | 1 + 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 769893ab7..341a77818 100644 --- a/Makefile +++ b/Makefile @@ -184,6 +184,7 @@ altona: | beacon_node build/beacon_node \ --network=altona \ --log-level="$(LOG_LEVEL)" \ + --log-file=nbc_bn_$$(date +"%Y%m%d%H%M%S").log \ --data-dir=build/data/shared_altona_$(NODE_ID) \ $(GOERLI_TESTNETS_PARAMS) $(NODE_PARAMS) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index f9f94dbcc..82c2633f6 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -1161,7 +1161,7 @@ proc createWalletInteractively( programMain: var config = makeBannerAndConfig(clientId, BeaconNodeConf) - setupMainProc(config.logLevel) + setupLogging(config.logLevel, config.logFile) if config.eth2Network.isSome: let diff --git a/beacon_chain/beacon_node.nim.cfg b/beacon_chain/beacon_node.nim.cfg index 1e8d4f284..b00a3586b 100644 --- a/beacon_chain/beacon_node.nim.cfg +++ b/beacon_chain/beacon_node.nim.cfg @@ -1,3 +1,4 @@ +-d:"chronicles_sinks=textlines,json[file]" -d:"chronicles_runtime_filtering=on" -d:"chronicles_default_output_device=dynamic" diff --git a/beacon_chain/conf.nim b/beacon_chain/conf.nim index e90e33416..a8d15826b 100644 --- a/beacon_chain/conf.nim +++ b/beacon_chain/conf.nim @@ -41,6 +41,10 @@ type desc: "Sets the log level" name: "log-level" }: string + logFile* {. + desc: "Specifies a path for the written Json log file" + name: "log-file" }: Option[OutFile] + eth2Network* {. desc: "The Eth2 network to join" name: "network" }: Option[string] @@ -351,6 +355,10 @@ type desc: "Sets the log level." name: "log-level" }: string + logFile* {. + desc: "Specifies a path for the written Json log file" + name: "log-file" }: Option[OutFile] + dataDir* {. defaultValue: config.defaultDataDir() desc: "The directory where nimbus will store all blockchain data" diff --git a/beacon_chain/nimbus_binary_common.nim b/beacon_chain/nimbus_binary_common.nim index 16e9a0063..8ec706287 100644 --- a/beacon_chain/nimbus_binary_common.nim +++ b/beacon_chain/nimbus_binary_common.nim @@ -9,18 +9,18 @@ import # Standard library - tables, random, strutils, + tables, random, strutils, os, typetraits, # Nimble packages - chronos, + chronos, confutils/defs, chronicles, chronicles/helpers as chroniclesHelpers, # Local modules spec/[datatypes, crypto], eth2_network, time -proc setupMainProc*(logLevel: string) = +proc setupLogging*(logLevel: string, logFile: Option[OutFile]) = when compiles(defaultChroniclesStream.output.writer): - defaultChroniclesStream.output.writer = + defaultChroniclesStream.outputs[0].writer = proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe, raises: [Defect].} = try: stdout.write(msg) @@ -29,6 +29,23 @@ proc setupMainProc*(logLevel: string) = randomize() + if logFile.isSome: + when defaultChroniclesStream.outputs.type.arity > 1: + block openLogFile: + let + logFile = logFile.get.string + logFileDir = splitFile(logFile).dir + try: + createDir logFileDir + except CatchableError as err: + error "Failed to create directory for log file", path = logFileDir, err = err.msg + break openLogFile + + if not defaultChroniclesStream.outputs[1].open(logFile): + error "Failed to create log file", logFile + else: + warn "The --log-file option is not active in the current build" + try: let directives = logLevel.split(";") try: diff --git a/beacon_chain/validator_client.nim b/beacon_chain/validator_client.nim index 93715d6e5..7ea115165 100644 --- a/beacon_chain/validator_client.nim +++ b/beacon_chain/validator_client.nim @@ -200,7 +200,7 @@ proc onSlotStart(vc: ValidatorClient, lastSlot, scheduledSlot: Slot) {.gcsafe, a programMain: let config = makeBannerAndConfig("Nimbus validator client v" & fullVersionStr, ValidatorClientConf) - setupMainProc(config.logLevel) + setupLogging(config.logLevel, config.logFile) case config.cmd of VCNoCommand: diff --git a/beacon_chain/validator_client.nim.cfg b/beacon_chain/validator_client.nim.cfg index ba0389d80..b3f3c97d0 100644 --- a/beacon_chain/validator_client.nim.cfg +++ b/beacon_chain/validator_client.nim.cfg @@ -1,3 +1,4 @@ +-d:"chronicles_sinks=textlines,json[file]" -d:"chronicles_runtime_filtering=on" -d:"chronicles_default_output_device=dynamic"