Skip to content

Commit f60d1a1

Browse files
committed
update log for gin mvc
1 parent 3f01b00 commit f60d1a1

File tree

8 files changed

+29
-13
lines changed

8 files changed

+29
-13
lines changed

mvx/mvc/gin-mvc/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ $ go get -u gorm.io/driver/mysql
9191
$ go mod tidy
9292

9393
# 运行服务
94-
$ go run cmd/main.go
94+
$ go run cmd/main.go # 默认 test 环境
95+
$ APP_ENV=production go run cmd/main.go # production 环境
9596

9697
# 查看服务
9798
$ curl -X GET http://localhost:8080

mvx/mvc/gin-mvc/cmd/main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,30 @@ func main() {
3737
gin.SetMode(gin.ReleaseMode) // 强制切换为生产模式[1,5](@ref)
3838
defaultEnv := os.Getenv("APP_ENV") // 读取系统环境变量,默认test
3939
// 解析命令行参数
40-
env := flag.String("env", defaultEnv, "Application environment (production, test)")
40+
env := flag.String("env", defaultEnv, "Application environment (production, test, dev)")
4141
flag.Parse()
4242

4343
// 初始化配置
4444
config.Init(env)
4545

4646
// 获取配置
4747
cfg := config.GetConfig()
48-
logger.Info("Config loaded successfully. " + cfg.Server.Addr) // 使用 logger 记录信息
49-
logger.Println("config:\r\n", cfg)
5048

5149
// 初始化日志
5250
logger.Init(cfg)
5351

54-
r := gin.Default()
52+
logger.Info("Config loaded successfully. " + cfg.Server.Addr) // 使用 logger 记录信息
53+
logger.Println("config:\r\n", cfg)
54+
55+
// r := gin.Default()
56+
57+
r := gin.New() // 不使用 gin.Default(),避免默认日志污染 logrus
58+
// 替换 Gin 日志
59+
r.Use(gin.LoggerWithConfig(gin.LoggerConfig{
60+
Output: logger.GetLogger().Out, // 让 Gin 访问日志输出到 logrus
61+
}))
62+
r.Use(gin.Recovery())
63+
5564
// 斜杠自动重定向
5665
r.RedirectTrailingSlash = true
5766

mvx/mvc/gin-mvc/internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func Init(env *string) {
135135
cfg, err := LoadConfig[Config](filePath);
136136
if err == nil {
137137
configInstance = cfg
138-
fmt.Printf("Configuration initialized successfully in %s environment.", configFile)
138+
fmt.Printf("Configuration initialized successfully in %s environment. ", configFile)
139139
} else {
140140
fmt.Println("Failed to initialize config: %w", err)
141141
}

mvx/mvc/gin-mvc/internal/config/config_prod.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ cache:
2828

2929
log:
3030
format: json # 日志格式,可选值: text, json
31-
output: file # 日志输出方式,可选值: stdout(标准输出)或 file(文件输出)
31+
output: stdout # 日志输出方式,可选值: stdout(标准输出)或 file(文件输出)
3232
level: "debug" # 日志级别,可选值: debug, info, warn, error, fatal
3333
file: "logs/app.log" # 日志文件路径,仅在 output 设置为 file 时生效
3434
max_size: 10 # 单个日志文件的最大大小(单位 MB),超出后会创建新文件

mvx/mvc/gin-mvc/internal/config/config_test.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ cache:
2727
expiration: 3600
2828

2929
log:
30-
level: info
31-
format: json
32-
output: stdout
33-
log_file: "logs/app.log"
30+
format: json # 日志格式,可选值: text, json
31+
output: file # 日志输出方式,可选值: stdout(标准输出)或 file(文件输出)
32+
level: "debug" # 日志级别,可选值: debug, info, warn, error, fatal
33+
file: "logs/app.log" # 日志文件路径,仅在 output 设置为 file 时生效
34+
max_size: 10 # 单个日志文件的最大大小(单位 MB),超出后会创建新文件
35+
max_backups: 5 # 最多保留的历史日志文件数,超过此数量则删除最旧的日志文件
36+
max_age: 7 # 日志文件最大保存天数,超过后自动删除旧日志
37+
compress: true # 是否压缩旧的日志文件(gzip 压缩)

mvx/mvc/gin-mvc/internal/controllers/home/home_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func GetIndex(c *gin.Context) {
2020
// 根路径返回 HTML 页面
2121
port := 8080
2222
htmlContent := fmt.Sprintf(`
23-
<h1>Welcome to DDD example.</h1>
23+
<h1>Welcome to Go MVC example.</h1>
2424
<pre>
2525
测试
2626
<code>

mvx/mvc/gin-mvc/internal/services/order/order_service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type OrderService interface {
2727
DeleteOrder(orderNo string) error
2828
}
2929

30-
// OrderService 订单服务实现 OrderServiceInterface
30+
// OrderServiceImpl 订单服务实现 OrderService 接口
31+
// 如不实现接口,也可以只定义struct,接口会更加利于扩展
3132
type OrderServiceImpl struct {
3233
repo repository.OrderRepository
3334
}

mvx/mvc/gin-mvc/pkg/logger/logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (f *PlainTextFormatter) Format(entry *logrus.Entry) ([]byte, error) {
5252

5353
// Init 初始化日志配置
5454
func Init(cfg *config.Config) {
55+
log.Info("init config log:", config.GetConfig().Log)
5556
config := cfg.Log
5657
// 设置日志格式
5758
if config.Format == "json" {

0 commit comments

Comments
 (0)