Skip to content

Commit c80d24e

Browse files
committed
logrus
1 parent 0b49a55 commit c80d24e

File tree

17 files changed

+321
-0
lines changed

17 files changed

+321
-0
lines changed

logrus/00-logrus-baisc.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/sirupsen/logrus"
7+
)
8+
9+
func main() {
10+
log := logrus.New()
11+
log.Debug("lgorus new") // 这一句不会打印到文件中,因为此时还没有设置setOuput
12+
file, err := os.OpenFile("debug.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModeExclusive)
13+
if err != nil {
14+
log.Fatalf("create file debug.log failed:%v", err) // 所以这一句日志也会丢失, 应该用fmt
15+
}
16+
defer file.Close()
17+
log.SetLevel(logrus.DebugLevel)
18+
log.Formatter = &logrus.JSONFormatter{}
19+
log.SetOutput(file)
20+
log.WithField("key", "value").Info("test for withfield")
21+
log.WithFields(logrus.Fields{
22+
"animal": "walrus",
23+
"size": 10,
24+
}).Info("A group of walrus emerges from the ocean")
25+
}

logrus/01-lgorus-scope.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/researchlab/gbp/logrus/scope/a/a1"
8+
"github.com/researchlab/gbp/logrus/scope/a/a2"
9+
"github.com/researchlab/gbp/logrus/scope/b"
10+
"github.com/researchlab/gbp/logrus/scope/logs"
11+
)
12+
13+
var mLog = logs.Entry().WithField("scope", "main")
14+
15+
func main() {
16+
logFile, err := os.OpenFile("./data/scope.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModeExclusive)
17+
if err != nil {
18+
fmt.Println("create file scope.log failed:", err) // 所以这一句日志也会丢失, 应该用fmt
19+
}
20+
defer logFile.Close()
21+
logs.Init(logFile)
22+
mLog.Info("main start")
23+
a1.Foo()
24+
b.Boo()
25+
a2.Doo()
26+
}

logrus/02-logrus-hook.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"github.com/researchlab/gbp/logrus/hook/logs"
5+
"github.com/researchlab/gbp/logrus/hook/pkg/a"
6+
log "github.com/sirupsen/logrus"
7+
)
8+
9+
func main() {
10+
logs.InitLog("debug")
11+
log.Info("main start")
12+
a.Aoo()
13+
}

logrus/03-runtime-caller.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
func main() {
9+
foo()
10+
fmt.Println("boo----")
11+
boo()
12+
fmt.Println("coo----")
13+
coo()
14+
fmt.Println("doo----")
15+
doo()
16+
}
17+
18+
19+
func foo() {
20+
pc, file, line, ok := runtime.Caller(1)
21+
fmt.Println("pc:", pc)
22+
fmt.Println("file:", file)
23+
fmt.Println("line:", line)
24+
fmt.Println("ok:", ok)
25+
}
26+
27+
func boo() {
28+
pc, file, line, ok := runtime.Caller(1)
29+
fmt.Println("pc:", pc)
30+
fmt.Println("file:", file)
31+
fmt.Println("line:", line)
32+
fmt.Println("ok:", ok)
33+
}
34+
35+
func coo() {
36+
log()
37+
}
38+
func doo() {
39+
log()
40+
}
41+
func log() {
42+
pc, file, line, ok := runtime.Caller(2)
43+
fmt.Println("pc:", pc)
44+
fmt.Println("file:", file)
45+
fmt.Println("line:", line)
46+
fmt.Println("ok:", ok)
47+
}

logrus/data/hook.log

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[info] 2022-11-10 18:05:21.055 /Users/lihong/workbench/dev/src/github.com/sirupsen/logrus/logger.go:226 main start
2+
[info] 2022-11-10 18:05:42.054 /Users/lihong/workbench/dev/src/github.com/sirupsen/logrus/logger.go:226 main start
3+
[info] 2022-11-10 18:06:18.010 /Users/lihong/workbench/dev/src/github.com/sirupsen/logrus/logger.go:226 main start
4+
[info] 2022-11-10 18:08:32.252 /Users/lihong/workbench/dev/src/github.com/sirupsen/logrus/logger.go:226 main start
5+
[info] 2022-11-10 18:08:32.252 /Users/lihong/workbench/dev/src/github.com/sirupsen/logrus/logger.go:226 Aoo test
6+
[info] 2022-11-10 18:08:32.252 /Users/lihong/workbench/dev/src/github.com/researchlab/gbp/logrus/hook/pkg/a/a.go:9 ALog test
7+
{
8+
"level": "info",
9+
"msg": "main start",
10+
"time": "2022-11-10 18:19:57"
11+
}
12+
{
13+
"level": "info",
14+
"msg": "Aoo test",
15+
"time": "2022-11-10 18:19:57"
16+
}
17+
{
18+
"key": "aLog",
19+
"level": "info",
20+
"msg": "ALog test",
21+
"time": "2022-11-10 18:19:57"
22+
}
23+
{"level":"info","msg":"main start","time":"2022-11-10 18:20:38"}
24+
{"level":"info","msg":"Aoo test","time":"2022-11-10 18:20:38"}
25+
{"key":"aLog","level":"info","msg":"ALog test","time":"2022-11-10 18:20:38"}

logrus/data/scope.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

