作者 yangfu

add redis cache log

@@ -14,7 +14,7 @@ require ( @@ -14,7 +14,7 @@ require (
14 //github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e 14 //github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e
15 //github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5 15 //github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5
16 //github.com/openzipkin/zipkin-go v0.2.5 16 //github.com/openzipkin/zipkin-go v0.2.5
17 - github.com/tiptok/gocomm v1.0.4 17 + github.com/tiptok/gocomm v1.0.6
18 ) 18 )
19 19
20 -//replace github.com/tiptok/gocomm v1.0.3 => F:\go\src\learn_project\gocomm 20 +//replace github.com/tiptok/gocomm v1.0.5 => F:\go\src\learn_project\gocomm
@@ -3,12 +3,15 @@ package main @@ -3,12 +3,15 @@ package main
3 import ( 3 import (
4 "github.com/astaxie/beego" 4 "github.com/astaxie/beego"
5 "github.com/tiptok/gocomm/config" 5 "github.com/tiptok/gocomm/config"
  6 + "github.com/tiptok/gocomm/pkg/cache"
6 "github.com/tiptok/gocomm/pkg/log" 7 "github.com/tiptok/gocomm/pkg/log"
  8 + "github.com/tiptok/gocomm/pkg/redis"
7 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant" 9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant"
8 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant" 10 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant"
9 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg" 11 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg"
10 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/redis" 12 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/redis"
11 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego" 13 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego"
  14 + //logx "log"
12 ) 15 )
13 16
14 func main() { 17 func main() {
@@ -22,6 +25,12 @@ func main() { @@ -22,6 +25,12 @@ func main() {
22 Level: constant.LOG_LEVEL, 25 Level: constant.LOG_LEVEL,
23 }) 26 })
24 27
  28 + cache.InitDefault(cache.WithDefaultRedisPool(redis.GetRedisPool()), cache.WithDebugLog(true, log.DefaultLog))
  29 +
  30 + //log.DefaultLog = console.NewConsoleLog(config.Logger{
  31 + // Filename: constant.LOG_File,
  32 + // Level: constant.LOG_LEVEL,
  33 + //}, fmt.Sprintf("[%v]", constant.POSTGRESQL_DB_NAME), 3)
