Files
gf/internal/intlog/intlog.go
houseme 7ffdff37e4 chore: upgrade golangci-lint configuration and optimize codebase (#4236)
This PR includes the following changes:

- **Upgrade `.golangci.yml`**: Updated the configuration file to align
with the latest golangci-lint version, ensuring compatibility and
leveraging new features.
- **Refactor GitHub Action workflow**: Modified `golangci-lint.yml` in
the GitHub Actions workflow to reflect the updated configuration and
improve CI performance.
- **Codebase optimization**: Refactored code to address issues and
warnings raised by the updated golangci-lint rules, including:
  - Improved function length and complexity.
  - Enhanced error handling and variable naming conventions.
- Fixed minor issues such as unused imports and formatting
inconsistencies.

These changes aim to maintain code quality, ensure compatibility with
the latest tools, and improve overall maintainability.
2025-08-22 13:29:09 +08:00

126 lines
3.1 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package intlog provides internal logging for GoFrame development usage only.
package intlog
import (
"bytes"
"context"
"fmt"
"path/filepath"
"time"
"go.opentelemetry.io/otel/trace"
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/internal/utils"
)
const (
stackFilterKey = "/internal/intlog"
)
// Print prints `v` with newline using fmt.Println.
// The parameter `v` can be multiple variables.
func Print(ctx context.Context, v ...interface{}) {
if !utils.IsDebugEnabled() {
return
}
doPrint(ctx, fmt.Sprint(v...), false)
}
// Printf prints `v` with format `format` using fmt.Printf.
// The parameter `v` can be multiple variables.
func Printf(ctx context.Context, format string, v ...interface{}) {
if !utils.IsDebugEnabled() {
return
}
doPrint(ctx, fmt.Sprintf(format, v...), false)
}
// Error prints `v` with newline using fmt.Println.
// The parameter `v` can be multiple variables.
func Error(ctx context.Context, v ...interface{}) {
if !utils.IsDebugEnabled() {
return
}
doPrint(ctx, fmt.Sprint(v...), true)
}
// Errorf prints `v` with format `format` using fmt.Printf.
func Errorf(ctx context.Context, format string, v ...interface{}) {
if !utils.IsDebugEnabled() {
return
}
doPrint(ctx, fmt.Sprintf(format, v...), true)
}
// PrintFunc prints the output from function `f`.
// It only calls function `f` if debug mode is enabled.
func PrintFunc(ctx context.Context, f func() string) {
if !utils.IsDebugEnabled() {
return
}
s := f()
if s == "" {
return
}
doPrint(ctx, s, false)
}
// ErrorFunc prints the output from function `f`.
// It only calls function `f` if debug mode is enabled.
func ErrorFunc(ctx context.Context, f func() string) {
if !utils.IsDebugEnabled() {
return
}
s := f()
if s == "" {
return
}
doPrint(ctx, s, true)
}
func doPrint(ctx context.Context, content string, stack bool) {
if !utils.IsDebugEnabled() {
return
}
buffer := bytes.NewBuffer(nil)
buffer.WriteString(time.Now().Format("2006-01-02 15:04:05.000"))
buffer.WriteString(" [INTE] ")
buffer.WriteString(file())
buffer.WriteString(" ")
if s := traceIDStr(ctx); s != "" {
buffer.WriteString(s + " ")
}
buffer.WriteString(content)
buffer.WriteString("\n")
if stack {
buffer.WriteString("Caller Stack:\n")
buffer.WriteString(gdebug.StackWithFilter([]string{stackFilterKey}))
}
fmt.Print(buffer.String())
}
// traceIDStr retrieves and returns the trace id string for logging output.
func traceIDStr(ctx context.Context) string {
if ctx == nil {
return ""
}
spanCtx := trace.SpanContextFromContext(ctx)
if traceID := spanCtx.TraceID(); traceID.IsValid() {
return "{" + traceID.String() + "}"
}
return ""
}
// file returns caller file name along with its line number.
func file() string {
_, p, l := gdebug.CallerWithFilter([]string{stackFilterKey})
return fmt.Sprintf(`%s:%d`, filepath.Base(p), l)
}