123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- package log
- import (
- "fmt"
- "os"
- "path/filepath"
- "runtime"
- "strings"
- "time"
- )
- var level = 0
- const (
- // TRACE level
- TRACE = 0
- // DEBUG level
- DEBUG = 1
- // INFO level
- INFO = 2
- // WARNING level
- WARNING = 3
- // ERROR level
- ERROR = 4
- // FATAL level
- FATAL = 5
- )
- // Init func
- func Init(l int) {
- level = l
- }
- // IsDebugEnable func
- func IsDebugEnable() bool {
- return level <= DEBUG
- }
- // Trace log
- func Trace(a ...interface{}) {
- if level <= TRACE {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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(a...))
- }
- }
- // Tracef log
- func Tracef(format string, a ...interface{}) {
- if level <= TRACE {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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, a...))
- }
- }
- // Debug log
- func Debug(a ...interface{}) {
- if level <= DEBUG {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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(a...))
- }
- }
- // Debugf log
- func Debugf(format string, a ...interface{}) {
- if level <= DEBUG {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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, a...))
- }
- }
- // Info log
- func Info(a ...interface{}) {
- if level <= INFO {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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(a...))
- }
- }
- // Infof log
- func Infof(format string, a ...interface{}) {
- if level <= INFO {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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, a...))
- }
- }
- // Warning log
- func Warning(a ...interface{}) {
- if level <= WARNING {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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(a...))
- }
- }
- // Warningf log
- func Warningf(format string, a ...interface{}) {
- if level <= WARNING {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 1)
- _, file := filepath.Split(path)
- fmt.Fprintf(os.Stderr, "WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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, a...))
- }
- }
- // Error log
- func Error(a ...interface{}) {
- if level <= ERROR {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 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(a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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(a...))
- }
- }
- // Errorf log
- func Errorf(format string, a ...interface{}) {
- if level <= ERROR {
- for i := 1; i > 0 && i < 100; i++ {
- _, path, _, _ := runtime.Caller(i)
- if path != "" {
- if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
- _, path, line, _ := runtime.Caller(i + 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, a...))
- return
- }
- } else {
- i = -999
- }
- }
- _, 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, a...))
- }
- }
|