Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved logging #3001

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion nimbus/common/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import
chronicles,
logging,
../db/[core_db, ledger, storage_types],
../utils/[utils],
".."/[constants, errors, version],
Expand All @@ -26,7 +27,8 @@ export
hardforks,
genesis,
utils,
taskpools
taskpools,
logging

type
SyncProgress = object
Expand Down
35 changes: 35 additions & 0 deletions nimbus/common/logging.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Nimbus
# Copyright (c) 2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
std/[typetraits, net],
json_serialization,
web3/primitives,
confutils/defs,
eth/common/eth_types_json_serialization

# nim-eth
proc writeValue*(
w: var JsonWriter, v: EthTime | NetworkId | ChainId
) {.inline, raises: [IOError].} =
w.writeValue distinctBase(v)

# nim-web3
proc writeValue*(w: var JsonWriter, v: Quantity) {.inline, raises: [IOError].} =
w.writeValue distinctBase(v)

# nim-confutils
proc writeValue*(
w: var JsonWriter, v: InputFile | OutDir | OutFile | RestOfCmdLine | OutPath
) {.inline, raises: [IOError].} =
w.writeValue distinctBase(v)

# build-system
proc writeValue*(w: var JsonWriter, v: Port) {.inline, raises: [IOError].} =
w.writeValue distinctBase(v)
21 changes: 14 additions & 7 deletions nimbus/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import
common/chain_config,
db/opts

export net, defs
from beacon_chain/nimbus_binary_common import setupLogging, StdoutLogKind

export net, defs, StdoutLogKind


const
Expand Down Expand Up @@ -217,13 +219,16 @@ type
logLevel* {.
separator: "\pLOGGING AND DEBUGGING OPTIONS:"
desc: "Sets the log level for process and topics (" & logLevelDesc & ")"
defaultValue: LogLevel.INFO
defaultValueDesc: $LogLevel.INFO
name: "log-level" }: LogLevel
defaultValue: "INFO"
defaultValueDesc: "Info topic level logging"
name: "log-level" }: string

logFile* {.
desc: "Specifies a path for the written Json log file"
name: "log-file" }: Option[OutFile]
logStdout* {.
hidden
desc: "Specifies what kind of logs should be written to stdout (auto, colors, nocolors, json)"
defaultValueDesc: "auto"
defaultValue: StdoutLogKind.Auto
name: "log-format" .}: StdoutLogKind

logMetricsEnabled* {.
desc: "Enable metrics logging"
Expand Down Expand Up @@ -822,6 +827,8 @@ proc makeConfig*(cmdLine = commandLineParams()): NimbusConf
except CatchableError as e:
raise e

setupLogging(result.logLevel, result.logStdout, none(OutFile))

var networkId = result.getNetworkId()

if result.customNetwork.isSome:
Expand Down
25 changes: 8 additions & 17 deletions nimbus/nimbus_execution_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ proc preventLoadingDataDirForTheWrongNetwork(db: CoreDbRef; conf: NimbusConf) =
kvt.put(dataDirIdKey().toOpenArray, calculatedId.data).isOkOr:
fatal "Cannot write data dir ID", ID=calculatedId
quit(QuitFailure)

let
kvt = db.ctx.getKvt()
calculatedId = calcHash(conf.networkId, conf.networkParams)
Expand All @@ -173,22 +173,14 @@ proc preventLoadingDataDirForTheWrongNetwork(db: CoreDbRef; conf: NimbusConf) =
if conf.rewriteDatadirId:
writeDataDirId(kvt, calculatedId)
return

if calculatedId.data != dataDirIdBytes:
fatal "Data dir already initialized with other network configuration",
get=dataDirIdBytes.toHex,
expected=calculatedId
quit(QuitFailure)

proc run(nimbus: NimbusNode, conf: NimbusConf) =
## logging
setLogLevel(conf.logLevel)
if conf.logFile.isSome:
let logFile = string conf.logFile.get()
defaultChroniclesStream.output.outFile = nil # to avoid closing stdout
discard defaultChroniclesStream.output.open(logFile, fmAppend)

setupFileLimits()

info "Launching execution client",
version = FullVersionStr,
Expand Down Expand Up @@ -297,19 +289,18 @@ proc run(nimbus: NimbusNode, conf: NimbusConf) =
when isMainModule:
var nimbus = NimbusNode(state: NimbusState.Starting, ctx: newEthContext())

## Processing command line arguments
let conf = makeConfig()

setupFileLimits()

## Ctrl+C handling
proc controlCHandler() {.noconv.} =
when defined(windows):
# workaround for https://github.com/nim-lang/Nim/issues/4057
setupForeignThreadGc()
nimbus.state = NimbusState.Stopping
echo "\nCtrl+C pressed. Waiting for a graceful shutdown."
notice "\nCtrl+C pressed. Waiting for a graceful shutdown."
setControlCHook(controlCHandler)

## Show logs on stdout until we get the user's logging choice
discard defaultChroniclesStream.output.open(stdout)

## Processing command line arguments
let conf = makeConfig()

nimbus.run(conf)
7 changes: 7 additions & 0 deletions nimbus/nimbus_execution_client.nim.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
-d:"chronicles_runtime_filtering=on"
-d:"chronicles_disable_thread_id"

@if release:
-d:"chronicles_line_numbers:0"
@end
4 changes: 2 additions & 2 deletions nimbus/nimbus_import.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2024 Status Research & Development GmbH
# Copyright (c) 2024-2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand Down Expand Up @@ -277,7 +277,7 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
else:
raiseAssert "Other networks are unsupported or do not have an era1"
db = Era1DbRef.init(conf.era1Dir.string, era1Name).valueOr:
fatal "Could not open era1 database", era1Dir = conf.era1Dir, era1Name, error
fatal "Could not open era1 database", era1Dir=conf.era1Dir, era1Name=era1Name, error=error
quit(QuitFailure)

notice "Importing era1 archive",
Expand Down
5 changes: 3 additions & 2 deletions nimbus/sync/protocol/eth68.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nimbus - Ethereum Wire Protocol
#
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Copyright (c) 2018-2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
Expand All @@ -21,7 +21,8 @@ import
stew/byteutils,
./trace_config,
./eth/eth_types,
../../utils/utils
../../utils/utils,
../../common/logging
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected that eth68.nim needs to add any imports without other changes using said imports. Presumably, it does some logging which triggers the writeValues, e.g., due to the EthTime thing?

Copy link
Contributor Author

@pedromiguelmiranda pedromiguelmiranda Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only import added was the common/logging and not only for EthTime but also others (eg, NetworkId. There are several trace log messages along eth68 with types without json serialization (gathered all of them in logging.nim to avoid code duplication)


export
eth_types
Expand Down
2 changes: 1 addition & 1 deletion nimbus/sync/sync_sched.nim
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ proc onPeerConnected[S,W](dsc: RunnerSyncRef[S,W]; peer: Peer) =
# This could happen if there are idle entries in the table, i.e.
# somehow hanging runners.
if dsc.ctx.noisyLog: trace "Peer table full! Dequeuing least used entry",
oldest, nPeers, nWorkers=dsc.buddies.len, maxWorkers
oldestPeer=oldest.peer, oldestOnly=oldest.only, nPeers=nPeers, nWorkers=dsc.buddies.len, maxWorkers
# Setting to `zombie` will trigger the worker to terminate (if any.)
oldest.ctrl.zombie = true

Expand Down
Loading