Skip to content

Commit

Permalink
fix: fix getLog panic nil point
Browse files Browse the repository at this point in the history
  • Loading branch information
panco committed Apr 13, 2022
1 parent 24864f5 commit 6694655
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 33 deletions.
10 changes: 7 additions & 3 deletions core/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"errors"
"fmt"

"github.com/go-redis/redis/v8"
clientV3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
Expand Down Expand Up @@ -38,9 +39,12 @@ func (g *Garden) Set(name string, val interface{}) error {
}

//GetLog instance to write custom Logs
func (g *Garden) GetLog() *zap.SugaredLogger {
res, _ := g.Get("log")
return res.(*zap.SugaredLogger)
func (g *Garden) GetLog() (*zap.SugaredLogger, error) {
res, err := g.Get("log")
if err != nil {
return nil, err
}
return res.(*zap.SugaredLogger), nil
}

//GetDb instance to performing database operations
Expand Down
29 changes: 18 additions & 11 deletions core/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package core

import (
"fmt"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"log"
"os"
"time"

"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func (g *Garden) bootLog() {
Expand All @@ -30,14 +31,24 @@ func (g *Garden) bootLog() {

l := logger.Sugar()
g.setSafe("log", l)

g.logBoot = 1
}

// Log format to write log file and print to shell if debug set true
func (g *Garden) Log(level logLevel, label string, data interface{}) {
l := g.GetLog()
format := logFormat(label, data)
l, err := g.GetLog()
if err != nil {
switch level {
case PanicLevel:
log.Panic(format)
case FatalLevel:
log.Fatal(format)
default:
log.Print(format)
}
return
}

switch level {
case DebugLevel:
l.Debug(format)
Expand All @@ -52,11 +63,7 @@ func (g *Garden) Log(level logLevel, label string, data interface{}) {
case PanicLevel:
l.Panic(format)
case FatalLevel:
if g.logBoot == 1 {
l.Fatal(format)
} else {
log.Fatal(format)
}
l.Fatal(format)
}
}

Expand Down
1 change: 0 additions & 1 deletion core/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type (
Garden struct {
container sync.Map
cfg cfg
logBoot uint
serviceType uint //0:service 1:gateway
services map[string]*service
serviceManager chan serviceOperate
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ global.Garden.Log(core.DebugLevel,"container test", res)

### 十三、消息队列

暂无
试着尝试模仿mysql与redis的自定义配置项,在你的项目里把消息队列rabbitmq配置定义并获取后封装到容器全局变量?

### 十四、负载均衡

Expand Down
10 changes: 5 additions & 5 deletions examples/gateway/configs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ service:

etcdKey: garden
etcdAddress:
- 192.168.125.194:2379
- 127.0.0.1:2379

tracerDrive: jaeger
jaegerAddress: 192.168.125.194:6831
#zipkinAddress: http://192.168.125.194:9411/api/v2/spans
jaegerAddress: 127.0.0.1:6831
#zipkinAddress: http://127.0.0.1:9411/api/v2/spans

pushGatewayAddress: 192.168.125.194:9091
pushGatewayAddress: 127.0.0.1:9091

config:
#redis:
Expand All @@ -34,7 +34,7 @@ config:
#port: "3306"
#user: "root"
#pass: ""
#dbname: "test"
#dbname: "garden_example"
#charset: "utf8mb4"
#parseTime: true
#connPool: 10
8 changes: 4 additions & 4 deletions examples/pay/configs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ service:

etcdKey: garden
etcdAddress:
- 192.168.125.194:2379
- 127.0.0.1:2379

tracerDrive: jaeger
jaegerAddress: 192.168.125.194:6831
#zipkinAddress: http://192.168.125.194:9411/api/v2/spans
jaegerAddress: 127.0.0.1:6831
#zipkinAddress: http://127.0.0.1:9411/api/v2/spans

pushGatewayAddress: 192.168.125.194:9091
pushGatewayAddress: 127.0.0.1:9091

config:
db:
Expand Down
8 changes: 4 additions & 4 deletions examples/user/configs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ service:

etcdKey: garden
etcdAddress:
- 192.168.125.194:2379
- 127.0.0.1:2379

tracerDrive: jaeger
jaegerAddress: 192.168.125.194:6831
#zipkinAddress: http://192.168.125.194:9411/api/v2/spans
jaegerAddress: 127.0.0.1:6831
#zipkinAddress: http://127.0.0.1:9411/api/v2/spans

pushGatewayAddress: 192.168.125.194:9091
pushGatewayAddress: 127.0.0.1:9091

config:
db:
Expand Down
8 changes: 4 additions & 4 deletions examples/user2/configs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ service:

etcdKey: garden
etcdAddress:
- 192.168.125.194:2379
- 127.0.0.1:2379

tracerDrive: jaeger
jaegerAddress: 192.168.125.194:6831
#zipkinAddress: http://192.168.125.194:9411/api/v2/spans
jaegerAddress: 127.0.0.1:6831
#zipkinAddress: http://127.0.0.1:9411/api/v2/spans

pushGatewayAddress: 192.168.125.194:9091
pushGatewayAddress: 127.0.0.1:9091

config:
db:
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ github地址:https://github.com/panco95/go-garden

- **服务监控与警报**

- **可选组件:数据库、缓存等**

- **统一日志存储**

- **脚手架工具**
Expand Down

0 comments on commit 6694655

Please sign in to comment.