Skip to content

Commit

Permalink
feat(log): support loki log but gozero only support one writer
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix-X committed Jan 13, 2025
1 parent 240bc98 commit 73429da
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 268 deletions.
34 changes: 26 additions & 8 deletions cmd/server/powerx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"PowerX/internal/middleware/recovery"
"PowerX/internal/model"
"PowerX/pkg/pluginx"
"PowerX/pkg/zerox"
"PowerX/pkg/zerox/log"
"os"

"context"
"flag"
"fmt"
"path/filepath"

"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
"path/filepath"

"PowerX/internal/config"
"PowerX/internal/handler"
Expand All @@ -20,6 +23,7 @@ import (
zgin "github.com/zeromicro/zero-contrib/router/gin"

"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx"
)

// 定义了一个全局变量 configFile,用于指定配置文件的路径,默认为 etc/powerx.yaml。
Expand All @@ -34,6 +38,23 @@ func main() {
var c config.Config
conf.MustLoad(*configFile, &c)

// ---------- 设置日志 ----------
if c.Log.Loki.Enabled {
c.Log.Loki.Labels = map[string]string{
"project": c.Server.Name,
"host": c.Server.Host,
"env": c.Env,
"region": c.Region,
"version": c.Version,
"filename": c.Log.Logx.Path,
"job": "WebServer",
"node_id": fmt.Sprintf("node_%d", os.Getpid()),
"service_name": c.Log.Logx.ServiceName,
"level": c.Log.Logx.Level,
}
}
log.MustSetupLog(&c.Log)

// 设置配置文件目录
c.EtcDir = filepath.Dir(*configFile)
model.PowerXSchema = c.PowerXDatabase.Schema
Expand All @@ -44,8 +65,8 @@ func main() {
r := zgin.NewRouter()

// 创建 Go-Zero REST 服务器实例
server := rest.MustNewServer(c.Server, rest.WithRouter(r))

// server := rest.MustNewServer(c.Server, rest.WithRouter(r))
var server *rest.Server
// 设置跨域配置
runOpt := config.SetupCors(&c)
if runOpt == nil {
Expand All @@ -56,9 +77,6 @@ func main() {
// 确保服务器关闭
defer server.Stop()

// ---------- 设置日志 ----------
zerox.MustSetupLog(c.Log)

// ---------- 创建插件管理器实例 ----------
plugin := pluginx.NewManager(context.Background(), r, fmt.Sprintf("%s:%d", "127.0.0.1", c.Server.Port))
// 创建服务上下文
Expand Down Expand Up @@ -87,6 +105,6 @@ func main() {
httpx.SetErrorHandlerCtx(handler.ErrorHandleCtx)

// ---------- 启动服务器 ----------
fmt.Printf("Starting server at %s:%d...\n", c.Server.Host, c.Server.Port)
logx.Infof("Starting server at %s:%d...\n", c.Server.Host, c.Server.Port)
server.Start()
}
32 changes: 25 additions & 7 deletions etc/powerx-example.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Version: v1.0.0 # 版本
Env: develop # 环境
Region: global
Server:
Name: PowerX后台系统 # 服务器名称
Name: PowerX后台系统 # 服务器名称
Host: 0.0.0.0 # 服务器地址
Port: 8888 # 服务器端口
Timeout: 30000 # 超时时间
Expand All @@ -13,13 +15,29 @@ Cors:
# - api.powerx.cn

Log:
Path: logs
Split:
- error
Level: info
KeepDays: 30
Console: true
Stat: false
Logx:
ServiceName: powerx
Mode: file
Encoding: plain
TimeFormat: "2006-01-02 15:04:05"
Path: logs
Level: info
MaxContentLength: 10485760
Compress: false
Stat: false
KeepDays: 30
StackCooldownMillis: 100
MaxBackups: 0
MaxSize: 0
Rotation: daily # `json:",default=daily,options=[daily,size]"`

Loki:
Enabled: true
URL: http://localhost:3100/loki/api/v1/push
Labels:
app: powerx
env: develop

Root:
Account: root # 管理员账号
Expand Down
26 changes: 13 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ require (
github.com/brianvoe/gofakeit/v6 v6.21.0
github.com/casbin/casbin/v2 v2.60.0
github.com/gin-gonic/gin v1.9.0
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/golang-module/carbon/v2 v2.2.3
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.0
github.com/kr/pretty v0.3.1
github.com/minio/minio-go/v7 v7.0.52
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
github.com/urfave/cli/v2 v2.11.0
github.com/zeromicro/go-zero v1.7.2
github.com/zeromicro/go-zero v1.7.6
github.com/zeromicro/go-zero/tools/goctl v1.7.2
github.com/zeromicro/zero-contrib/router/gin v0.0.0-20230708101417-16e73a5d116c
golang.org/x/crypto v0.27.0
golang.org/x/crypto v0.31.0
golang.org/x/oauth2 v0.22.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/datatypes v1.1.1
Expand All @@ -49,7 +49,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand Down Expand Up @@ -84,11 +84,11 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.20.2 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.58.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand All @@ -110,18 +110,18 @@ require (
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
go.uber.org/automaxprocs v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 73429da

Please sign in to comment.