feat(database): 新增命令行参数解析、空值检查和数据库配置管理功能
- 实现了 command 包用于控制台参数和选项解析 - 添加了 empty 包提供变量空值/零值检查功能 - 集成了 Viper 库进行配置文件管理和环境变量支持 - 提供了数据库配置初始化和默认值设置功能 - 实现了配置值获取、设置和结构化解析方法 - 添加了服务器、数据库和JWT配置的便捷获取方法 - 实现了完整的数据库接口定义和事务管理功能
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
// 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"
|
||||
|
||||
"git.magicany.cc/black1552/gin-base/database/utils"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"github.com/gogf/gf/v2/debug/gdebug"
|
||||
)
|
||||
|
||||
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 ...any) {
|
||||
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 ...any) {
|
||||
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 ...any) {
|
||||
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 ...any) {
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user