From fcfbf909919a02d05ad4c7cd4ca49fe05192fa56 Mon Sep 17 00:00:00 2001 From: Guoqiang Chen Date: Thu, 17 May 2018 15:48:37 +0000 Subject: [PATCH] Fixes fileline in formatters --- examples/main.go | 25 +++++++++++++++++++++++++ formatters/json_formatter.go | 2 +- formatters/text_formatter.go | 4 ++-- formatters/util_fileline.go | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 examples/main.go diff --git a/examples/main.go b/examples/main.go new file mode 100644 index 0000000..0a0f574 --- /dev/null +++ b/examples/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "os" + + "github.com/subchen/go-log" + "github.com/subchen/go-log/formatters" +) + +func main() { + log.Info("hello", 123) + log.Warn("hello", 123) + + log.Default.Formatter = new(formatters.TextFormatter) + log.Infoln("hello", "world") + log.Warnln("hello", "world") + + newLog := &log.Logger{ + Level: log.INFO, + Formatter: new(formatters.JSONFormatter), + Out: os.Stdout, + } + newLog.Infof("hello %v", 123) + newLog.Warnf("hello %v", 123) +} diff --git a/formatters/json_formatter.go b/formatters/json_formatter.go index d08b9a8..ae05d4e 100644 --- a/formatters/json_formatter.go +++ b/formatters/json_formatter.go @@ -41,7 +41,7 @@ func (f JSONFormatter) Format(level log.Level, msg string, logger *log.Logger) [ data := make(map[string]interface{}, 8) // file, line - file, line := FilelineCaller(7) + file, line := FilelineCaller(5) data["time"] = time.Now().Format(f.TimeFormat) data["level"] = level.String() diff --git a/formatters/text_formatter.go b/formatters/text_formatter.go index be05ce2..a332f7c 100644 --- a/formatters/text_formatter.go +++ b/formatters/text_formatter.go @@ -83,14 +83,14 @@ func (f TextFormatter) Format(level log.Level, msg string, logger *log.Logger) [ buf.Write(f.pid) // file, line - file, line := FilelineCaller(7) + file, line := FilelineCaller(5) buf.WriteByte(' ') buf.WriteString(file) buf.WriteByte(':') buf.WriteString(strconv.Itoa(line)) - buf.WriteByte('"') // msg + buf.WriteByte(' ') buf.WriteString(msg) // newline diff --git a/formatters/util_fileline.go b/formatters/util_fileline.go index 9c9cfbb..9288439 100644 --- a/formatters/util_fileline.go +++ b/formatters/util_fileline.go @@ -24,7 +24,7 @@ func FilelineCaller(skip int) (file string, line int) { } } - if !strings.HasPrefix(file, "log/") { + if !strings.HasPrefix(file, "go-log/") { return file, line } }