|
|
@ -3,6 +3,7 @@ package logkit |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
|
|
|
|
"path" |
|
|
|
"runtime" |
|
|
|
"runtime" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
) |
|
|
|
) |
|
|
@ -16,7 +17,7 @@ var ( |
|
|
|
logPath string |
|
|
|
logPath string |
|
|
|
channel Channel |
|
|
|
channel Channel |
|
|
|
alsoStdout bool |
|
|
|
alsoStdout bool |
|
|
|
withCaller bool |
|
|
|
withCaller Caller |
|
|
|
|
|
|
|
|
|
|
|
levelToNames = map[Level]string{ |
|
|
|
levelToNames = map[Level]string{ |
|
|
|
LevelFatal: "FATAL", |
|
|
|
LevelFatal: "FATAL", |
|
|
@ -42,6 +43,7 @@ const ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type Channel byte |
|
|
|
type Channel byte |
|
|
|
|
|
|
|
type Caller byte |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
|
FIlE Channel = iota |
|
|
|
FIlE Channel = iota |
|
|
@ -49,6 +51,13 @@ const ( |
|
|
|
KAFKA |
|
|
|
KAFKA |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
|
|
|
_ Caller = iota |
|
|
|
|
|
|
|
NONE |
|
|
|
|
|
|
|
FullPATH |
|
|
|
|
|
|
|
File |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type Writer interface { |
|
|
|
type Writer interface { |
|
|
|
//Write 写日志
|
|
|
|
//Write 写日志
|
|
|
|
Write(Level, string) |
|
|
|
Write(Level, string) |
|
|
@ -61,7 +70,7 @@ func Exit() { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Init(_channel Channel, name string, level Level, _alsoStdout bool, _withCaller bool) error { |
|
|
|
func Init(_channel Channel, name string, level Level, _alsoStdout bool, _withCaller Caller) error { |
|
|
|
if inited { |
|
|
|
if inited { |
|
|
|
return fmt.Errorf("logkit has been inited") |
|
|
|
return fmt.Errorf("logkit has been inited") |
|
|
|
} |
|
|
|
} |
|
|
@ -88,9 +97,20 @@ func getLevelName(level Level) string { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func format(level Level, msg string) string { |
|
|
|
func format(level Level, msg string) string { |
|
|
|
if withCaller { |
|
|
|
if withCaller != NONE { |
|
|
|
_, file, line, _ := runtime.Caller(3) |
|
|
|
var ( |
|
|
|
return fmt.Sprintf("%s [%s] %s:%d %s \n", time.Now().Format("2006-01-02 15:04:05.999"), getLevelName(level), file, line, msg) |
|
|
|
context string |
|
|
|
|
|
|
|
pc uintptr |
|
|
|
|
|
|
|
file string |
|
|
|
|
|
|
|
line int |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
pc, file, line, _ = runtime.Caller(3) |
|
|
|
|
|
|
|
if withCaller == FullPATH { |
|
|
|
|
|
|
|
context = fmt.Sprintf("%s:%d(%s)", file, line, runtime.FuncForPC(pc).Name()) |
|
|
|
|
|
|
|
}else{ |
|
|
|
|
|
|
|
context = fmt.Sprintf("%s:%d(%s)", path.Base(file), line, runtime.FuncForPC(pc).Name()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return fmt.Sprintf("%s [%s] %s %s \n", time.Now().Format("2006-01-02 15:04:05.999"), getLevelName(level), context, msg) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return fmt.Sprintf("%s [%s] %s \n", time.Now().Format("2006-01-02 15:04:05.999"), getLevelName(level), msg) |
|
|
|
return fmt.Sprintf("%s [%s] %s \n", time.Now().Format("2006-01-02 15:04:05.999"), getLevelName(level), msg) |
|
|
|
} |
|
|
|
} |
|
|
|