Skip to content

Commit 27bcb04

Browse files
committed
Add context to logging
1 parent 2243f99 commit 27bcb04

File tree

6 files changed

+255
-101
lines changed

6 files changed

+255
-101
lines changed

client.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,13 @@ func NewClient(host string, opts ...func(*Client)) (*Client, error) {
217217
}
218218

219219
// Log successful connection
220-
client.logger.Info("NETCONF connection established",
220+
client.logger.Info(context.Background(), "NETCONF connection established",
221221
"host", client.Host,
222222
"port", client.Port,
223223
"sessionID", client.sessionID,
224224
"version", client.serverVersion)
225225

226-
client.logger.Debug("NETCONF capabilities discovered",
226+
client.logger.Debug(context.Background(), "NETCONF capabilities discovered",
227227
"count", len(client.Capabilities))
228228

229229
return client, nil
@@ -252,7 +252,7 @@ func (c *Client) Close() error {
252252
return fmt.Errorf("failed to close NETCONF session: %w", err)
253253
}
254254

255-
c.logger.Info("NETCONF connection closed",
255+
c.logger.Info(context.Background(), "NETCONF connection closed",
256256
"host", c.Host,
257257
"sessionID", c.sessionID)
258258

@@ -744,7 +744,7 @@ func (c *Client) prepareXMLForLogging(xml string) string {
744744
strings.Count(xml, "<community>")
745745

746746
if sensitiveCount > MaxSensitiveElements {
747-
c.logger.Warn("Too many sensitive elements detected",
747+
c.logger.Warn(context.Background(), "Too many sensitive elements detected",
748748
"count", sensitiveCount,
749749
"max", MaxSensitiveElements)
750750
return XMLTooManySensitiveMessage
@@ -903,7 +903,7 @@ func (c *Client) Backoff(attempt int) time.Duration {
903903
//
904904
// Returns an error if the lock is not released within LockReleaseTimeout.
905905
func (c *Client) waitForLockRelease(ctx context.Context, target string) error {
906-
c.logger.Info("NETCONF waiting for lock release",
906+
c.logger.Info(ctx, "NETCONF waiting for lock release",
907907
"target", target,
908908
"timeout", c.LockReleaseTimeout.String())
909909

@@ -927,7 +927,7 @@ func (c *Client) waitForLockRelease(ctx context.Context, target string) error {
927927
// Note: ignoring unlock errors is intentional - we proved lock availability
928928
_, _ = c.Unlock(ctx, target) //nolint:errcheck // Intentional: verifying lock availability only
929929

930-
c.logger.Info("NETCONF lock acquired",
930+
c.logger.Info(ctx, "NETCONF lock acquired",
931931
"target", target)
932932

933933
return nil
@@ -947,7 +947,7 @@ func (c *Client) reconnect() error {
947947
c.mu.Lock()
948948
defer c.mu.Unlock()
949949

950-
c.logger.Warn("NETCONF reconnecting",
950+
c.logger.Warn(context.Background(), "NETCONF reconnecting",
951951
"host", c.Host,
952952
"reason", "transport error")
953953

@@ -976,7 +976,7 @@ func (c *Client) reconnect() error {
976976
// Create new driver
977977
driver, err := netconf.NewDriver(c.Host, scrapliOpts...)
978978
if err != nil {
979-
c.logger.Error("NETCONF reconnection failed",
979+
c.logger.Error(context.Background(), "NETCONF reconnection failed",
980980
"host", c.Host,
981981
"error", err.Error())
982982
return fmt.Errorf("failed to create driver during reconnect: %w", err)
@@ -985,7 +985,7 @@ func (c *Client) reconnect() error {
985985
// Open connection
986986
err = driver.Open()
987987
if err != nil {
988-
c.logger.Error("NETCONF reconnection failed",
988+
c.logger.Error(context.Background(), "NETCONF reconnection failed",
989989
"host", c.Host,
990990
"error", err.Error())
991991
return fmt.Errorf("failed to open connection during reconnect: %w", err)
@@ -1005,7 +1005,7 @@ func (c *Client) reconnect() error {
10051005
}
10061006
}
10071007

1008-
c.logger.Info("NETCONF reconnected",
1008+
c.logger.Info(context.Background(), "NETCONF reconnected",
10091009
"host", c.Host,
10101010
"sessionID", c.sessionID)
10111011

@@ -1074,14 +1074,14 @@ func (c *Client) sendRPC(ctx context.Context, req *Req) (Res, error) {
10741074

10751075
// Log retry attempts
10761076
if attempt > 0 {
1077-
c.logger.Warn("NETCONF operation retry",
1077+
c.logger.Warn(ctx, "NETCONF operation retry",
10781078
"operation", req.Operation,
10791079
"attempt", attempt,
10801080
"maxRetries", c.MaxRetries)
10811081
}
10821082

10831083
// Log operation start (Debug level)
1084-
c.logger.Debug("NETCONF RPC request",
1084+
c.logger.Debug(ctx, "NETCONF RPC request",
10851085
"operation", req.Operation,
10861086
"target", req.Target,
10871087
"sessionID", c.sessionID)
@@ -1091,7 +1091,7 @@ func (c *Client) sendRPC(ctx context.Context, req *Req) (Res, error) {
10911091

10921092
// Log operation completion (Debug level)
10931093
if err == nil {
1094-
c.logger.Debug("NETCONF RPC response",
1094+
c.logger.Debug(ctx, "NETCONF RPC response",
10951095
"operation", req.Operation,
10961096
"ok", res.OK,
10971097
"errorCount", len(res.Errors),
@@ -1150,15 +1150,15 @@ func (c *Client) sendRPC(ctx context.Context, req *Req) (Res, error) {
11501150
// Not transient or max retries reached
11511151
if !isTransient || attempt >= c.MaxRetries {
11521152
// Log operation failure
1153-
c.logger.Error("NETCONF operation failed",
1153+
c.logger.Error(ctx, "NETCONF operation failed",
11541154
"operation", req.Operation,
11551155
"retries", attempt,
11561156
"transient", isTransient,
11571157
"errorCount", len(res.Errors))
11581158

11591159
// Log each RPC error
11601160
for i, rpcErr := range res.Errors {
1161-
c.logger.Error("NETCONF RPC error",
1161+
c.logger.Error(ctx, "NETCONF RPC error",
11621162
"index", i,
11631163
"errorType", rpcErr.ErrorType,
11641164
"errorTag", rpcErr.ErrorTag,
@@ -1197,7 +1197,7 @@ func (c *Client) sendRPC(ctx context.Context, req *Req) (Res, error) {
11971197

11981198
// Apply backoff before next retry
11991199
delay := c.Backoff(attempt)
1200-
c.logger.Debug("NETCONF retry backoff",
1200+
c.logger.Debug(ctx, "NETCONF retry backoff",
12011201
"operation", req.Operation,
12021202
"attempt", attempt,
12031203
"delay", delay.String())

0 commit comments

Comments
 (0)