✨ add log config and gorm config
This commit is contained in:
@@ -2,9 +2,9 @@ package config
|
|||||||
|
|
||||||
// Logger 配置
|
// Logger 配置
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
Level string `yaml:"level"` // 日志级别
|
Level string `yaml:"level"` // 日志级别
|
||||||
Prefix string `yaml:"prefix"` // 日志前缀
|
Prefix string `yaml:"prefix"` // 日志前缀
|
||||||
Director string `yaml:"director"` // 日志文件存放目录
|
Director string `yaml:"director"` // 日志文件存放目录
|
||||||
ShowLine string `yaml:"showLine"` // 是否显示文件行号
|
ShowLine bool `yaml:"show-line"` // 是否显示文件行号
|
||||||
LogInConsole string `yaml:"logInConsole"` // 是否在控制台打印日志
|
LogInConsole string `yaml:"log-in-console"` // 是否在控制台打印日志
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,20 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
// MySQL 配置
|
// MySQL 配置
|
||||||
type MySQL struct {
|
type MySQL struct {
|
||||||
Host string `yaml:"host"` //主机地址
|
Host string `yaml:"host"` //主机地址
|
||||||
Port string `yaml:"port"` //端口号
|
Port int `yaml:"port"` //端口号
|
||||||
DB string `yaml:"db"` //数据库名
|
DB string `yaml:"db"` //数据库名
|
||||||
User string `yaml:"user"` //用户名
|
User string `yaml:"user"` //用户名
|
||||||
Password string `yaml:"password"` //密码
|
Password string `yaml:"password"` //密码
|
||||||
LogLevel string `yaml:"log-level"` // 日志级别 debug: 输出全部SQL语句; release: 只输出错误信息
|
LogLevel string `yaml:"log-level"` //日志级别 debug: 输出全部SQL语句; release: 只输出错误信息
|
||||||
|
Config string `yaml:"config"` //高级配置
|
||||||
|
MaxIdleConnes int `yaml:"max-idle-connes"` //最大空闲连接数
|
||||||
|
MaxOpenConnes int `yaml:"max-open-connes"` //最大连接数
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MySQL) Dsn() string {
|
||||||
|
return m.User + ":" + m.Password + "@tcp(" + m.Host + ":" + strconv.Itoa(m.Port) + ")/" + m.DB + "?" + m.Config
|
||||||
}
|
}
|
||||||
|
26
core/config.go
Normal file
26
core/config.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"schisandra-cloud-album/config"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InitConfig 初始化配置
|
||||||
|
func InitConfig() {
|
||||||
|
const ConfigFile = "config.yaml"
|
||||||
|
c := &config.Config{}
|
||||||
|
yamlCOnf, err := os.ReadFile(ConfigFile)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("get yaml config error: %s", err))
|
||||||
|
}
|
||||||
|
err = yaml.Unmarshal(yamlCOnf, c)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("config init unmarshal error: ", err)
|
||||||
|
}
|
||||||
|
log.Println("config init success")
|
||||||
|
global.CONFIG = c
|
||||||
|
}
|
37
core/gorm.go
Normal file
37
core/gorm.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/driver/mysql"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitGorm() {
|
||||||
|
global.DB = mySQlConnect()
|
||||||
|
}
|
||||||
|
func mySQlConnect() *gorm.DB {
|
||||||
|
if global.CONFIG.MySQL.Host == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
dsn := global.CONFIG.MySQL.Dsn()
|
||||||
|
var mysqlLogger logger.Interface
|
||||||
|
if global.CONFIG.System.Env == "dev" {
|
||||||
|
mysqlLogger = logger.Default.LogMode(logger.Info)
|
||||||
|
} else {
|
||||||
|
mysqlLogger = logger.Default.LogMode(logger.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||||
|
Logger: mysqlLogger,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
sqlDB, _ := db.DB()
|
||||||
|
sqlDB.SetMaxIdleConns(global.CONFIG.MySQL.MaxIdleConnes)
|
||||||
|
sqlDB.SetMaxOpenConns(global.CONFIG.MySQL.MaxOpenConnes)
|
||||||
|
sqlDB.SetConnMaxLifetime(time.Hour * 4) //连接最大复用时间
|
||||||
|
return db
|
||||||
|
}
|
80
core/logrus.go
Normal file
80
core/logrus.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
red = 31
|
||||||
|
yellow = 33
|
||||||
|
blue = 34
|
||||||
|
gray = 37
|
||||||
|
)
|
||||||
|
|
||||||
|
type LogFormatter struct{}
|
||||||
|
|
||||||
|
func (f *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||||
|
//根据不同的level显示不同的颜色
|
||||||
|
var levelColor int
|
||||||
|
switch entry.Level {
|
||||||
|
case logrus.DebugLevel, logrus.TraceLevel:
|
||||||
|
levelColor = gray
|
||||||
|
case logrus.WarnLevel:
|
||||||
|
levelColor = yellow
|
||||||
|
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
|
||||||
|
levelColor = red
|
||||||
|
default:
|
||||||
|
levelColor = blue
|
||||||
|
}
|
||||||
|
var b *bytes.Buffer
|
||||||
|
if entry.Buffer != nil {
|
||||||
|
b = entry.Buffer
|
||||||
|
} else {
|
||||||
|
b = &bytes.Buffer{}
|
||||||
|
}
|
||||||
|
log := global.CONFIG.Logger
|
||||||
|
// 自定义日期格式
|
||||||
|
timestamp := entry.Time.Format("2006-01-02 15:04:05")
|
||||||
|
if entry.HasCaller() {
|
||||||
|
//自定义文件路径
|
||||||
|
funcVal := entry.Caller.Function
|
||||||
|
fileVal := fmt.Sprintf("%s:%d", path.Base(entry.Caller.File), entry.Caller.Line)
|
||||||
|
//自定义输出格式
|
||||||
|
fmt.Fprintf(b, "%s[%s] \x1b[%dm[%s]\x1b[0m %s %s : %s\n", log.Prefix, timestamp, levelColor, entry.Level, fileVal, funcVal, entry.Message)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(b, "%s[%s] \x1b[%dm[%s]\x1b[0m : %s\n", log.Prefix, timestamp, levelColor, entry.Level, entry.Message)
|
||||||
|
}
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitLogger() *logrus.Logger {
|
||||||
|
newLog := logrus.New()
|
||||||
|
newLog.SetOutput(os.Stdout) //设置输出类型
|
||||||
|
newLog.SetReportCaller(global.CONFIG.Logger.ShowLine) //设置是否显示函数名和行号
|
||||||
|
newLog.SetFormatter(&LogFormatter{}) //设置日志格式
|
||||||
|
level, err := logrus.ParseLevel(global.CONFIG.Logger.Level)
|
||||||
|
if err != nil {
|
||||||
|
level = logrus.InfoLevel
|
||||||
|
}
|
||||||
|
newLog.SetLevel(level) //设置日志级别
|
||||||
|
global.LOG = newLog
|
||||||
|
InitDefaultLogger()
|
||||||
|
return newLog
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitDefaultLogger() {
|
||||||
|
//全局日志
|
||||||
|
logrus.SetOutput(os.Stdout) //设置输出类型
|
||||||
|
logrus.SetReportCaller(global.CONFIG.Logger.ShowLine) //设置是否显示函数名和行号
|
||||||
|
logrus.SetFormatter(&LogFormatter{}) //设置日志格式
|
||||||
|
level, err := logrus.ParseLevel(global.CONFIG.Logger.Level)
|
||||||
|
if err != nil {
|
||||||
|
level = logrus.InfoLevel
|
||||||
|
}
|
||||||
|
logrus.SetLevel(level) //设置日志级别
|
||||||
|
}
|
14
global/global.go
Normal file
14
global/global.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"schisandra-cloud-album/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config 全局配置文件
|
||||||
|
var (
|
||||||
|
CONFIG *config.Config
|
||||||
|
DB *gorm.DB
|
||||||
|
LOG *logrus.Logger
|
||||||
|
)
|
13
go.mod
13
go.mod
@@ -1,3 +1,16 @@
|
|||||||
module schisandra-cloud-album
|
module schisandra-cloud-album
|
||||||
|
|
||||||
go 1.22.5
|
go 1.22.5
|
||||||
|
|
||||||
|
require (
|
||||||
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||||
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
|
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||||
|
golang.org/x/text v0.16.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
gorm.io/driver/mysql v1.5.7 // indirect
|
||||||
|
gorm.io/gorm v1.25.11 // indirect
|
||||||
|
)
|
||||||
|
29
go.sum
Normal file
29
go.sum
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||||
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||||
|
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||||
|
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
|
||||||
|
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
|
||||||
|
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||||
|
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
|
||||||
|
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
13
main.go
13
main.go
@@ -1 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"schisandra-cloud-album/core"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// 初始化配置
|
||||||
|
core.InitConfig()
|
||||||
|
core.InitLogger()
|
||||||
|
core.InitGorm()
|
||||||
|
global.LOG.Error("hello world")
|
||||||
|
}
|
||||||
|
@@ -1,20 +0,0 @@
|
|||||||
# 系统配置
|
|
||||||
system:
|
|
||||||
host: "0.0.0.0" # 主机地址
|
|
||||||
port: 8080 # 端口
|
|
||||||
env: env # 环境
|
|
||||||
# MySQL配置
|
|
||||||
mysql:
|
|
||||||
host: "1.95.0.111" # 主机地址
|
|
||||||
port: 3306 # 端口
|
|
||||||
db: schisandra-cloud-album # 数据库名称
|
|
||||||
user: root # 用户名
|
|
||||||
password: LDQ20020618xxx # 密码
|
|
||||||
log-level: dev # 日志级别
|
|
||||||
# 日志配置
|
|
||||||
logger:
|
|
||||||
level: info # 日志级别
|
|
||||||
prefix: "[schisandra]" # 日志前缀
|
|
||||||
director: log # 日志文件存放目录
|
|
||||||
show-line: true # 是否显示文件行号
|
|
||||||
log-in-console: true # 是否在控制台打印日志
|
|
Reference in New Issue
Block a user