Skip to content

Commit

Permalink
Merge pull request #15 from EdgarTeng/feature/fields-support
Browse files Browse the repository at this point in the history
Feature/fields support
  • Loading branch information
edtenz authored Jul 15, 2021
2 parents c7bce59 + 9f772bb commit 195669b
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 156 deletions.
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
# etlog

**etlog** is a log component for go.


**etlog** is a log component for go.

## Features

- Support log level
- Stdout and file appender
- log markers support



## Quick start



1. Simple usage

```go
etlog.Log.Info("Hello World")
```
```go
etlog.Log.Info("Hello World")
```

It will output the log into stdout device(console log).

Expand All @@ -29,7 +23,7 @@ It will output the log into stdout device(console log).
```go
logger, err := etlog.NewEtLogger(etlog.SetConfigPath("log.yaml"))
if err != nil {
panic(err)
panic(err)
}
logger.Debug("hello")
logger.Info("world")
Expand All @@ -41,9 +35,9 @@ the log config file please refer to: [example/log.yaml](./example/log.yaml)

```go
etlog.Log.WithError(fmt.Errorf("oops")).
WithField("key", "word").
WithField("now", time.Now()).
Error("something wrong happened")
WithField("key", "word").
WithField("now", time.Now()).
Error("something wrong happened")
```

the `WithField` method will help you print K-V fields into log.
Expand All @@ -54,5 +48,7 @@ the `WithField` method will help you print K-V fields into log.
etlog.Log.WithMarkers("trace").Data("hello world")
```

Because we support different log handler, to determine which handler the content will be output, we use `marker` to route it. Such as the example, when we use `trace` as marker of log, then the content will be processed by handler marked as `trace`.
Because we support different log handler, to determine which handler the content will be output, we use `marker` to
route it. Such as the example, when we use `trace` as marker of log, then the content will be processed by handler
marked as `trace`.

10 changes: 4 additions & 6 deletions common/utils/sourceutils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"fmt"
"runtime"
)

Expand All @@ -17,14 +16,13 @@ func SourceLoc(skip int) (file string, line int, funcName string, ok bool) {
return
}

func ShortSourceLoc(skip int) (line string, funcName string, ok bool) {
func ShortSourceLoc(skip int) (fileName string, line int, funcName string, ok bool) {
var _file string
var _line int
var _funcName string
if _file, _line, _funcName, ok = SourceLoc(skip); !ok {
return "", "", false
if _file, line, _funcName, ok = SourceLoc(skip); !ok {
return "", 0, "", false
}
line = LastSubstring(fmt.Sprintf("%s:%d", _file, _line), "/")
fileName = LastSubstring(_file, "/")
funcName = LastSubstring(_funcName, "/")
return
}
52 changes: 36 additions & 16 deletions common/utils/sourceutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ func TestShortSourceLoc(t *testing.T) {
}
type testCase struct {
args args
wantLine string
wantFileName string
wantLine int
wantFuncName string
wantOk bool
}
Expand All @@ -19,14 +20,20 @@ func TestShortSourceLoc(t *testing.T) {
skip: 0,
},
wantOk: true,
wantLine: "sourceutils.go:13",
wantFileName: "sourceutils.go",
wantLine: 12,
wantFuncName: "utils.SourceLoc",
}
gotLine, gotFuncName, gotOk := ShortSourceLoc(tc.args.skip)
t.Log(gotLine, gotFuncName, gotOk)
gotFileName, gotLine, gotFuncName, gotOk := ShortSourceLoc(tc.args.skip)
t.Log(gotFileName, gotLine, gotFuncName, gotOk)
if gotFileName != tc.wantFileName {
t.Errorf("ShortSourceLoc() gotFileName = %v, want %v", gotLine, tc.wantLine)
}

if gotLine != tc.wantLine {
t.Errorf("ShortSourceLoc() gotLine = %v, want %v", gotLine, tc.wantLine)
}

if gotFuncName != tc.wantFuncName {
t.Errorf("ShortSourceLoc() gotFuncName = %v, want %v", gotFuncName, tc.wantFuncName)
}
Expand All @@ -41,11 +48,16 @@ func TestShortSourceLoc(t *testing.T) {
skip: 1,
},
wantOk: true,
wantLine: "sourceutils.go:24",
wantFileName: "sourceutils.go",
wantLine: 22,
wantFuncName: "utils.ShortSourceLoc",
}
gotLine, gotFuncName, gotOk := ShortSourceLoc(tc.args.skip)
t.Log(gotLine, gotFuncName, gotOk)
gotFileName, gotLine, gotFuncName, gotOk := ShortSourceLoc(tc.args.skip)
t.Log(gotFileName, gotLine, gotFuncName, gotOk)
if gotFileName != tc.wantFileName {
t.Errorf("ShortSourceLoc() gotFileName = %v, want %v", gotLine, tc.wantLine)
}

