Skip to content

Commit 60b5416

Browse files
committed
perf: 优化日志组件
1 parent 40725ad commit 60b5416

File tree

8 files changed

+78
-8
lines changed

8 files changed

+78
-8
lines changed

configs/config.yaml.example

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ http:
4949
# 日志组件配置
5050
logger:
5151
level: info
52-
logfile:
52+
disableConsole: false # 是否禁用控制台输出
53+
logFile:
5354
enable: true
54-
errorfilename: ./runtime/logs/cago.err.log
55+
errorFilename: ./runtime/logs/cago.err.log
5556
filename: ./runtime/logs/cago.log
57+
# 不推荐该方式, 推荐使用`promtail`来抓取日志
5658
loki:
5759
level: info
5860
url: http://127.0.0.1:3100/loki/api/v1/push

deploy/helm/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ metadata:
44
name: {{ include "cago.fullname" . }}
55
labels:
66
{{- include "cago.labels" . | nindent 4 }}
7+
cago.io/version: {{ .Chart.AppVersion | quote }}
8+
cago.io/environment: {{ .Values.appConfig.env | quote }}
79
spec:
810
{{- if not .Values.autoscaling.enabled }}
911
replicas: {{ .Values.replicaCount }}

examples/simple/configs/config.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ http:
2323
- :8080
2424
logger:
2525
level: info
26-
logfile:
26+
disableConsole: false # 是否禁用控制台输出
27+
logFile:
2728
enable: true
28-
errorfilename: ./runtime/logs/cago.err.log
29+
errorFilename: ./runtime/logs/cago.err.log
2930
filename: ./runtime/logs/cago.log
31+
# 不推荐该方式, 推荐使用`promtail`来抓取日志
3032
loki:
3133
level: info
3234
url: http://127.0.0.1:3100/loki/api/v1/push

middleware/permission/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 权限控制组件
22

3-
> 后来并未用上改组件, 未完成
3+
> 后来并未用上该组件, 未完成
44
55
参考了ABAC、Casbin、ladon等,实现了一个简单的权限控制组件。

pkg/logger/global.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ func Logger(ctx context.Context, config *configs.Config) error {
4848
zapcore.Lock(os.Stdout),
4949
zapcore.DebugLevel,
5050
)))
51+
} else {
52+
if !cfg.DisableConsole {
53+
opts = append(opts, AppendCore(zapcore.NewCore(
54+
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
55+
zapcore.Lock(os.Stdout),
56+
ToLevel(cfg.Level),
57+
)))
58+
}
5159
}
5260
for _, f := range initLogger {
5361
o, err := f(ctx, config, cfg)

pkg/logger/logger.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import (
99
)
1010

1111
type Config struct {
12-
Level string
13-
LogFile LogFileConfig
12+
Level string
13+
DisableConsole bool `yaml:"disableConsole"`
14+
LogFile LogFileConfig `yaml:"logFile"`
1415
}
1516

1617
type LogFileConfig struct {
1718
Enable bool
1819
Filename string
19-
ErrorFilename string
20+
ErrorFilename string `yaml:"errorFilename"`
2021
}
2122

2223
func New(opt ...Option) (*zap.Logger, error) {

pkg/logger/loki/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# loki
2+
3+
通过API将日志提交到loki, 不推荐该方式, 推荐使用`promtail`来抓取日志
4+
5+
```yaml
6+
# 日志组件配置
7+
logger:
8+
level: info
9+
logfile:
10+
enable: true
11+
errorfilename: ./runtime/logs/cago.err.log
12+
filename: ./runtime/logs/cago.log
13+
# 不推荐该方式, 推荐使用`promtail`来抓取日志
14+
loki:
15+
level: info
16+
url: http://127.0.0.1:3100/loki/api/v1/push
17+
username: ""
18+
password: ""
19+
```
20+
21+
## k8s helm promtail配置
22+
23+
拉取promtail的helm chart, `scrapeConfigs`添加job
24+
25+
```yaml
26+
- job_name: cago
27+
kubernetes_sd_configs:
28+
- role: pod
29+
relabel_configs:
30+
- source_labels:
31+
- __meta_kubernetes_pod_label_app_kubernetes_io_name
32+
action: keep
33+
regex: cago
34+
- source_labels:
35+
- __meta_kubernetes_pod_label_app_kubernetes_io_instance
36+
regex: ^;*([^;]+)(;.*)?$
37+
action: replace
38+
target_label: instance
39+
- source_labels:
40+
- __meta_kubernetes_pod_label_app_kubernetes_io_instance
41+
regex: -([^-]+)$
42+
action: replace
43+
replacement: $1
44+
target_label: env
45+
```

server/mux/router.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func (r *Routes) RequestHandle(request interface{}, handlers ...gin.HandlerFunc)
5252
return r.requestHandle(requestEl, handlers...)
5353
}
5454

55+
func Metadata(request interface{}) (string, string) {
56+
requestEl := reflect.TypeOf(request)
57+
route, ok := requestEl.FieldByName("Meta")
58+
// 必须有Route字段
59+
if !ok || route.Type != reflect.TypeOf(Meta{}) {
60+
panic("invalid method, second parameter must have Meta field")
61+
}
62+
return route.Tag.Get("path"), route.Tag.Get("method")
63+
}
64+
5565
func (r *Routes) requestHandle(requestEl reflect.Type, handlers ...gin.HandlerFunc) *Routes {
5666
route, ok := requestEl.FieldByName("Meta")
5767
// 必须有Route字段

0 commit comments

Comments
 (0)