123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package log
- import (
- "fmt"
- "github.com/davecgh/go-spew/spew"
- "os"
- "path/filepath"
- "runtime"
- "time"
- )
- var level = 0
- const (
-
- TRACE = 0
-
- DEBUG = 1
-
- INFO = 2
-
- WARNING = 3
-
- ERROR = 4
-
- FATAL = 5
- )
- type Stringer interface {
- LogString() string
- }
- func Init(plevel int) {
- level = plevel
- }
- func Dump(a ...interface{}) string {
- return spew.Sdump(a...)
- }
- func dump(a ...interface{}) []interface{} {
- for ai, av := range a {
- if avf, ok := av.(func() string); ok {
- a[ai] = avf()
- } else if avf, ok := av.(Stringer); ok {
- a[ai] = avf.LogString()
- } else if avf, ok := av.(fmt.GoStringer); ok {
- a[ai] = avf.GoString()
- } else if avf, ok := av.(fmt.Stringer); ok {
- a[ai] = avf.String()
- }
- }
- return a
- }
- func Trace(a ...interface{}) {
- if level <= TRACE {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
- }
- }
- func Tracef(format string, a ...interface{}) {
- if level <= TRACE {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
- }
- }
- func Debug(a ...interface{}) {
- if level <= DEBUG {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
- }
- }
- func Debugf(format string, a ...interface{}) {
- if level <= DEBUG {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
- }
- }
- func Info(a ...interface{}) {
- if level <= INFO {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
- }
- }
- func Infof(format string, a ...interface{}) {
- if level <= INFO {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
- }
- }
- func Warning(a ...interface{}) {
- if level <= WARNING {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
- }
- }
- func Warningf(format string, a ...interface{}) {
- if level <= WARNING {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
- }
- }
- func Error(a ...interface{}) {
- if level <= ERROR {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
- }
- }
- func Errorf(format string, a ...interface{}) {
- if level <= ERROR {
- _, path, line, _ := runtime.Caller(1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
- }
- }
|