[TASK] add log entry

This commit is contained in:
Martin Geno 2017-06-11 01:33:35 +02:00
commit a096771e20
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
3 changed files with 194 additions and 0 deletions

116
entry/level.go Normal file
View File

@ -0,0 +1,116 @@
package entry
type logLevel int32
const (
DebugLevel = -1
InfoLevel = 0
WarnLevel = 1
ErrorLevel = 2
PanicLevel = 3
)
func (l logLevel) String() string {
switch l {
case DebugLevel:
return "Debug"
case InfoLevel:
return "Info"
case WarnLevel:
return "Warn"
case ErrorLevel:
return "ERROR"
case PanicLevel:
return "PANIC"
}
return "NOT VALID"
}
/**
* log command
*/
// debug
func (e *Entry) Debug(v ...interface{}) {
e.Log(DebugLevel, v...)
}
func (e *Entry) Debugf(format string, v ...interface{}) {
e.Logf(DebugLevel, format, v...)
}
// info
func (e *Entry) Info(v ...interface{}) {
e.Log(InfoLevel, v...)
}
func (e *Entry) Infof(format string, v ...interface{}) {
e.Logf(InfoLevel, format, v...)
}
// warn
func (e *Entry) Warn(v ...interface{}) {
e.Log(WarnLevel, v...)
}
func (e *Entry) Warnf(format string, v ...interface{}) {
e.Logf(WarnLevel, format, v...)
}
// error
func (e *Entry) Error(v ...interface{}) {
e.Log(ErrorLevel, v...)
}
func (e *Entry) Errorf(format string, v ...interface{}) {
e.Logf(ErrorLevel, format, v...)
}
// panic
func (e *Entry) Panic(v ...interface{}) {
e.Log(PanicLevel, v...)
}
func (e *Entry) Panicf(format string, v ...interface{}) {
e.Logf(PanicLevel, format, v...)
}
/**
* Direct log command
*/
// debug
func Debug(v ...interface{}) {
New().Log(DebugLevel, v...)
}
func Debugf(format string, v ...interface{}) {
New().Logf(DebugLevel, format, v...)
}
// info
func Info(v ...interface{}) {
New().Log(InfoLevel, v...)
}
func Infof(format string, v ...interface{}) {
New().Logf(InfoLevel, format, v...)
}
// warn
func Warn(v ...interface{}) {
New().Log(WarnLevel, v...)
}
func Warnf(format string, v ...interface{}) {
New().Logf(WarnLevel, format, v...)
}
// error
func Error(v ...interface{}) {
New().Log(ErrorLevel, v...)
}
func Errorf(format string, v ...interface{}) {
New().Logf(ErrorLevel, format, v...)
}
// panic
func Panic(v ...interface{}) {
New().Log(PanicLevel, v...)
}
func Panicf(format string, v ...interface{}) {
New().Logf(PanicLevel, format, v...)
}

39
entry/main.go Normal file
View File

@ -0,0 +1,39 @@
package entry
import "fmt"
type Entry struct {
Level logLevel `json:"level"`
Fields map[string]interface{} `json:"fields"`
Text string `json:"text"`
}
func (e *Entry) Log(level logLevel, v ...interface{}) {
e.Text = fmt.Sprint(v...)
e.Level = level
save(e)
}
func (e *Entry) Logf(level logLevel, format string, v ...interface{}) {
e.Text = fmt.Sprintf(format, v...)
e.Level = level
save(e)
}
func New() *Entry {
return &Entry{Fields: make(map[string]interface{})}
}
func (e *Entry) AddField(key string, value interface{}) *Entry {
e.Fields[key] = value
return e
}
func (e *Entry) AddFields(fields map[string]interface{}) *Entry {
for key, value := range fields {
e.Fields[key] = value
}
return e
}
func (e *Entry) FieldString() string {
return FieldOutput(e.Fields)
}

39
entry/output.go Normal file
View File

@ -0,0 +1,39 @@
package entry
import (
"fmt"
"os"
"time"
)
var TimeFormat = "2006-01-02 15:04:05"
var InternelSend = func(e *Entry) {
format := "%s [%s] %s\n"
v := []interface{}{time.Now().Format(TimeFormat), e.Level.String(), e.Text}
if len(e.Fields) > 0 {
format = "%s [%s] %s (%s)\n"
v = append(v, e.FieldString())
}
text := fmt.Sprintf(format, v...)
if e.Level == PanicLevel {
panic(text)
} else if e.Level > WarnLevel {
os.Stderr.WriteString(text)
} else {
os.Stdout.WriteString(text)
}
}
var FieldOutput = func(fields map[string]interface{}) string {
text := ""
for key, value := range fields {
text = fmt.Sprintf("%s %s=%v", text, key, value)
}
return text[1:]
}
func save(e *Entry) {
InternelSend(e)
}