You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Logkit/gokit.go

234 lines
4.2 KiB

6 years ago
package logkit
import (
"fmt"
"io"
5 years ago
"path"
"runtime"
6 years ago
"time"
)
var (
inited bool
logWriter Writer
logLevel = LevelInfo
6 years ago
logLevelName string
logName string
logPath string
channel Channel
alsoStdout bool
5 years ago
withCaller Caller
6 years ago
levelToNames = map[Level]string{
LevelFatal: "FATAL",
LevelError: "ERROR",
LevelWarn: "WARN",
LevelInfo: "INFO",
LevelDebug: "DEBUG",
LevelTrace: "TRACE",
}
)
//Level 日志等级
5 years ago
type Level int
6 years ago
const (
Default Level = iota
LevelTrace
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
)
6 years ago
6 years ago
type Channel byte
5 years ago
type Caller byte
6 years ago
6 years ago
const (
FIlE Channel = iota
SYSLOG
KAFKA
)
6 years ago
5 years ago
const (
_ Caller = iota
NONE
5 years ago
FullPATHFunc
BasePathFunc
BasePath
5 years ago
)
6 years ago
type Writer interface {
//Write 写日志
Write(Level, string)
//Exit 日志退出
Exit()
}
func Exit() {
logWriter.Exit()
}
5 years ago
func Init(_channel Channel, name string, level Level, _alsoStdout bool, _withCaller Caller) error {
6 years ago
if inited {
return fmt.Errorf("logkit has been inited")
}
if name != "" {
logName = name
} else {
return fmt.Errorf("log name must not be empty")
6 years ago
}
6 years ago
logLevel = level
channel = _channel
alsoStdout = _alsoStdout
withCaller = _withCaller
6 years ago
return nil
}
6 years ago
func SetPath(path string) {
logPath = path
}
6 years ago
func getLevelName(level Level) string {
levelName, _ := levelToNames[level]
return levelName
}
func format(level Level, msg string) string {
5 years ago
if withCaller != NONE {
var (
context string
pc uintptr
file string
line int
)
pc, file, line, _ = runtime.Caller(3)
5 years ago
switch withCaller {
case FullPATHFunc:
5 years ago
context = fmt.Sprintf("%s:%03d::%s", file, line, path.Base(runtime.FuncForPC(pc).Name()))
5 years ago
case BasePathFunc:
5 years ago
context = fmt.Sprintf("%s:%03d::%s", path.Base(file), line, path.Base(runtime.FuncForPC(pc).Name()))
5 years ago
case BasePath:
context = fmt.Sprintf("%s:%03d", path.Base(file), line)
default:
context = fmt.Sprintf("%s:%03d", path.Base(file), line)
5 years ago
}
5 years ago
return fmt.Sprintf("%s\t[%3s]\t%s\t%s\n", time.Now().Format("2006-01-02 15:04:05.999"), getLevelName(level), context, msg)
} else {
return fmt.Sprintf("%s\t[%3s]\t%s\n", time.Now().Format("2006-01-02 15:04:05.999"), getLevelName(level), msg)
}
6 years ago
}
func write(level Level, msg string) {
if !inited {
if logWriter == nil && channel == FIlE {
if logPath == "" {
logPath = "/data/logs/" + logName + ".log"
}
6 years ago
logWriter = NewFileLogger(logPath, logName, time.Second*5, 1204*1024*1800, 256*1024)
}
if logWriter == nil && channel == SYSLOG {
6 years ago
logWriter, _ = NewSyslogWriter("", "", level, logName)
}
inited = true
6 years ago
}
messageStr := format(level, msg)
logWriter.Write(level, messageStr)
6 years ago
if alsoStdout {
5 years ago
fmt.Print(messageStr)
6 years ago
}
}
func level() Level {
return logLevel
}
func Debug(str string) {
if level() <= LevelDebug {
write(LevelDebug, str)
}
}
func Debugs(args ...interface{}) {
if level() <= LevelDebug {
write(LevelDebug, fmt.Sprintln(args...))
}
}
func Debugf(format string, args ...interface{}) {
if level() <= LevelDebug {
write(LevelDebug, fmt.Sprintf(format, args...))
}
}
func Info(str string) {
if level() <= LevelInfo {
write(LevelInfo, str)
}
}
func Infos(args ...interface{}) {
if level() <= LevelInfo {
write(LevelInfo, fmt.Sprintln(args...))
}
}
func Infof(format string, args ...interface{}) {
if level() <= LevelInfo {
write(LevelInfo, fmt.Sprintf(format, args...))
}
}
func Warn(str string) {
if level() <= LevelWarn {
write(LevelWarn, str)
}
}
func Warns(args ...interface{}) {
if level() <= LevelWarn {
write(LevelWarn, fmt.Sprintln(args...))
}
}
func Warnf(format string, args ...interface{}) {
if level() <= LevelWarn {
write(LevelWarn, fmt.Sprintf(format, args...))
}
}
func Error(str string) {
if level() <= LevelError {
write(LevelError, str)
}
}
func Errors(args ...interface{}) {
if level() <= LevelError {
write(LevelError, fmt.Sprintln(args...))
}
}
func Errorf(format string, args ...interface{}) {
if level() <= LevelError {
write(LevelError, fmt.Sprintf(format, args...))
}
}
func NewLogWriter(level Level) io.Writer {
return &stdWriter{level}
}
type stdWriter struct {
level Level
}
func (this *stdWriter) Write(data []byte) (int, error) {
write(this.level, string(data))
return len(data), nil
}