Skip to content

Commit 247a620

Browse files
authored
more human readable logging pattern (#4763)
### Changes - Caller (file name) only displayed in debug mode - Shorter timestamp format except in debug mode - Field format are now key=value, instead of [key=value] - Reduce node info we log during startup, except in debug mode - Removed connect message ### New Format ``` → bacalhau serve --orchestrator --compute 12:09:06 | INF Config loaded from: [], and with data-dir /Users/walid/.bacalhau 12:09:06 | INF Starting bacalhau... 12:09:07 | INF Starting connection manager node_id=n-2952ac48-9c5a-4114-8564-60eca482ba2e start_time=2024-12-12T12:09:07.805517+02:00 12:09:07 | INF Attempting to establish connection node_id=n-2952ac48-9c5a-4114-8564-60eca482ba2e 12:09:07 | INF bacalhau node running name=n-2952ac48-9c5a-4114-8564-60eca482ba2e orchestrators=["nats://127.0.0.1:4222"] 12:09:07 | INF handshake successful with node n-2952ac48-9c5a-4114-8564-60eca482ba2e ^C12:09:09 | INF bacalhau node shutting down... ``` ### Old Format ``` → bacalhau-1.5.0 serve --orchestrator --compute 12:09:11.576 | INF cmd/cli/serve/serve.go:102 > Config loaded from: [], and with data-dir /Users/walid/.bacalhau 12:09:11.577 | INF cmd/cli/serve/serve.go:181 > Starting bacalhau... 12:09:11.641 | INF cmd/cli/serve/serve.go:256 > bacalhau node running [address:0.0.0.0:1234] [capacity:"{CPU: 5.60, Memory: 12 GB, Disk: 76 GB, GPU: 0}"] [compute_enabled:true] [engines:["wasm","docker"]] [name:n-2952ac48-9c5a-4114-8564-60eca482ba2e] [orchestrator_address:0.0.0.0:4222] [orchestrator_enabled:true] [orchestrators:["nats://127.0.0.1:4222"]] [publishers:["noop","s3","local"]] [storages:["urldownload","inline","s3"]] [webui_enabled:false] To connect to this node from the local client, run the following commands in your shell: export BACALHAU_API_HOST=127.0.0.1 export BACALHAU_API_PORT=1234 A copy of these variables have been written to: /Users/walid/.bacalhau/bacalhau.run ^C12:09:14.077 | INF cmd/cli/serve/serve.go:271 > bacalhau node shutting down... ``` ![image](https://github.com/user-attachments/assets/4c102647-c398-48e9-92a7-6cb357017b4d) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a debug mode for enhanced logging, providing detailed startup information when enabled. - **Bug Fixes** - Improved error handling for configuration setup and logging issues. - **Documentation** - Updated log output format based on the log level, simplifying messages in non-debug mode. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent b8174c4 commit 247a620

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

cmd/cli/serve/serve.go

+26-22
Original file line numberDiff line numberDiff line change
@@ -229,42 +229,46 @@ func serve(cmd *cobra.Command, cfg types.Bacalhau, fsRepo *repo.FsRepo) error {
229229
}()
230230
}
231231

