Support logging to file

This commit is contained in:
Eric 2023-09-29 12:13:41 +10:00
parent 2f1c778d02
commit 1351b0d222
No known key found for this signature in database
3 changed files with 39 additions and 3 deletions

View File

@ -129,7 +129,13 @@ proc bootstrapInteractions(
return (client, host, validator)
proc start*(s: CodexServer) {.async.} =
notice "Starting codex node"
notice "Starting codex node",
persistence = s.config.persistence,
validator = s.config.validator,
ethAccount = s.config.ethAccount,
simProofFailures = s.config.simulateProofFailures,
dataDir = s.config.dataDir,
logFile = s.config.logFile
await s.repoStore.start()
s.maintenance.start()

View File

@ -28,8 +28,11 @@ import pkg/metrics
import pkg/metrics/chronos_httpserver
import pkg/stew/shims/net as stewnet
import pkg/stew/shims/parseutils
import pkg/stew/byteutils
import pkg/libp2p
import pkg/ethers
import pkg/questionable
import pkg/questionable/results
import ./discovery
import ./stores
@ -259,6 +262,13 @@ type
hidden
.}: int
logFile* {.
desc: "Logs to file"
defaultValue: string.none
name: "log-file"
hidden
.}: Option[string]
of initNode:
discard
@ -444,9 +454,10 @@ proc updateLogLevel*(logLevel: string) {.upraises: [ValueError].} =
warn "Unrecognized logging topic", topic = topicName
proc setupLogging*(conf: CodexConf) =
when defaultChroniclesStream.outputs.type.arity != 2:
when defaultChroniclesStream.outputs.type.arity != 3:
warn "Logging configuration options not enabled in the current build"
else:
var logFile: ?IoHandle
proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard
proc writeAndFlush(f: File, msg: LogOutputStr) =
try:
@ -461,6 +472,25 @@ proc setupLogging*(conf: CodexConf) =
proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) =
writeAndFlush(stdout, stripAnsi(msg))
proc fileFlush(logLevel: LogLevel, msg: LogOutputStr) =
if file =? logFile:
if error =? file.writeFile(stripAnsi(msg).toBytes).errorOption:
error "failed to write to log file", errorCode = $error
defaultChroniclesStream.outputs[2].writer = noOutput
if logFilePath =? conf.logFile and logFilePath.len > 0:
let logFileHandle = openFile(
logFilePath,
{OpenFlags.Write, OpenFlags.Create, OpenFlags.Truncate}
)
if logFileHandle.isErr:
error "failed to open log file",
path = logFilePath,
errorCode = $logFileHandle.error
else:
logFile = logFileHandle.option
defaultChroniclesStream.outputs[2].writer = fileFlush
defaultChroniclesStream.outputs[1].writer = noOutput
defaultChroniclesStream.outputs[0].writer =

View File

@ -111,7 +111,7 @@ switch("define", "libp2p_pki_schemes=secp256k1")
#TODO this infects everything in this folder, ideally it would only
# apply to codex.nim, but since codex.nims is used for other purpose
# we can't use it. And codex.cfg doesn't work
switch("define", "chronicles_sinks=textlines[dynamic],json[dynamic]")
switch("define", "chronicles_sinks=textlines[dynamic],json[dynamic],textlines[dynamic]")
# begin Nimble config (version 1)
when system.fileExists("nimble.paths"):