refactor: 引入日志切割库、indexApi拆分等

This commit is contained in:
meilin.huang
2024-01-23 19:30:28 +08:00
parent d530365ef9
commit e4d13f3377
43 changed files with 324 additions and 241 deletions

View File

@@ -66,6 +66,9 @@ func (c *Config) IfBlankDefaultValue() {
AddSource: c.Log.AddSource,
Filename: c.Log.File.Name,
Filepath: c.Log.File.Path,
MaxSize: c.Log.File.MaxSize,
MaxAge: c.Log.File.MaxAge,
Compress: c.Log.File.Compress,
})
c.Server.Default()

View File

@@ -2,7 +2,6 @@ package config
import (
"mayfly-go/pkg/logx"
"path"
)
type Log struct {
@@ -26,23 +25,9 @@ func (l *Log) Default() {
}
type LogFile struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
}
// 获取完整路径文件名
func (l *LogFile) GetFilename() string {
var filepath, filename string
if fp := l.Path; fp == "" {
filepath = "./"
} else {
filepath = fp
}
if fn := l.Name; fn == "" {
filename = "default.log"
} else {
filename = fn
}
return path.Join(filepath, filename)
Name string `yaml:"name"`
Path string `yaml:"path"`
MaxSize int `yaml:"max-size"`
MaxAge int `yaml:"max-age"`
Compress bool `yaml:"compress"`
}

View File

@@ -18,7 +18,7 @@ type Mysql struct {
func (m *Mysql) Default() {
if m.Host == "" {
m.Host = "localhost:3306"
logx.Warnf("未配置mysql.host, 默认值: %s", m.Host)
logx.Warnf("[使用sqlite可忽略]未配置mysql.host, 默认值: %s", m.Host)
}
if m.Config == "" {
m.Config = "charset=utf8&loc=Local&parseTime=true"

View File

@@ -11,7 +11,7 @@ type Sqlite struct {
func (m *Sqlite) Default() {
if m.Path == "" {
m.Path = "./mayfly-go.sqlite"
logx.Warnf("未配置sqlite.path, 默认值: %s", m.Path)
logx.Warnf("[使用mysql可忽略]未配置sqlite.path, 默认值: %s", m.Path)
}
if m.MaxIdleConns == 0 {
m.MaxIdleConns = 5

View File

@@ -109,11 +109,15 @@ func (c *Container) injectWithField(objValue reflect.Value) error {
}
fieldValue := objValue.Field(i)
fieldPtrValue := reflect.NewAt(fieldValue.Type(), fieldValue.Addr().UnsafePointer())
fieldValue = fieldPtrValue.Elem()
if !fieldValue.IsValid() || !fieldValue.CanSet() {
return fmt.Errorf("%s error: 字段无效或为不可导出类型", injectInfo)
// 不可导出变量处理
fieldPtrValue := reflect.NewAt(fieldValue.Type(), fieldValue.Addr().UnsafePointer())
fieldValue = fieldPtrValue.Elem()
if !fieldValue.IsValid() || !fieldValue.CanSet() {
return fmt.Errorf("%s error: 字段无效或为不可导出类型", injectInfo)
}
}
fieldValue.Set(reflect.ValueOf(component))
}

View File

@@ -1,20 +1,25 @@
package logx
import (
"fmt"
"io"
"log/slog"
"os"
"path"
"strings"
"gopkg.in/natefinch/lumberjack.v2"
)
type Config struct {
Level string
Type string // 日志类型text、json
AddSource bool // 是否记录调用方法
Filename string
Filepath string
Filename string // 日志文件名
Filepath string // 日志路径
MaxSize int // 日志文件的最大大小(以兆字节为单位)。当日志文件大小达到该值时,将触发切割操作,默认为 100 兆字节
MaxAge int // 根据文件名中的时间戳,设置保留旧日志文件的最大天数。一天被定义为 24 小时
Compress bool // 是否使用 gzip 压缩方式压缩轮转后的日志文件
writer io.Writer
}
@@ -24,15 +29,18 @@ func (c *Config) GetLogOut() io.Writer {
if c.writer != nil {
return c.writer
}
writer := os.Stdout
var writer io.Writer
writer = os.Stdout
// 根据配置文件设置日志级别
if c.Filepath != "" && c.Filename != "" {
// 写入文件
file, err := os.OpenFile(path.Join(c.Filepath, c.Filename), os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModeAppend|0666)
if err != nil {
panic(fmt.Sprintf("创建日志文件失败: %s", err.Error()))
writer = &lumberjack.Logger{
Filename: path.Join(c.Filepath, c.Filename),
MaxSize: c.MaxSize,
MaxAge: c.MaxAge,
Compress: c.Compress,
LocalTime: true,
}
writer = file
}
c.writer = writer

View File

@@ -22,3 +22,23 @@ func Kvs(elements ...any) M {
}
return myMap
}
// Keys returns the keys of the map m.
// The keys will be in an indeterminate order.
func MapKeys[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
// Values returns the values of the map m.
// The values will be in an indeterminate order.
func MapValues[M ~map[K]V, K comparable, V any](m M) []V {
r := make([]V, 0, len(m))
for _, v := range m {
r = append(r, v)
}
return r
}