Skip to content

Commit 17fb552

Browse files
committed
add glog
1 parent b253b90 commit 17fb552

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

glog.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package glog
2+
3+
import (
4+
"fmt"
5+
"github.com/sirupsen/logrus"
6+
"io"
7+
"os"
8+
"path"
9+
"runtime"
10+
)
11+
12+
// writerHook is a structure that defines a hook for logrus.
13+
// It contains a slice of io.Writer where logs will be written,
14+
// and LogLevels, which define the log levels that the hook will process.
15+
type writerHook struct {
16+
Writer []io.Writer
17+
LogLevels []logrus.Level
18+
}
19+
20+
// Fire is called by logrus when a log entry is fired.
21+
// It writes the log entry to all the writers defined in the hook.
22+
// Returns an error if writing to any writer fails.
23+
func (hook *writerHook) Fire(entry *logrus.Entry) error {
24+
line, err := entry.String()
25+
if err != nil {
26+
return err
27+
}
28+
29+
for _, w := range hook.Writer {
30+
if _, err := w.Write([]byte(line)); err != nil {
31+
return fmt.Errorf("failed to write log entry to writer: %v", err)
32+
}
33+
}
34+
return err
35+
}
36+
37+
// Levels returns the log levels that the hook will process.
38+
// This method is required by logrus' Hook interface.
39+
func (hook *writerHook) Levels() []logrus.Level {
40+
return hook.LogLevels
41+
}
42+
43+
var e *logrus.Entry
44+
45+
// Logger is a wrapper around logrus.Entry to provide additional methods.
46+
type Logger struct {
47+
*logrus.Entry
48+
}
49+
50+
// GetLogger returns the global logger instance.
51+
func GetLogger() *Logger {
52+
return &Logger{e}
53+
}
54+
55+
// GetLoggerWithField returns a new Logger with a specific field added to the log entries.
56+
func (l *Logger) GetLoggerWithField(k string, v interface{}) *Logger {
57+
return &Logger{l.WithField(k, v)}
58+
}
59+
60+
// init initializes the logger, sets formatting, output writers, and log levels.
61+
func init() {
62+
l := logrus.New()
63+
64+
l.SetReportCaller(true)
65+
66+
l.Formatter = &logrus.TextFormatter{
67+
CallerPrettyfier: func(frame *runtime.Frame) (function string, file string) {
68+
filename := path.Base(frame.File)
69+
return fmt.Sprintf("%s()", frame.Function), fmt.Sprintf("%s:%d", filename, frame.Line)
70+
},
71+
DisableColors: false,
72+
FullTimestamp: true,
73+
}
74+
75+
err := os.MkdirAll("logs", 0755)
76+
if err != nil {
77+
panic(err)
78+
}
79+
80+
allFile, err := os.OpenFile("logs/all.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
81+
if err != nil {
82+
panic(err)
83+
}
84+
85+
fmt.Printf("all log file: %s\n", allFile.Name())
86+
87+
l.SetOutput(io.Discard)
88+
89+
l.AddHook(&writerHook{
90+
Writer: []io.Writer{allFile, os.Stdout},
91+
LogLevels: logrus.AllLevels,
92+
})
93+
94+
l.SetLevel(logrus.TraceLevel)
95+
96+
e = logrus.NewEntry(l)
97+
}

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/EvvTim/glog
2+
3+
go 1.23.0
4+
5+
require (
6+
github.com/sirupsen/logrus v1.9.3 // indirect
7+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
8+
)

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
5+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
9+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)