if gotLine != tc.wantLine {
t.Errorf("ShortSourceLoc() gotLine = %v, want %v", gotLine, tc.wantLine)
}
Expand All @@ -63,11 +75,16 @@ func TestShortSourceLoc(t *testing.T) {
skip: 2,
},
wantOk: true,
wantLine: "sourceutils_test.go:69",
wantFileName: "sourceutils_test.go",
wantLine: 82,
wantFuncName: "utils.TestShortSourceLoc.func3",
}
gotLine, gotFuncName, gotOk := ShortSourceLoc(tc.args.skip)
t.Log(gotLine, gotFuncName, gotOk)
gotFileName, gotLine, gotFuncName, gotOk := ShortSourceLoc(tc.args.skip)
t.Log(gotFileName, gotLine, gotFuncName, gotOk)
if gotFileName != tc.wantFileName {
t.Errorf("ShortSourceLoc() gotFileName = %v, want %v", gotLine, tc.wantLine)
}

if gotLine != tc.wantLine {
t.Errorf("ShortSourceLoc() gotLine = %v, want %v", gotLine, tc.wantLine)
}
Expand All @@ -85,13 +102,16 @@ func TestShortSourceLoc(t *testing.T) {
skip: 2,
},
wantOk: true,
wantLine: "sourceutils_test.go:92",
wantFuncName: "utils.TestShortSourceLoc.func4.1",
wantFileName: "sourceutils_test.go",
wantLine: 109,
wantFuncName: "utils.TestShortSourceLoc.func4",
}
gotLine, gotFuncName, gotOk := func() (string, string, bool) {
return ShortSourceLoc(tc.args.skip)
}()
t.Log(gotLine, gotFuncName, gotOk)
gotFileName, gotLine, gotFuncName, gotOk := ShortSourceLoc(tc.args.skip)
t.Log(gotFileName, gotLine, gotFuncName, gotOk)
if gotFileName != tc.wantFileName {
t.Errorf("ShortSourceLoc() gotFileName = %v, want %v", gotLine, tc.wantLine)
}

if gotLine != tc.wantLine {
t.Errorf("ShortSourceLoc() gotLine = %v, want %v", gotLine, tc.wantLine)
}
Expand Down
4 changes: 3 additions & 1 deletion core/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func (ff *FullFormatter) Format(entry *LogEntry) *bufferpool.Buffer {

// line & func
if entry.UseLoc {
buf.AppendString(entry.Line)
buf.AppendString(entry.FileName)
buf.AppendByte(':')
buf.AppendInt(int64(entry.Line))
buf.AppendByte('|')
buf.AppendString(entry.FuncName)
} else {
Expand Down
6 changes: 4 additions & 2 deletions core/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func TestFullFormatter_Format(t *testing.T) {
Level: INFO,
Msg: "hello world",
UseLoc: true,
Line: "hello.go:123",
FileName: "hello.go",
Line: 123,
FuncName: "TestFormatter.func1",
},
},
Expand All @@ -71,7 +72,8 @@ func TestFullFormatter_Format(t *testing.T) {
Level: INFO,
Msg: "hello world",
UseLoc: true,
Line: "hello.go:123",
FileName: "hello.go",
Line: 123,
FuncName: "TestFormatter.func1",
Err: errors.New("oops"),
Fields: map[string]interface{}{"Hello": "world", "abc": 123},
Expand Down
4 changes: 3 additions & 1 deletion core/logentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type Fields map[string]interface{}
type LogEntry struct {
Time time.Time `json:"time"`
Level Level `json:"level"`
Line string `json:"line"`
FileName string `json:"file"`
Line int `json:"line"`
FuncName string `json:"func"`
Msg string `json:"msg"`
Marker string `json:"marker"`
Expand All @@ -30,6 +31,7 @@ func (le *LogEntry) Copy() *LogEntry {
return &LogEntry{
Time: le.Time,
Level: le.Level,
FileName: le.FileName,
Line: le.Line,
FuncName: le.FuncName,
Msg: le.Msg,
Expand Down
11 changes: 10 additions & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {

RunAll()
RunRotate()
time.Sleep(10 * time.Second)
log.Println("done")

}
Expand Down Expand Up @@ -75,6 +76,15 @@ func RunAll() {
WithField("key", "word").
WithField("now", time.Now()).
Error("something wrong happened")
etlog.Log.WithError(fmt.Errorf("oops")).
WithField("key", "word").
WithField("now", time.Now()).
Error("something wrong happened")
etlog.Log.WithFields(core.Fields{
"abc": 123,
"xyz": "hello world",
"now": time.Now(),
}).Info("test fields")

for i := 1; i < 10; i++ {
etlog.Log.WithError(fmt.Errorf("oops")).
Expand Down Expand Up @@ -111,6 +121,5 @@ func RunRotate() {
}
wg.Wait()
etlog.Log.WithField("endTime", time.Now()).Info("complete test")
time.Sleep(10 * time.Second)

}
Loading

0 comments on commit 195669b

Please sign in to comment.