232+
isDebug := system.IsDebugMode()
232233
startupLog := log.Info().
233-
Str("name", sysmeta.NodeName).
234-
Str("address", fmt.Sprintf("%s:%d", hostAddress, cfg.API.Port)).
235-
Bool("compute_enabled", cfg.Compute.Enabled).
236-
Bool("orchestrator_enabled", cfg.Orchestrator.Enabled).
237-
Bool("webui_enabled", cfg.WebUI.Enabled)
238-
if cfg.Compute.Enabled {
239-
capacity := standardNode.ComputeNode.Capacity.GetMaxCapacity(ctx)
234+
Str("name", sysmeta.NodeName)
235+
236+
if isDebug {
240237
startupLog.
241-
Strs("engines", standardNode.ComputeNode.Executors.Keys(ctx)).
242-
Strs("publishers", standardNode.ComputeNode.Publishers.Keys(ctx)).
243-
Strs("storages", standardNode.ComputeNode.Storages.Keys(ctx)).
244-
Strs("orchestrators", cfg.Compute.Orchestrators).
245-
Str("capacity", capacity.String())
246-
247-
if len(cfg.Compute.AllowListedLocalPaths) > 0 {
248-
startupLog.Strs("volumes", cfg.Compute.AllowListedLocalPaths)
238+
Str("address", fmt.Sprintf("%s:%d", hostAddress, cfg.API.Port)).
239+
Bool("compute_enabled", cfg.Compute.Enabled).
240+
Bool("orchestrator_enabled", cfg.Orchestrator.Enabled).
241+
Bool("webui_enabled", cfg.WebUI.Enabled)
242+
}
243+
if cfg.Compute.Enabled {
244+
startupLog.Strs("orchestrators", cfg.Compute.Orchestrators)
245+
246+
if isDebug {
247+
capacity := standardNode.ComputeNode.Capacity.GetMaxCapacity(ctx)
248+
startupLog.
249+
Strs("engines", standardNode.ComputeNode.Executors.Keys(ctx)).
250+
Strs("publishers", standardNode.ComputeNode.Publishers.Keys(ctx)).
251+
Strs("storages", standardNode.ComputeNode.Storages.Keys(ctx)).
252+
Str("capacity", capacity.String())
253+
if len(cfg.Compute.AllowListedLocalPaths) > 0 {
254+
startupLog.Strs("volumes", cfg.Compute.AllowListedLocalPaths)
255+
}
249256
}
250257
}
251258

252259
if cfg.Orchestrator.Enabled {
253-
startupLog.Str("orchestrator_address",
254-
fmt.Sprintf("%s:%d", cfg.Orchestrator.Host, cfg.Orchestrator.Port))
260+
if isDebug {
261+
startupLog.Str("orchestrator_address",
262+
fmt.Sprintf("%s:%d", cfg.Orchestrator.Host, cfg.Orchestrator.Port))
263+
}
255264
}
256265
startupLog.Msg("bacalhau node running")
257266

258267
envvars := buildEnvVariables(cfg)
259-
cmd.Println()
260-
cmd.Println("To connect to this node from the local client, run the following commands in your shell:")
261-
cmd.Println(envvars)
262-
263268
riPath, err := fsRepo.WriteRunInfo(ctx, envvars)
264269
if err != nil {
265270
return err
266271
}
267-
cmd.Printf("A copy of these variables have been written to: %s\n", riPath)
268272
defer os.Remove(riPath)
269273

270274
<-ctx.Done() // block until killed

pkg/logger/logger.go

+20-18
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,28 @@ func defaultLogFormat(w *zerolog.ConsoleWriter) {
188188
isTerminal := isatty.IsTerminal(os.Stdout.Fd())
189189
w.Out = os.Stdout
190190
w.NoColor = !isTerminal
191-
w.TimeFormat = "15:04:05.999 |"
192-
w.PartsOrder = []string{
193-
zerolog.TimestampFieldName,
194-
zerolog.LevelFieldName,
195-
zerolog.CallerFieldName,
196-
zerolog.MessageFieldName,
197-
}
198-
199-
// TODO: figure out a way to show the custom fields at the beginning of the log line rather than at the end.
200-
// Adding the fields to the parts section didn't help as it just printed the fields twice.
201-
w.FormatFieldName = func(i interface{}) string {
202-
return fmt.Sprintf("[%s:", i)
203-
}
204191

205-
w.FormatFieldValue = func(i interface{}) string {
206-
// don't print nil in case field value wasn't preset. e.g. no nodeID
207-
if i == nil {
208-
i = ""
192+
// Get the current log level to determine format
193+
level := zerolog.GlobalLevel()
194+
isDebug := level <= zerolog.DebugLevel
195+
196+
if isDebug {
197+
// Debug mode - show detailed information
198+
w.TimeFormat = "15:04:05.999 |"
199+
w.PartsOrder = []string{
200+
zerolog.TimestampFieldName,
201+
zerolog.LevelFieldName,
202+
zerolog.CallerFieldName,
203+
zerolog.MessageFieldName,
204+
}
205+
} else {
206+
// Normal mode - simplified, user-friendly format
207+
w.TimeFormat = "15:04:05 |"
208+
w.PartsOrder = []string{
209+
zerolog.TimestampFieldName,
210+
zerolog.LevelFieldName,
211+
zerolog.MessageFieldName,
209212
}
210-
return fmt.Sprintf("%s]", i)
211213
}
212214
}
213215

pkg/logger/logger_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ func TestConfigureLogging(t *testing.T) {
3333
subsubpackage.TestLog("testing error logging", "testing message")
3434

3535
actual := logging.String()
36-
// Like 12:47:40.875 | ERR pkg/logger/testpackage/subpackage/subsubpackage/testutil.go:12 > testing message error="testing error logging" [stack:[{"func":"TestLog","line":"10","source":"testutil.go"},{"func":"TestConfigureLogging","line":"27","source":"logger_test.go"},...]]
36+
// Like 12:47:40.875 | ERR > testing message error="testing error logging" stack=[{"func":"TestLog","line":"10","source":"testutil.go"},{"func":"TestConfigureLogging","line":"27","source":"logger_test.go"},...]
3737
t.Log(actual)
3838

3939
assert.Contains(t, actual, "testing message", "Log statement doesn't contain the log message")
4040
assert.Contains(t, actual, `error="testing error logging"`, "Log statement doesn't contain the logged error")
41-
assert.Contains(t, actual, "pkg/logger/testpackage/subpackage/subsubpackage/testutil.go", "Log statement doesn't contain the full package path")
42-
assert.Contains(t, actual, `stack:[{"func":"TestLog","line":`, "Log statement didn't automatically include the error's stacktrace")
41+
assert.Contains(t, actual, `stack=[{"func":"TestLog","line":`, "Log statement didn't automatically include the error's stacktrace")
4342
}
4443

4544
func TestParseAndConfigureLogging(t *testing.T) {

0 commit comments

Comments
 (0)