- Add internal/logging package with JSON file logger - Support log rotation at 10MB with 3 backup files - Add MultiHandler for dual output (stderr + file) - Include automatic secret redaction in log entries - Add transport-level request/response logging helpers
67 lines
1.9 KiB
Go
67 lines
1.9 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"
|
|
)
|
|
|
|
// MultiHandler fans out slog records to multiple handlers.
|
|
// Each handler applies its own level filter independently.
|
|
type MultiHandler struct {
|
|
handlers []slog.Handler
|
|
}
|
|
|
|
// NewMultiHandler creates a handler that writes to all provided handlers.
|
|
func NewMultiHandler(handlers ...slog.Handler) *MultiHandler {
|
|
return &MultiHandler{handlers: handlers}
|
|
}
|
|
|
|
func (h *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
|
for _, handler := range h.handlers {
|
|
if handler.Enabled(ctx, level) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (h *MultiHandler) Handle(ctx context.Context, record slog.Record) error {
|
|
for _, handler := range h.handlers {
|
|
if handler.Enabled(ctx, record.Level) {
|
|
if err := handler.Handle(ctx, record); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
handlers := make([]slog.Handler, len(h.handlers))
|
|
for i, handler := range h.handlers {
|
|
handlers[i] = handler.WithAttrs(attrs)
|
|
}
|
|
return &MultiHandler{handlers: handlers}
|
|
}
|
|
|
|
func (h *MultiHandler) WithGroup(name string) slog.Handler {
|
|
handlers := make([]slog.Handler, len(h.handlers))
|
|
for i, handler := range h.handlers {
|
|
handlers[i] = handler.WithGroup(name)
|
|
}
|
|
return &MultiHandler{handlers: handlers}
|
|
}
|