feat: update log with given context (#147)

Update logging with the given context.
This commit is contained in:
Yuan
2024-12-23 19:55:25 -08:00
committed by GitHub
parent bf276e5f56
commit 809e547a48
8 changed files with 94 additions and 54 deletions

View File

@@ -44,7 +44,7 @@ type Server struct {
}
// NewServer returns a Server object based on provided Config.
func NewServer(cfg ServerConfig, log logLib.Logger) (*Server, error) {
func NewServer(ctx context.Context, cfg ServerConfig, log logLib.Logger) (*Server, error) {
logLevel, err := logLib.SeverityToLevel(cfg.LogLevel.String())
if err != nil {
return nil, fmt.Errorf("unable to initialize http log: %w", err)
@@ -89,7 +89,7 @@ func NewServer(cfg ServerConfig, log logLib.Logger) (*Server, error) {
}
sourcesMap[name] = s
}
log.Info(fmt.Sprintf("Initialized %d sources.", len(sourcesMap)))
log.InfoContext(ctx, fmt.Sprintf("Initialized %d sources.", len(sourcesMap)))
// initialize and validate the auth sources
authSourcesMap := make(map[string]auth.AuthSource)
@@ -100,7 +100,7 @@ func NewServer(cfg ServerConfig, log logLib.Logger) (*Server, error) {
}
authSourcesMap[name] = a
}
log.Info(fmt.Sprintf("Initialized %d authSources.", len(authSourcesMap)))
log.InfoContext(ctx, fmt.Sprintf("Initialized %d authSources.", len(authSourcesMap)))
// initialize and validate the tools
toolsMap := make(map[string]tools.Tool)
@@ -111,7 +111,7 @@ func NewServer(cfg ServerConfig, log logLib.Logger) (*Server, error) {
}
toolsMap[name] = t
}
log.Info(fmt.Sprintf("Initialized %d tools.", len(toolsMap)))
log.InfoContext(ctx, fmt.Sprintf("Initialized %d tools.", len(toolsMap)))
// create a default toolset that contains all tools
allToolNames := make([]string, 0, len(toolsMap))
@@ -131,7 +131,7 @@ func NewServer(cfg ServerConfig, log logLib.Logger) (*Server, error) {
}
toolsetsMap[name] = t
}
log.Info(fmt.Sprintf("Initialized %d toolsets.", len(toolsetsMap)))
log.InfoContext(ctx, fmt.Sprintf("Initialized %d toolsets.", len(toolsetsMap)))
s := &Server{
conf: cfg,

View File

@@ -55,7 +55,7 @@ func TestServe(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}
s, err := server.NewServer(cfg, testLogger)
s, err := server.NewServer(context.Background(), cfg, testLogger)
if err != nil {
t.Fatalf("unable to initialize server! %v", err)
}