Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,18 @@ const (
)

const (
LoggerLevelKey = "logger.level"
LoggerDriverKey = "logger.driver"
LoggerFormatKey = "logger.format"
LoggerAppenderKey = "logger.appender"
LoggerFileNameKey = "logger.file.name"
LoggerFileNaxSizeKey = "logger.file.max-size"
LoggerFileMaxBackupsKey = "logger.file.max-backups"
LoggerFileMaxAgeKey = "logger.file.max-age"
LoggerFileLocalTimeKey = "logger.file.local-time"
LoggerFileCompressKey = "logger.file.compress"
LoggerLevelKey = "logger.level"
LoggerDriverKey = "logger.driver"
LoggerFormatKey = "logger.format"
LoggerAppenderKey = "logger.appender"
LoggerFileNameKey = "logger.file.name"
LoggerFileNaxSizeKey = "logger.file.max-size"
LoggerFileMaxBackupsKey = "logger.file.max-backups"
LoggerFileMaxAgeKey = "logger.file.max-age"
LoggerFileLocalTimeKey = "logger.file.local-time"
LoggerFileCompressKey = "logger.file.compress"
LoggerTraceEnabledKey = "logger.trace-integration.enabled"
LoggerTraceRecordErrorKey = "logger.trace-integration.record-error-to-span"
)

// metrics key
Expand Down
23 changes: 23 additions & 0 deletions config/logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ type LoggerConfig struct {

// logger file
File *File `yaml:"file"`

// trace integration configuration
TraceIntegration *TraceIntegrationConfig `yaml:"trace-integration"`
}

// TraceIntegrationConfig configures the integration between logging and OpenTelemetry tracing.
type TraceIntegrationConfig struct {
// whether to enable trace integration (inject traceId, spanId into logs)
Enabled *bool `default:"false" yaml:"enabled"`

// whether to record error logs to span
RecordErrorToSpan *bool `default:"true" yaml:"record-error-to-span"`
}

type File struct {
Expand Down Expand Up @@ -112,6 +124,17 @@ func (l *LoggerConfig) toURL() *common.URL {
common.WithParamsValue(constant.LoggerFileMaxAgeKey, strconv.Itoa(l.File.MaxAge)),
common.WithParamsValue(constant.LoggerFileCompressKey, strconv.FormatBool(*l.File.Compress)),
)

// Add trace integration parameters if configured
if l.TraceIntegration != nil {
if l.TraceIntegration.Enabled != nil {
url.AddParam(constant.LoggerTraceEnabledKey, strconv.FormatBool(*l.TraceIntegration.Enabled))
}
if l.TraceIntegration.RecordErrorToSpan != nil {
url.AddParam(constant.LoggerTraceRecordErrorKey, strconv.FormatBool(*l.TraceIntegration.RecordErrorToSpan))
}
}

return url
}

Expand Down
17 changes: 17 additions & 0 deletions logger/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package logger

import (
"context"
)

