From e3360c3bfd1b266feaefcd9ec97e94da61a85dc6 Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Wed, 8 Feb 2023 22:12:15 +0800 Subject: [PATCH] logfile --- go.mod | 6 +++++- logfile.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 logfile.go diff --git a/go.mod b/go.mod index 46c1b78..025fdd3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module github.com/smbrave/goutil -go 1.19 +go 1.18 + +require github.com/sirupsen/logrus v1.9.0 + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/logfile.go b/logfile.go new file mode 100644 index 0000000..11eed79 --- /dev/null +++ b/logfile.go @@ -0,0 +1,22 @@ +package goutil + +import ( + "fmt" + log "github.com/sirupsen/logrus" + "path/filepath" + "strings" +) + +type LogFile struct { +} + +func (f *LogFile) Format(entry *log.Entry) ([]byte, error) { + funcs := strings.SplitN(entry.Caller.Function, ".", 2) + msg := fmt.Sprintf("[%s] [%s] [%s:%d] %s\n", + entry.Time.Format("2006-01-02 15:04:05,000"), + strings.ToUpper(entry.Level.String()), + funcs[0]+"/"+filepath.Base(entry.Caller.File), + entry.Caller.Line, + entry.Message) + return []byte(msg), nil +}