Skip to content

Commit

Permalink
v0.1.17 支持多日志通道输出
Browse files Browse the repository at this point in the history
  • Loading branch information
陈壁浩 committed Jul 26, 2024
1 parent 05f6864 commit 7d23808
Show file tree
Hide file tree
Showing 29 changed files with 614 additions and 418 deletions.
16 changes: 16 additions & 0 deletions app/http/module/demo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func Register(r *gin.Engine) error {
r.GET("/demo/orm", api.DemoOrm)
r.GET("/demo/redis", api.DemoRedis)
r.GET("/demo/cache", api.DemoCache)

r.GET("/demo/logs", api.DemoLogs)
return nil
}

Expand Down Expand Up @@ -76,3 +78,17 @@ func (api *DemoApi) DemoPost(c *gin.Context) {
}
c.JSON(200, nil)
}

func (api *DemoApi) DemoLogs(c *gin.Context) {
log := c.MustMakeLog()
count, _ := c.DefaultQueryInt("count", 1)
for i := 0; i < count; i++ {
log.Error(c, "Error( ErrorErrorError )", nil)
log.Warn(c, "Warn( WarnWarnWarn )", nil)
log.Info(c, "Info( InfoInfoInfo )", nil)
log.Info(c, "Info( InfoInfoInfo )", map[string]interface{}{"info": "xxxxxxxxxxxxx"})
log.Debug(c, "Debug( DebugDebugDebug )", nil)
log.Trace(c, "Trace( TraceTraceTrace )", nil)
}
c.JSON(200, "已打印测试日志")
}
83 changes: 40 additions & 43 deletions config/dev/log.yaml
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
## drivers(启用的日志通道)默认值: [console]
## level(日志等级)默认值: info
## formatter(格式化器)默认值: text
## folder(文件目录)默认值取 app 的 LogFolder地址
## file(文件名)默认值:app.log

driver: console
formatter: text
# 启用的日志通道
drivers: [console,single,rotate,rolling]
level: trace
formatter: text
folder: ./storage/log/

#driver: single
#level: trace
#folder: ./storage/log/single
#file: gob_single.log
# 控制台输出
console:
level: info
formatter: text

#driver: rotate # 切割日志
#level: trace # 日志级别
#file: gob.log # 保存的日志文件
#rotate_count: 10 # 最多日志文件个数
#rotate_size: 120000 # 每个日志大小
#rotate_time: "1m" # 切割时间
#max_age: "10d" # 文件保存时间
#date_format: "%Y-%m-%d-%H-%M" # 文件后缀格式
# 单文件输出
single:
level: info
folder: ./storage/log/
file: app.log

#driver: aliyun_sls
#level: trace
#formatter: json
# 按日期切割(rotate库已于21年存档,寻找替代品准备移除)
rotate:
level: info # 日志级别
folder: ./storage/log/ # 日志文件
file: app_rotate.log # 保存的日志文件
rotate_count: 10 # 最多日志文件个数
rotate_size: 1048576 # 每个日志大小(byte)
rotate_time: "1d" # 切割时间
max_age: "90d" # 文件保存时间
date_format: "%Y-%m-%d" # 文件后缀格式

#################### 改造成: https://blog.csdn.net/xmcy001122/article/details/119916227
# 按容量切割(缺少按日切割功能)
rolling:
level: info # 日志级别
folder: ./storage/log/ # 日志文件
file: app_rolling.log # 保存的日志文件
maxSize: 1 # 文件大小(mb) 0为不限制
maxAge: 0 # 保留旧日志文件的最大天数 0为不限制
maxBackups: 0 # 保留旧日志文件的最大数量 0为不限制

