Skip to content

Commit c714da8

Browse files
committed
improvements in the logging package
1 parent d8dc009 commit c714da8

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

pkg/logtrace/log.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,22 @@ type ContextKey string
1818
const CorrelationIDKey ContextKey = "correlation_id"
1919

2020
// Setup initializes the logger with a specified log level
21-
func Setup(serviceName, env string, level slog.Level) {
21+
func Setup(serviceName, env string, level string) {
22+
var slogLevel slog.Level
23+
switch level {
24+
case "warn":
25+
slogLevel = slog.LevelWarn
26+
case "error":
27+
slogLevel = slog.LevelError
28+
case "debug":
29+
slogLevel = slog.LevelDebug
30+
default:
31+
slogLevel = slog.LevelInfo
32+
}
33+
2234
opts := &slog.HandlerOptions{
23-
AddSource: true,
24-
Level: level,
35+
AddSource: false,
36+
Level: slogLevel,
2537
}
2638

2739
hostname, _ := os.Hostname()
@@ -35,20 +47,38 @@ func Setup(serviceName, env string, level slog.Level) {
3547
func log(logFunc LogLevel, message string, fields Fields) {
3648
fieldArgs := make([]interface{}, 0, len(fields)*2+1)
3749

38-
pc, file, line, ok := runtime.Caller(2)
39-
if ok {
40-
details := runtime.FuncForPC(pc)
41-
fieldArgs = append(fieldArgs, slog.Group(
42-
"source",
43-
slog.Attr{Key: "filename", Value: slog.StringValue(file)},
44-
slog.Attr{Key: "lineno", Value: slog.IntValue(line)},
45-
slog.Attr{Key: "function", Value: slog.StringValue(details.Name())},
46-
))
50+
// Determine the current level
51+
var currentLevel slog.Level
52+
switch logFunc {
53+
case slog.Debug:
54+
currentLevel = slog.LevelDebug
55+
case slog.Info:
56+
currentLevel = slog.LevelInfo
57+
case slog.Warn:
58+
currentLevel = slog.LevelWarn
59+
case slog.Error:
60+
currentLevel = slog.LevelError
61+
default:
62+
currentLevel = slog.LevelInfo
63+
}
64+
65+
// Only attach source info for TRACE/DEBUG level
66+
if currentLevel <= slog.LevelDebug {
67+
if pc, file, line, ok := runtime.Caller(2); ok {
68+
details := runtime.FuncForPC(pc)
69+
fieldArgs = append(fieldArgs, slog.Group(
70+
"source",
71+
slog.String("filename", file),
72+
slog.Int("lineno", line),
73+
slog.String("function", details.Name()),
74+
))
75+
}
4776
}
4877

4978
for key, value := range fields {
5079
fieldArgs = append(fieldArgs, slog.Any(key, value))
5180
}
81+
5282
logFunc(message, fieldArgs...)
5383
}
5484

supernode/cmd/start.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"log/slog"
76
"os"
87
"os/signal"
98
"syscall"
@@ -35,8 +34,7 @@ var startCmd = &cobra.Command{
3534
The supernode will connect to the Lumera network and begin participating in the network.`,
3635
RunE: func(cmd *cobra.Command, args []string) error {
3736
// Initialize logging
38-
logLevel := slog.LevelInfo
39-
logtrace.Setup("supernode", "dev", logLevel)
37+
logtrace.Setup("supernode", "dev", appConfig.LogConfig.Level)
4038

4139
// Create context with correlation ID for tracing
4240
ctx := logtrace.CtxWithCorrelationID(context.Background(), "supernode-start")

supernode/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ lumera:
2727
# RaptorQ Configuration
2828
raptorq:
2929
files_dir: "raptorq_files"
30+
31+
#Logging Configuration
32+
log:
33+
level: "info" #debug, info, warn, error

supernode/config/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ type RaptorQConfig struct {
3939
FilesDir string `yaml:"files_dir"`
4040
}
4141

42+
type LogConfig struct {
43+
Level string `yaml:"level"`
44+
}
45+
4246
type Config struct {
4347
SupernodeConfig `yaml:"supernode"`
4448
KeyringConfig `yaml:"keyring"`
4549
P2PConfig `yaml:"p2p"`
4650
LumeraClientConfig `yaml:"lumera"`
4751
RaptorQConfig `yaml:"raptorq"`
52+
LogConfig `yaml:"log"`
4853

4954
// Store base directory (not from YAML)
5055
BaseDir string `yaml:"-"`
@@ -135,11 +140,12 @@ func LoadConfig(filename string, baseDir string) (*Config, error) {
135140
return nil, err
136141
}
137142

138-
logtrace.Info(ctx, "Configuration loaded successfully", logtrace.Fields{
143+
logtrace.Debug(ctx, "Configuration loaded successfully", logtrace.Fields{
139144
"baseDir": baseDir,
140145
"keyringDir": config.GetKeyringDir(),
141146
"p2pDataDir": config.GetP2PDataDir(),
142147
"raptorqFilesDir": config.GetRaptorQFilesDir(),
148+
"logLevel": config.LogConfig.Level,
143149
})
144150

145151
return &config, nil

0 commit comments

Comments
 (0)