logrus/debug.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"key":"value","level":"info","msg":"test for withfield","time":"2022-11-10T16:17:36+08:00"}
2+
{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the ocean","size":10,"time":"2022-11-10T16:17:36+08:00"}

logrus/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module logrus
2+
3+
go 1.17
4+
5+
require github.com/sirupsen/logrus v1.9.0
6+
7+
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

logrus/go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
7+
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
8+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
10+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
12+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
14+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

logrus/hook/logs/logs.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package logs
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"runtime"
8+
9+
"github.com/rifflock/lfshook"
10+
log "github.com/sirupsen/logrus"
11+
)
12+
13+
const logDir = "./data"
14+
const logPath = "hook"
15+
16+
type MyJSONFormatter struct {
17+
Time string `json:"time"`
18+
File string `json:"file"`
19+
Line int `json:"line"`
20+
Level string `json:"level"`
21+
Info string `json:"info"`
22+
Msg string `json:"msg"`
23+
}
24+
25+
func (f *MyJSONFormatter) Format(entry *log.Entry) ([]byte, error) {
26+
logrusJF := &(log.JSONFormatter{})
27+
logrusJF.TimestampFormat = "2006-01-02 15:04:05.000"
28+
bytes, _ := logrusJF.Format(entry)
29+
json.Unmarshal(bytes, &f)
30+
if _, file, no, ok := runtime.Caller(8); ok {
31+
f.File = file
32+
f.Line = no
33+
}
34+
str := fmt.Sprintf("[%s] %s %s:%d %s\n", f.Level, f.Time, f.File, f.Line, f.Msg)
35+
return []byte(str), nil
36+
}
37+
38+
func InitLog(level string) (err error) {
39+
err = setAppLog(level)
40+
return
41+
}
42+
43+
func DefaultJSONFormatter()*log.JSONFormatter{
44+
return &log.JSONFormatter{
45+
TimestampFormat:"2006-01-02 15:04:05",
46+
//PrettyPrint:true, 表示Json展开打印
47+
}
48+
}
49+
func setAppLog(level string) (err error) {
50+
switch level {
51+
case "debug":
52+
log.SetLevel(log.DebugLevel)
53+
case "info":
54+
log.SetLevel(log.InfoLevel)
55+
case "warn":
56+
log.SetLevel(log.WarnLevel)
57+
case "error":
58+
log.SetLevel(log.ErrorLevel)
59+
default:
60+
log.Fatal("log conf only allow [debug, info,warn,error], please check your confguire")
61+
}
62+
63+
//os.MkdirAll(logDir+"/"+logPath, os.ModeDir)
64+
//var path = logDir + "/" + logPath + "/" + logPath + ".log"
65+
var path = logDir + "/" + logPath + ".log"
66+
log.SetOutput(ioutil.Discard)
67+
log.AddHook(lfshook.NewHook(
68+
lfshook.PathMap{
69+
log.InfoLevel: path,
70+
log.DebugLevel: path,
71+
log.WarnLevel: path,
72+
log.ErrorLevel: path,
73+
log.FatalLevel: path,
74+
log.PanicLevel: path,
75+
},
76+
//&MyJSONFormatter{}))
77+
DefaultJSONFormatter()))
78+
return nil
79+
}

logrus/hook/pkg/a/a.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package a
2+
import (
3+
log "github.com/sirupsen/logrus"
4+
)
5+
6+
var aLog = log.WithField("key","aLog")
7+
func Aoo(){
8+
log.Info("Aoo test")
9+
aLog.Info("ALog test")
10+
}

logrus/scope/a/a.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package a
2+
3+
import "github.com/researchlab/gbp/logrus/scope/logs"
4+
5+
// common
6+
7+
var ALog = logs.Entry().WithField("scope","a")

logrus/scope/a/a1/a1.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package a1
2+
import "github.com/researchlab/gbp/logrus/scope/a"
3+
import "github.com/sirupsen/logrus"
4+
func Foo(){
5+
a.ALog.Info("a1 foo test")
6+
a.ALog.WithFields(logrus.Fields{
7+
"key":"a1",
8+
"size":10,
9+
}).Error("with fields test for a1")
10+
}

logrus/scope/a/a2/a2.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package a2
2+
import "github.com/sirupsen/logrus"
3+
4+
var a2Log = logrus.WithField("scope","a2Log")
5+
func Doo(){
6+
a2Log.Info("Doo test")
7+
Eoo()
8+
}
9+
10+
func Eoo(){
11+
a2Log.Info("Eoo test")
12+
}

logrus/scope/b/b.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package b
2+
3+
import "github.com/researchlab/gbp/logrus/scope/logs"
4+
import "github.com/researchlab/gbp/logrus/scope/c"
5+
6+
// commone
7+
var bLog = logs.Entry()
8+
9+
func Boo(){
10+
bLog.WithField("func","boo").Info("b boo test")
11+
c.Coo()
12+
}

logrus/scope/c/c.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package c
2+
import "github.com/researchlab/gbp/logrus/scope/logs"
3+
var cLog = logs.Entry().WithField("scope","c")
4+
5+
func Coo(){
6+
cLog.Info("Coo test")
7+
}

logrus/scope/logs/log.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package logs
2+
3+
import (
4+
"os"
5+
6+
"github.com/sirupsen/logrus"
7+
)
8+
9+
var serviceLog = logrus.New()
10+
11+
func Init(logFile *os.File) {
12+
serviceLog.SetLevel(logrus.DebugLevel)
13+
serviceLog.Formatter = &logrus.JSONFormatter{}
14+
serviceLog.SetOutput(logFile)
15+
}
16+
17+
func Logger() *logrus.Logger {
18+
return serviceLog
19+
}
20+
21+
func Entry() *logrus.Entry {
22+
return serviceLog.WithField("service", "scope")
23+
}

0 commit comments

Comments
 (0)