# 启用的日志通道
#enable: console,single
#
#console:
# level: trace
# formatter: text
#
#single:
# level: trace
# folder: ./storage/log/
# file: gob_single.log
#
#rotate:
# level: trace # 日志级别
# folder: ./storage/log/
# file: gob_rotate.log # 保存的日志文件
# rotate_count: 10 # 最多日志文件个数
# rotate_size: 120000 # 每个日志大小
# rotate_time: "1m" # 切割时间
# max_age: "10d" # 文件保存时间
# date_format: "%Y-%m-%d-%H-%M" # 文件后缀格式
#
#aliyun_sls:
# level: trace
# formatter: json
# 阿里云的 SLS 日志
aliyun_sls:
level: info
formatter: json
4 changes: 2 additions & 2 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gob_docs",
"version": "0.1.15",
"version": "0.1.17",
"description": "A VuePress project",
"license": "MIT",
"type": "module",
Expand Down
11 changes: 10 additions & 1 deletion docs/src/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,29 @@ export default defineUserConfig({
// 添加导航栏
navbar: [
{text: "主页", link: "/"}, // 导航条
{text: "快速开始", link: "/start/"},
{text: "使用文档", link: "/guide/"},
{text: "服务提供者", link: "/provider/"},
{text: "提供命令", link: "/command/"},
{text: "Github", link: "https://github.com/chenbihao/gob"}
],
// 为以下路由添加侧边栏
sidebar: {
"/start/": [
{
title: "快速上手",
collapsable: false,
children: [
"", // 快速上手
],
},
],
"/guide/": [
{
title: "指南",
collapsable: false,
children: [
"", // 介绍
"introduce", // 快速上手
"install", // 安装
"app", // 运行

Expand Down
2 changes: 1 addition & 1 deletion docs/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: 开始体验

actions:
- text: 快速开始
link: /guide/introduce
link: /start/
type: primary
# - text: Introduction
# link: https://vuejs.press/guide/introduction.html
Expand Down
5 changes: 5 additions & 0 deletions docs/src/command/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ description: 提供命令

Gob 框架提供了很多开箱即用的命令

> 注:在全局调用通过使用 `go instal` 安装到 `$GOPATH/bin` 的二进制执行文件时,可用的命令受限。
> 目前仅支持 `gob new``gob version` 等命令
> 其他命令需要在使用 `gob new` 命令生成应用并构建后,利用应用二进制文件来调用命令。
9 changes: 2 additions & 7 deletions docs/src/gen_docs/gen_get_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"bytes"
"go/ast"
"go/token"
"os"
Expand Down Expand Up @@ -48,9 +47,7 @@ func getProviderSourceCode(filePath string, fset *token.FileSet, d *ast.TypeSpec
f, _ := os.OpenFile(filePath, os.O_RDONLY, 0666)
defer f.Close()

buf := make([]byte, 1024*4)
f.Read(buf)
reader := bufio.NewReader(bytes.NewReader(buf))
reader := bufio.NewReader(f)

sb := strings.Builder{}
for i := 1; i <= endLine; i++ {
Expand Down Expand Up @@ -89,9 +86,7 @@ func getRemark(filePath string, fset *token.FileSet, astFile *ast.File) (s strin
f, _ := os.OpenFile(filePath, os.O_RDONLY, 0666)
defer f.Close()

buf := make([]byte, 1024*4)
f.Read(buf)
reader := bufio.NewReader(bytes.NewReader(buf))
reader := bufio.NewReader(f)
sb := strings.Builder{}
for i := 1; i < endLine; i++ {
line, _, _ := reader.ReadLine()
Expand Down
19 changes: 13 additions & 6 deletions docs/src/guide/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ description:


- [ ] 日志
- [ ] 同时支持多个日志输出,并且接管gin的日志输出
- [ ] 同时支持多个日志输出
- [ ] 接管gin的日志输出
- [ ] 统一优化日志打印格式
- [ ] 可选固定 json 字段顺序打印配置(强迫症可选)

- [ ] 数据库重连重试机制

- [ ] model 代码生成功能优化
- [ ] 生成更符合业务调用场景
- [ ] 其他数据源如sqlite的字段优化
Expand All @@ -29,19 +32,20 @@ description:

- [ ] 将过时或停止维护的三方库换掉
- [ ] survey 换成 bubbletea
- [ ] 命令行支持静默运行参数
- [ ] 命令行支持静默运行参数
- [ ] github 的调用限制重构复用
- [ ] gspt 构建出错 , 需要交叉编译
- [ ] 日志库切换?
- [ ] file-rotatelogs 废弃

- [ ] 前端文件夹配置可选?
- [ ] frontendFolder 配置可选

- [ ] 数据库重连重试机制

- [ ] 其他优化
- [ ] 把其他命令适配到纯工具模式(`go install`
- [ ] win 下不支持 Daemon,兼容成后台运行(appDaemon)
- [ ] cache 服务当配置了 redis,并且有 redis 相关配置时,优先读取 redis 配置
- [ ] 注意配置读取时的存储链路,如果不是每次都调用 conf 服务读取的话,可能读不到热更新的新值

### 框架模块新增

Expand Down Expand Up @@ -100,8 +104,11 @@ description:
- [x] 梳理三方库引入
- fsnotify、go-daemon、goconvey、swaggo、cast
- survey/v2、go-git/v5、go-github/v62、go-redis/v9、cron/v3、gorm + gen
- gotree、uuid、xid、ratelimit、file-rotatelogs、mapstructure
- jennifer/jen、jianfengye/collection、kr/pretty
- gotree、uuid、xid、ratelimit、mapstructure
- kr/pretty、jennifer/jen
- 预计移除:jianfengye/collection
- 预计移除:file-rotatelogs、natefinch/lumberjack
- 预计新增:samber/lo
- [x] 梳理三方框架使用
- vue、vuepress

Expand Down
4 changes: 4 additions & 0 deletions docs/src/provider/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ description:
type Config interface {
// IsExist 检查一个属性是否存在
IsExist(key string) bool
// GetIntIfExist 如果存在的话,获取一个 int 属性
GetIntIfExist(key string) (exist bool, value int)
// GetStringIfExist 如果存在的话,获取一个 string 属性
GetStringIfExist(key string) (exist bool, value string)
// Get 获取一个属性值
Get(key string) interface{}
// GetBool 获取一个 bool 属性
Expand Down
70 changes: 60 additions & 10 deletions docs/src/provider/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,67 @@ description:
# gob:log

## 服务介绍:
提供日志记录相关操作

提供日志记录相关操作,目前支持控制台、单文件、切割文件、阿里云SLS 等日志输出通道

> 阿里云SLS 需要配置对应的 sls 服务 [sls](./sls)
## 支持命令:无
## 支持配置:无
## 支持配置:

通过配置文件 `config/[env]/log.yaml` 可以配置缓存服务的驱动和参数,如下是一个配置示例:

> 注意:rotate库已于21年存档,寻找替代品准备移除
```yaml
## drivers(启用的日志通道)默认值: [console]
## level(日志等级)默认值: info
## formatter(格式化器)默认值: text
## folder(文件目录)默认值取 app 的 LogFolder地址
## file(文件名)默认值:app.log

# 启用的日志通道
drivers: [console,single,rotate,rolling]
level: trace
formatter: text
folder: ./storage/log/

# 控制台输出
console:
level: info
formatter: text

# 单文件输出
single:
level: info
folder: ./storage/log/
file: app_single.log

# 按日期切割(rotate库已于21年存档,寻找替代品准备移除)
rotate:
level: info # 日志级别
folder: ./storage/log/ # 日志文件
file: app_rotate.log # 保存的日志文件
rotate_count: 10 # 最多日志文件个数
rotate_size: 1048576 # 每个日志大小(byte)
rotate_time: "1d" # 切割时间
max_age: "90d" # 文件保存时间
date_format: "%Y-%m-%d" # 文件后缀格式

# 按容量切割(缺少按日切割功能)
rolling:
level: info # 日志级别
folder: ./storage/log/ # 日志文件
file: app_rolling.log # 保存的日志文件
maxSize: 1 # 文件大小(mb) 0为不限制
maxAge: 0 # 保留旧日志文件的最大天数 0为不限制
maxBackups: 0 # 保留旧日志文件的最大数量 0为不限制

# 阿里云的 SLS 日志
aliyun_sls:
level: info
formatter: json
```
## 提供方法:
```go
Expand All @@ -27,13 +85,5 @@ type Log interface {
Debug(ctx context.Context, msg string, fields map[string]interface{})
// Trace 表示最详细的信息,一般信息量比较大,可能包含调用堆栈等信息
Trace(ctx context.Context, msg string, fields map[string]interface{})
// SetLevel 设置日志级别
SetLevel(level LogLevel)
// SetCtxFielder 从context中获取上下文字段field
SetCtxFielder(handler CtxFielder)
// SetFormatter 设置输出格式
SetFormatter(formatter Formatter)
// SetOutput 设置输出管道
SetOutput(out io.Writer)
}
```
2 changes: 1 addition & 1 deletion docs/src/provider/sls.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sls 提供对接阿里云日志服务的服务,可以用于日志的收集。
## 支持命令:无
## 支持配置:

配置文件为 config/[env]/app.yaml ,如下是一个配置示例:
配置文件为 `config/[env]/app.yaml` ,如下是一个配置示例:

```yaml
# 阿里云SLS服务
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions framework/command/model/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ var modelApiCommand = &cobra.Command{
RunE: func(c *cobra.Command, args []string) error {
ctx := c.Context()
container := c.GetContainer()
logger := container.MustMake(contract.LogKey).(contract.Log)
logger.SetLevel(contract.ErrorLevel)

gormService := container.MustMake(contract.ORMKey).(contract.ORM)
db, err := gormService.GetDB(orm.WithConfigPath(database))
Expand Down
3 changes: 0 additions & 3 deletions framework/command/model/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ var modelGenCommand = &cobra.Command{

// 获取env环境
container := c.GetContainer()
logger := container.MustMake(contract.LogKey).(contract.Log)
logger.SetLevel(contract.ErrorLevel)

gormService := container.MustMake(contract.ORMKey).(contract.ORM)
db, err := gormService.GetDB(orm.WithConfigPath(database))
if err != nil {
Expand Down
Loading

0 comments on commit 7d23808

Please sign in to comment.