Files
dingtalk-workspace-cli/internal/logging/transport.go
T
audanye-sudo c8f1a9a912 chore: fix staticcheck warnings and restore missing .env.example
- Replace nil context with context.TODO() in logger calls
- Fix import ordering and struct field alignment
- Restore .env.example required by open-source audit tests
2026-03-29 00:30:27 +08:00

64 lines
2.0 KiB
Go

// Copyright 2026 Alibaba Group
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logging
import (
"context"
"log/slog"
"time"
)
// LogRequest logs a JSON-RPC request at Debug level.
func LogRequest(logger *slog.Logger, method, endpoint, executionId string, bodySize int) {
if logger == nil {
return
}
logger.Debug("jsonrpc_request",
slog.String("method", method),
slog.String("endpoint", redactEndpoint(endpoint)),
slog.String("execution_id", executionId),
slog.Int("body_size", bodySize),
)
}
// LogResponse logs a JSON-RPC response at Debug level.
func LogResponse(logger *slog.Logger, method, endpoint string, statusCode int, respSize int, duration time.Duration, err error) {
if logger == nil {
return
}
attrs := []slog.Attr{
slog.String("method", method),
slog.String("endpoint", redactEndpoint(endpoint)),
slog.Int("status", statusCode),
slog.Int("resp_size", respSize),
slog.String("duration", duration.Truncate(time.Millisecond).String()),
}
if err != nil {
attrs = append(attrs, slog.String("error", err.Error()))
logger.LogAttrs(context.TODO(), slog.LevelWarn, "jsonrpc_response", attrs...)
return
}
logger.LogAttrs(context.TODO(), slog.LevelDebug, "jsonrpc_response", attrs...)
}
// redactEndpoint removes query parameters from endpoint URLs in logs.
func redactEndpoint(endpoint string) string {
for i := 0; i < len(endpoint); i++ {
if endpoint[i] == '?' || endpoint[i] == '#' {
return endpoint[:i]
}
}
return endpoint
}