25 //{ 34 //{
26 // // set up a span reporter 35 // // set up a span reporter
27 // reporter := zipkinhttp.NewReporter("http://106.52.15.41:9411/api/v2/spans") 36 // reporter := zipkinhttp.NewReporter("http://106.52.15.41:9411/api/v2/spans")
@@ -45,6 +54,7 @@ func main() { @@ -45,6 +54,7 @@ func main() {
45 // // optionally set as Global OpenTracing tracer instance 54 // // optionally set as Global OpenTracing tracer instance
46 // opentracing.SetGlobalTracer(tracer) 55 // opentracing.SetGlobalTracer(tracer)
47 //} 56 //}
  57 + log.Debug(constant.REDIS_HOST, constant.REDIS_PORT)
48 58
49 beego.Run() 59 beego.Run()
50 } 60 }
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "github.com/tiptok/gocomm/pkg/cache" 5 "github.com/tiptok/gocomm/pkg/cache"
6 "github.com/tiptok/gocomm/pkg/log" 6 "github.com/tiptok/gocomm/pkg/log"
7 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory" 7 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant"
8 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain" 9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/dao" 10 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/dao"
10 "regexp" 11 "regexp"
@@ -13,7 +14,7 @@ import ( @@ -13,7 +14,7 @@ import (
13 14
14 //redis key 15 //redis key
15 func UserRoleAccessCacheKey(userId int64) string { 16 func UserRoleAccessCacheKey(userId int64) string {
16 - return fmt.Sprintf("godevp:user:access:%v", userId) 17 + return fmt.Sprintf("%v:user:access:%v", constant.POSTGRESQL_DB_NAME, userId)
17 } 18 }
18 19
19 type CacheService struct { 20 type CacheService struct {
@@ -2,7 +2,6 @@ package redis @@ -2,7 +2,6 @@ package redis
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 - "github.com/tiptok/gocomm/pkg/cache"  
6 "github.com/tiptok/gocomm/pkg/log" 5 "github.com/tiptok/gocomm/pkg/log"
7 "github.com/tiptok/gocomm/pkg/redis" 6 "github.com/tiptok/gocomm/pkg/redis"
8 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant" 7 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant"
@@ -14,5 +13,4 @@ func init() { @@ -14,5 +13,4 @@ func init() {
14 if err != nil { 13 if err != nil {
15 log.Error(err) 14 log.Error(err)
16 } 15 }
17 - cache.InitDefault(cache.WithDefaultRedisPool(redis.GetRedisPool()))  
18 } 16 }
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 "github.com/tiptok/gocomm/common" 6 "github.com/tiptok/gocomm/common"
7 "github.com/tiptok/gocomm/pkg/cache" 7 "github.com/tiptok/gocomm/pkg/cache"
8 . "github.com/tiptok/gocomm/pkg/orm/pgx" 8 . "github.com/tiptok/gocomm/pkg/orm/pgx"
  9 + "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant"
9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain" 10 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
10 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models" 11 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
11 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction" 12 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
@@ -13,7 +14,7 @@ import ( @@ -13,7 +14,7 @@ import (
13 14
14 var ( 15 var (
15 cacheRoleIdKey = func(id int64) string { 16 cacheRoleIdKey = func(id int64) string {
16 - return fmt.Sprintf("%v:cache:Role:id:%v", "godevp", id) 17 + return fmt.Sprintf("%v:cache:Role:id:%v", constant.POSTGRESQL_DB_NAME, id)
17 } 18 }
18 ) 19 )
19 20
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "github.com/tiptok/gocomm/common" 5 "github.com/tiptok/gocomm/common"
6 "github.com/tiptok/gocomm/pkg/cache" 6 "github.com/tiptok/gocomm/pkg/cache"
7 . "github.com/tiptok/gocomm/pkg/orm/pgx" 7 . "github.com/tiptok/gocomm/pkg/orm/pgx"
  8 + "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant"
8 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain" 9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models" 10 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
10 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction" 11 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
@@ -12,7 +13,7 @@ import ( @@ -12,7 +13,7 @@ import (
12 13
13 var ( 14 var (
14 cacheUsersIdKey = func(id int64) string { 15 cacheUsersIdKey = func(id int64) string {
15 - return fmt.Sprintf("%v:cache:Users:id:%v", "godevp", id) 16 + return fmt.Sprintf("%v:cache:Users:id:%v", constant.POSTGRESQL_DB_NAME, id)
16 } 17 }
17 ) 18 )
18 19
  1 +package log
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/logs"
  6 + "github.com/tiptok/gocomm/config"
  7 + logx "github.com/tiptok/gocomm/pkg/log"
  8 + "log"
  9 + "strings"
  10 +)
  11 +
  12 +type ConsoleLog struct {
  13 + //log *log.Logger
  14 + config config.Logger
  15 + depth int
  16 + level int
  17 +}
  18 +
  19 +func NewConsoleLog(config config.Logger, prefix string, depth int) logx.Log {
  20 + if len(prefix) > 0 {
  21 + log.SetPrefix(prefix)
  22 + }
  23 + log.SetFlags(log.LstdFlags | log.Lshortfile)
  24 + return &ConsoleLog{
  25 + config: config,
  26 + depth: depth,
  27 + level: beegoLogLevelAdapter(config.Level),
  28 + }
  29 +}
  30 +
  31 +func (this *ConsoleLog) Debug(args ...interface{}) {
  32 + if this.level < logs.LevelDebug {
  33 + return
  34 + }
  35 + log.Output(this.depth, this.appendAhead(args, "[D]"))
  36 +}
  37 +
  38 +func (this *ConsoleLog) Info(args ...interface{}) {
  39 + if this.level < logs.LevelInfo {
  40 + return
  41 + }
  42 + log.Output(this.depth, this.appendAhead(args, "[I]"))
  43 +}
  44 +
  45 +func (this *ConsoleLog) Warn(args ...interface{}) {
  46 + if this.level < logs.LevelWarning {
  47 + return
  48 + }
  49 + log.Output(this.depth, this.appendAhead(args, "[W]"))
  50 +}
  51 +
  52 +func (this *ConsoleLog) Error(args ...interface{}) {
  53 + if this.level < logs.LevelError {
  54 + return
  55 + }
  56 + log.Output(this.depth, this.appendAhead(args, "[E]"))
  57 +}
  58 +
  59 +func (this *ConsoleLog) Panic(args ...interface{}) {
  60 + log.Output(this.depth, this.appendAhead(args, "[P]"))
  61 +}
  62 +
  63 +func (this *ConsoleLog) Fatal(args ...interface{}) {
  64 + log.Output(this.depth, this.appendAhead(args, "[F]"))
  65 +}
  66 +
  67 +func (this *ConsoleLog) appendAhead(args []interface{}, addArgs ...interface{}) string {
  68 + var rsp []interface{}
  69 + rsp = append(rsp, addArgs...)
  70 + rsp = append(rsp, args...)
  71 + return fmt.Sprint(rsp...)
  72 +}
  73 +
  74 +func beegoLogLevelAdapter(logLevel string) int {
  75 + switch strings.ToUpper(logLevel) {
  76 + case "DEBUG":
  77 + return logs.LevelDebug
  78 + case "INFO":
  79 + return logs.LevelInformational
  80 + case "WARN":
  81 + return logs.LevelWarning
  82 + case "ERROR":
  83 + return logs.LevelError
  84 + }
  85 + return logs.LevelDebug
  86 +}
1 package middleware 1 package middleware
2 2
3 import ( 3 import (
  4 + "bufio"
  5 + "bytes"
4 "encoding/json" 6 "encoding/json"
5 "errors" 7 "errors"
6 "fmt" 8 "fmt"
7 "github.com/astaxie/beego/context" 9 "github.com/astaxie/beego/context"
  10 + "io"
  11 +
8 //"github.com/opentracing/opentracing-go" 12 //"github.com/opentracing/opentracing-go"
9 auth "github.com/abbot/go-http-auth" 13 auth "github.com/abbot/go-http-auth"
10 "github.com/tiptok/gocomm/common" 14 "github.com/tiptok/gocomm/common"
  15 + "github.com/tiptok/gocomm/gs"
11 "github.com/tiptok/gocomm/pkg/log" 16 "github.com/tiptok/gocomm/pkg/log"
12 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/cachex" 17 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/cachex"
13 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol" 18 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
@@ -172,3 +177,36 @@ func BasicAuth() func(ctx *context.Context) { @@ -172,3 +177,36 @@ func BasicAuth() func(ctx *context.Context) {
172 } 177 }
173 } 178 }
174 } 179 }
  180 +
  181 +// http://127.0.0.1:8080/logf?source=dev/2021-01-08/mmmgodevp.2021-01-08.dev.txt
  182 +func LogF() func(ctx *context.Context) {
  183 + return func(ctx *context.Context) {
  184 + source := ctx.Input.Query("source")
  185 + client := gs.NewGateWayClient("http://dockerlogs.fjmaimaimai.com/")
  186 + client.AddApi("log", source, "")
  187 + request := client.NewRequest("log")
  188 + rsp, err := request.Response()
  189 + if err != nil {
  190 + ctx.Output.SetStatus(http.StatusInternalServerError)
  191 + ctx.Output.Body([]byte(source))
  192 + return
  193 + }
  194 +
  195 + type LogInfo struct {
  196 + LogContent string `json:"log"`
  197 + }
  198 + buf := bufio.NewReader(rsp.Body)
  199 + rspData := bytes.NewBuffer(nil)
  200 + for {
  201 + data, _, err := buf.ReadLine()
  202 + if err == io.EOF || err != nil {
  203 + break
  204 + }
  205 + logItem := &LogInfo{}
  206 + common.JsonUnmarshal(string(data), logItem)
  207 + rspData.WriteString(logItem.LogContent)
  208 + }
  209 + ctx.Output.Body(rspData.Bytes())
  210 + ctx.Output.ContentType("text/plain; charset=utf-8")
  211 + }
  212 +}
@@ -3,8 +3,11 @@ package routers @@ -3,8 +3,11 @@ package routers
3 import ( 3 import (
4 "github.com/astaxie/beego" 4 "github.com/astaxie/beego"
5 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant" 5 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant"
  6 + "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/middleware"
6 ) 7 )
7 8
8 func init() { 9 func init() {
9 beego.SetStaticPath("/log", constant.LOG_File) 10 beego.SetStaticPath("/log", constant.LOG_File)
  11 +
  12 + beego.Get("/logf", middleware.LogF())
10 } 13 }
  1 +
  2 +
  3 +## 1. 客户端工具下载
  4 +
  5 +### 1.1 版本 v0.1.0
  6 +
  7 +https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20201228/object/1609137707_XsBEXcRxJscRHCzQmxeseTAEQXTH7A7Z.zip
  8 +
  9 +
  10 +
  11 +## 2. 账号登录
  12 +
  13 +
  14 +
  15 +账号需要向平台服务申请,用户名为 申请人手机号 默认密码 mmm123456
  16 +
  17 +![image-20201231152315657](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504740_smhbM5mmBrHjpytcydMNs2xm4AKjA2xY.png)
  18 +
  19 +
  20 +
  21 +## 3. 项目初始化
  22 +
  23 +
  24 +
  25 +设置项目/描述文件路径
  26 +
  27 +![image-20201231152940513](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504746_eTAEQXTH7A7ZK6z4FzZ2YeDwX3KwcGdD.png)
  28 +
  29 +
  30 +
  31 +## 4. 应用描述文件生成
  32 +
  33 +### 4.1 添加属性描述 (attribute)
  34 +
  35 +![image-20201231153445643](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504750_tEYHSbtbSdasD2PM4QGX3yZtx33xATYN.png)
  36 +
  37 +属性是描述文件的最小单元
  38 +
  39 +attibute名称
  40 +
  41 +```
  42 +例如此处 taskId
  43 +属性名称会影响到描述文件的文件名称,这边会生成的描述文件
  44 +
  45 +taskId.yaml
  46 +```
  47 +
  48 +attribute类型 包含
  49 +
  50 +```
  51 +primitive: 原始类型,包括:string,boolean,int,int32,int64,uint,uint32,uint64,float32,float64,datetime,bytes
  52 +array: 原始类型数组,或者自定义模式数组,例子:string或者task
  53 +schema: 自定义模式,Schema描述的name字段,例子:task
  54 +```
  55 +
  56 +attribute 所在文件名称
  57 +
  58 +```
  59 +例如此处 task
  60 +生成的描述文件就会保存在 /task/taskId.yaml
  61 +```
  62 +
  63 +文件内容如下
  64 +
  65 +```
  66 +version: v1
  67 +kind: Attribute
  68 +metadata:
  69 + name: taskId
  70 + description: 任务编号
  71 + type:
  72 + primitive: int64
  73 +```
  74 +
  75 +
  76 +
  77 +
  78 +
  79 +### 4.2 添加结构描述(schema)
  80 +
  81 +
  82 +
  83 +![image-20201231155037423](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504756_dFnFtQKEHHxJknQFEAK5rmXDfSrhxjA7.png)
  84 +
  85 +生成描述文件 /schemas/task.yaml
  86 +
  87 +```
  88 +version: v1
  89 +kind: Schema
  90 +metadata:
  91 + name: task
  92 + description: 任务实体
  93 + attributes:
  94 + - ref: taskId
  95 + required: true
  96 + - ref: taskName
  97 + required: true
  98 + - name: taskDescription
  99 + description: 任务描述-(这边写入自定义属性的描述)
  100 + type:
  101 + primitive: string
  102 +```
  103 +
  104 +
  105 +
  106 +### 4.3 添加服务描述 (service)
  107 +
  108 +
  109 +
  110 +![image-20201231155415791](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504761_er4FArQSQfbbxQPBPnFDeRabpJ3k7kzS.png)
  111 +
  112 +method名称 :指定CURD 工具会自动生成服务方法 create、get、list、remove、update
  113 +
  114 +```
  115 +F:\eggcode\dsl\godevp\services\task\methods\createTask.yaml
  116 +F:\eggcode\dsl\godevp\services\task\methods\getTask.yaml
  117 +F:\eggcode\dsl\godevp\services\task\methods\listTask.yaml
  118 +F:\eggcode\dsl\godevp\services\task\methods\removeTask.yaml
  119 +F:\eggcode\dsl\godevp\services\task\methods\updateTask.yaml
  120 +F:\eggcode\dsl\godevp\services\task\service.yaml
  121 +```
  122 +
  123 +
  124 +
  125 +
  126 +
  127 +### 4.4 添加接口描述(api)
  128 +
  129 +![image-20201231161927395](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504767_6tbPWDQksxsJKR5Gneb5AYJTABjkNEAG.png)
  130 +
  131 +
  132 +
  133 +生成描述文件 /http/task.yaml
  134 +
  135 +```
  136 +version: v1
  137 +kind: HttpApi
  138 +metadata:
  139 + service: task
  140 + path: /tasks
  141 + endpoints:
  142 + - method: createTask
  143 + route:
  144 + post: /
  145 + - method: updateTask
  146 + route:
  147 + put: /{taskId} ##注意这边需要手动加上task
  148 + - method: getTask
  149 + route:
  150 + get: /{taskId} ##注意这边需要手动加上task
  151 + - method: removeTask
  152 + route:
  153 + delete: /{taskId} ##注意这边需要手动加上task
  154 + - method: listTask
  155 + route:
  156 + get: /
  157 + params:
  158 + - name: offset
  159 + - name: limit
  160 +```
  161 +
  162 +
  163 +
  164 +## 5. 生成项目代码
  165 +
  166 +### 5.1 生成项目脚手架 (基础文件)
  167 +
  168 +指定web框架、数据库
  169 +
  170 +![image-20201231162224099](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504772_dbtZePks2hW2nNAhX4CRCmwczeFaK5Gz.png)
  171 +
  172 +
  173 +
  174 +### 5.2 生成领域模型 (domain相关文件)
  175 +
  176 +![image-20201231163250901](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504777_EnmnrTR7kA34XNFMPGeRFYJfcJ7pysry.png)
  177 +
  178 +
  179 +
  180 +### 5.3 生成应用服务 (application)
  181 +
  182 +![image-20201231163332731](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504782_MXKXHYEbKsRskTyRGYkmFrdwfRHEMc33.png)
  183 +
  184 +
  185 +
  186 +### 5.4 生成http服务文件(路由控制器)
  187 +
  188 +![image-20201231163433314](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504787_XC4dtwzeBhkCDt6CDF3QPJb2TMTBJyxc.png)
  189 +
  190 +
  191 +
  192 +
  193 +
  194 +## 6. 生成文档
  195 +
  196 +![image-20201231163458590](https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210113/object/1610504792_DKThDyRJmAk3mt7j6ypM3BYxceC7AWzX.png)