import (
"github.com/natefinch/lumberjack"

Expand All @@ -41,3 +45,16 @@ type Logger interface {
Fatal(args ...any)
Fatalf(fmt string, args ...any)
}

// CtxLogger extends Logger interface with context-aware logging methods.
type CtxLogger interface {
Logger
CtxDebug(ctx context.Context, args ...any)
CtxDebugf(ctx context.Context, template string, args ...any)
CtxInfo(ctx context.Context, args ...any)
CtxInfof(ctx context.Context, template string, args ...any)
CtxWarn(ctx context.Context, args ...any)
CtxWarnf(ctx context.Context, template string, args ...any)
CtxError(ctx context.Context, args ...any)
CtxErrorf(ctx context.Context, template string, args ...any)
}
162 changes: 162 additions & 0 deletions logger/core/logrus/ctx_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 logrus

import (
"context"
"errors"
"fmt"
)

import (
dubbogoLogger "github.com/dubbogo/gost/log/logger"

"github.com/sirupsen/logrus"

"go.opentelemetry.io/otel/codes"

"go.opentelemetry.io/otel/trace"
)

import (
"dubbo.apache.org/dubbo-go/v3/logger"
)

// LogrusCtxLogger wraps DubboLogger with context-aware logging support.
type LogrusCtxLogger struct {
*dubbogoLogger.DubboLogger
recordErrorToSpan bool
}

var _ logger.CtxLogger = (*LogrusCtxLogger)(nil)

// NewLogrusCtxLogger creates a new LogrusCtxLogger.
func NewLogrusCtxLogger(base *dubbogoLogger.DubboLogger, recordErrorToSpan bool) *LogrusCtxLogger {
return &LogrusCtxLogger{
DubboLogger: base,
recordErrorToSpan: recordErrorToSpan,
}
}

// withTraceFields injects trace information from context into logger.
func (l *LogrusCtxLogger) withTraceFields(ctx context.Context) *logrus.Entry {
logrusLogger, ok := l.Logger.(*logrus.Logger)
if !ok {
return nil
}

fields := logger.ExtractTraceFields(ctx)
if fields.TraceID == "" {
return logrus.NewEntry(logrusLogger) // No trace information
}

return logrusLogger.WithFields(logrus.Fields{
"trace_id": fields.TraceID,
"span_id": fields.SpanID,
"trace_flags": fields.TraceFlags,
})
}

func (l *LogrusCtxLogger) CtxDebugf(ctx context.Context, template string, args ...any) {
if entry := l.withTraceFields(ctx); entry != nil {
entry.Debugf(template, args...)
} else {
l.Debugf(template, args...)
}
}

func (l *LogrusCtxLogger) CtxDebug(ctx context.Context, args ...any) {
if entry := l.withTraceFields(ctx); entry != nil {
entry.Debug(args...)
} else {
l.Debug(args...)
}
}

func (l *LogrusCtxLogger) CtxInfof(ctx context.Context, template string, args ...any) {
if entry := l.withTraceFields(ctx); entry != nil {
entry.Infof(template, args...)
} else {
l.Infof(template, args...)
}
}

func (l *LogrusCtxLogger) CtxInfo(ctx context.Context, args ...any) {
if entry := l.withTraceFields(ctx); entry != nil {
entry.Info(args...)
} else {
l.Info(args...)
}
}

func (l *LogrusCtxLogger) CtxWarnf(ctx context.Context, template string, args ...any) {
if entry := l.withTraceFields(ctx); entry != nil {
entry.Warnf(template, args...)
} else {
l.Warnf(template, args...)
}
}

func (l *LogrusCtxLogger) CtxWarn(ctx context.Context, args ...any) {
if entry := l.withTraceFields(ctx); entry != nil {
entry.Warn(args...)
} else {
l.Warn(args...)
}
}

// CtxErrorf logs error and optionally records to span.
func (l *LogrusCtxLogger) CtxErrorf(ctx context.Context, template string, args ...any) {
if entry := l.withTraceFields(ctx); entry != nil {
entry.Errorf(template, args...)
} else {
l.Errorf(template, args...)
}

if l.recordErrorToSpan {
l.recordErrorToSpanIfPresent(ctx, template, args...)
}
}

// CtxError logs error and optionally records to span.
func (l *LogrusCtxLogger) CtxError(ctx context.Context, args ...any) {
if entry := l.withTraceFields(ctx); entry != nil {
entry.Error(args...)
} else {
l.Error(args...)
}

if l.recordErrorToSpan {
l.recordErrorToSpanIfPresent(ctx, "%s", fmt.Sprint(args...))
}
}

func (l *LogrusCtxLogger) recordErrorToSpanIfPresent(ctx context.Context, template string, args ...any) {
if ctx == nil {
return
}

span := trace.SpanFromContext(ctx)
if span == nil || !span.IsRecording() {
return
}

msg := fmt.Sprintf(template, args...)
span.SetStatus(codes.Error, msg)
span.RecordError(errors.New(msg))
}
Loading
Loading