Skip to content

Commit

Permalink
add shortcut call
Browse files Browse the repository at this point in the history
  • Loading branch information
zhcpku committed Jun 24, 2019
1 parent 577779c commit d13006f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
20 changes: 10 additions & 10 deletions backend/stderr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"sort"
"strings"

"github.com/go-coder/log"
"github.com/go-coder/log/types"
)

func Stderr() log.EntryWriter {
func Stderr() types.EntryWriter {
out := &outter{
entryChan: make(chan *log.Entry),
entryChan: make(chan *types.Entry),
}
go func() {
for e := range out.entryChan {
Expand All @@ -22,22 +22,22 @@ func Stderr() log.EntryWriter {
}

type outter struct {
entryChan chan *log.Entry
entryChan chan *types.Entry
}

var _ log.EntryWriter = (*outter)(nil)
var _ types.EntryWriter = (*outter)(nil)

func (o *outter) WriteEntry(e *log.Entry) {
func (o *outter) WriteEntry(e *types.Entry) {
o.entryChan <- e
}

func doWrite(e *log.Entry) {
func doWrite(e *types.Entry) {
var str string
if e.Level < 0 {
if e.Error != nil {
if e.Err != nil {
str = fmt.Sprintf("Er %s %s:%d %s [%s] %s\n %s %s\n",
e.Time.Format("2006/1/2 15:04:05"), shorten(e.FileName), e.LineNum, e.Prefix, e.Message, flatten(e.Fields),
e.Error.Message, e.Error.StackTrace)
e.Err.Message, e.Err.StackTrace)
} else {
str = fmt.Sprintf("Er %s %s:%d %s [%s] %s\n",
e.Time.Format("2006/1/2 15:04:05"), shorten(e.FileName), e.LineNum, e.Prefix, e.Message, flatten(e.Fields))
Expand All @@ -58,7 +58,7 @@ func shorten(fileName string) string {
}

// flatten returns string of sortted key-value pair
func flatten(dict map[string]*log.TypedValue) string {
func flatten(dict map[string]*types.TypedValue) string {
keys := make([]string, 0, len(dict))
for k := range dict {
keys = append(keys, k)
Expand Down
4 changes: 3 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

func main() {
logr := log.New(backend.Stderr())
log.Info("yes I can call it directly")

logr := log.NewLogger(backend.Stderr()).WithName("test").WithFields("key", "value")

logr.V(1).Info("msg", "uint", 112, "int", 211, "nil", nil)
var typedNil *int
Expand Down
39 changes: 20 additions & 19 deletions log.go → frontend/logr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package log
package frontend

import (
"fmt"
Expand All @@ -9,6 +9,7 @@ import (
"time"

"github.com/go-coder/logr"
"github.com/go-coder/log/types"
"github.com/spf13/pflag"
)

Expand All @@ -27,7 +28,7 @@ func init() {
pflag.Parse()
}

func New(outter EntryWriter) logr.Logger {
func New(outter types.EntryWriter) logr.Logger {
return &rlog{
level: 0,
name: "",
Expand All @@ -49,7 +50,7 @@ type rlog struct {
level int
name string
fields []interface{}
outter EntryWriter
outter types.EntryWriter
}

var _ logr.InfoLogger = (*rlog)(nil)
Expand Down Expand Up @@ -94,15 +95,15 @@ func (l *rlog) Error(err error, msg string, kvList ...interface{}) {
entry := l.getEntry(msg, kvList)
entry.Level = -1
if err != nil {
entry.Error = &Error{Message: err.Error()}
entry.Err = &types.ErrorRecord{Message: err.Error()}
if argShowErrorTrace {
entry.Error.StackTrace = string(debug.Stack())
entry.Err.StackTrace = string(debug.Stack())
}
}
l.outter.WriteEntry(entry)
}

func (l *rlog) getEntry(msg string, kvList []interface{}) *Entry {
func (l *rlog) getEntry(msg string, kvList []interface{}) *types.Entry {
if len(kvList)%2 != 0 {
panic("fields must be key-value pairs")
}
Expand All @@ -112,7 +113,7 @@ func (l *rlog) getEntry(msg string, kvList []interface{}) *Entry {
file = AutoGeneratedName
line = 0
}
return &Entry{
return &types.Entry{
Level: l.level,
Time: time.Now(),
FileName: file,
Expand All @@ -125,8 +126,8 @@ func (l *rlog) getEntry(msg string, kvList []interface{}) *Entry {
}

// kvList must be key-value pair
func kvListToMap(kvList []interface{}) map[string]*TypedValue {
dict := make(map[string]*TypedValue)
func kvListToMap(kvList []interface{}) map[string]*types.TypedValue {
dict := make(map[string]*types.TypedValue)
for i := 0; i < len(kvList); i += 2 {
key := kvList[i].(string)
value := getTypedValue(kvList[i+1])
Expand All @@ -135,29 +136,29 @@ func kvListToMap(kvList []interface{}) map[string]*TypedValue {
return dict
}

func getTypedValue(v interface{}) *TypedValue {
func getTypedValue(v interface{}) *types.TypedValue {
if v == nil {
return &TypedValue{
Type: TypeNil,
Value: ValueNil,
return &types.TypedValue{
Type: types.TypeNil,
Value: types.ValueNil,
}
}
if reflect.ValueOf(v).Kind() == reflect.Ptr {
if reflect.ValueOf(v).IsNil() {
return &TypedValue{
return &types.TypedValue{
Type: reflect.TypeOf(v).String(),
Value: ValueNil,
Value: types.ValueNil,
}
}
v = reflect.Indirect(reflect.ValueOf(v)).Interface()
}
if reflect.ValueOf(v).Kind() == reflect.Chan {
return &TypedValue{
Type: TypeChan,
Value: ValueChan,
return &types.TypedValue{
Type: types.TypeChan,
Value: types.ValueChan,
}
}
return &TypedValue{
return &types.TypedValue{
Type: fmt.Sprintf("%T", v),
Value: fmt.Sprintf("%v", v),
}
Expand Down
19 changes: 19 additions & 0 deletions logr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package log

import (
"github.com/go-coder/log/backend"
"github.com/go-coder/log/frontend"
"github.com/go-coder/logr"
)

var (
NewLogger = frontend.New

syslog logr.Logger = NewLogger(backend.Stderr())

V = syslog.V
WithName = syslog.WithName
WithFields = syslog.WithFields
Info = syslog.Info
Error = syslog.Error
)
6 changes: 3 additions & 3 deletions types.go → types/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package log
package types

import (
"time"
Expand All @@ -24,15 +24,15 @@ type Entry struct {
Prefix string
Message string
Fields map[string]*TypedValue
Error *Error
Err *ErrorRecord
}

type TypedValue struct {
Type string
Value string
}

type Error struct {
type ErrorRecord struct {
Message string
StackTrace string
}

0 comments on commit d13006f

Please sign in to comment.