1 Star 1 Fork 0

haming123/log4go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log4go.go 4.92 KB
一键复制 编辑 原始数据 按行查看 历史
haming 提交于 2021-11-30 16:29 . add read me
package log4go
import (
"fmt"
"runtime"
"strings"
"sync"
"time"
)
const tm_flush_loop = 10
type LogWriter interface {
Write(tm time.Time, data []byte)
Flush() error
Close() error
}
type Logger struct {
mu sync.Mutex
level Level
caller bool
out LogWriter
buf *LogBuffer
close_chan chan bool
calldepth int
pkg_hide_caller string
}
func NewTermLogger(log_level Level) *Logger {
var log Logger
log.out = NewTermWriter()
log.level = log_level
log.caller = true
log.calldepth = 2
log.buf = NewLogBuffer()
return &log
}
func NewFileLogger(log_path string, log_level Level) *Logger {
file_path := get_file_path(log_path)
var log Logger
log.out = NewFileWriter(file_path, ROTATE_DAY)
log.buf = NewLogBuffer()
log.level = log_level
log.caller = true
log.calldepth = 2
log.close_chan = make(chan bool)
go log.flush_loop()
return &log
}
func NewFileLoggerHour(log_path string, log_level Level) *Logger {
file_path := get_file_path(log_path)
var log Logger
log.out = NewFileWriter(file_path, ROTATE_HOUR)
log.buf = NewLogBuffer()
log.level = log_level
log.caller = true
log.calldepth = 2
log.close_chan = make(chan bool)
go log.flush_loop()
return &log
}
func (l *Logger) SetLevel(lvl Level) {
l.level = lvl
}
func (l *Logger) ShowCaller(show bool) {
l.caller = show
}
func (l *Logger) SetCallDepth(call_depth int) {
call_depth += 2
l.calldepth = call_depth
}
func (l *Logger) HideCaller4Package(package_name string) {
l.pkg_hide_caller = package_name
}
func (l *Logger) Flush() error {
l.mu.Lock()
defer l.mu.Unlock()
l.out.Flush()
return nil
}
func (l *Logger) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
l.out.Close()
if l.close_chan != nil {
close(l.close_chan)
l.close_chan = nil
}
//fmt.Println("logger Close")
return nil
}
func (lg *Logger) flush_loop() {
for {
select {
case <-time.After(tm_flush_loop * time.Second):
//fmt.Println("Flush")
lg.out.Flush()
case <-lg.close_chan:
//fmt.Println("stop flush loop")
return
}
}
}
func (lg *Logger)output(lvl Level, msg string) {
lg.mu.Lock()
defer lg.mu.Unlock()
now := time.Now()
level_str := lvl.String()
src_info := ""
if lg.caller {
pc, source, line, _ := runtime.Caller(lg.calldepth)
show_caller := true
if lg.pkg_hide_caller != ""{
pc_name := runtime.FuncForPC(pc).Name()
if strings.HasPrefix(pc_name, lg.pkg_hide_caller) {
show_caller = false
}
}
if show_caller {
source = get_short_name(source)
src_info = fmt.Sprintf("%s:%d", source, line)
}
}
lg.buf.Reset()
lg.buf.WriteTimeString(now)
lg.buf.WriteByte(' ')
lg.buf.WriteString(level_str)
if src_info != "" {
lg.buf.WriteByte(' ')
lg.buf.WriteString(src_info)
}
lg.buf.WriteByte(' ')
lg.buf.WriteString(msg)
if len(msg) == 0 || msg[len(msg)-1] != '\n' {
lg.buf.WriteByte('\n')
}
lg.out.Write(now, lg.buf.GetBytes())
}
func (lg *Logger) Fatal(v ...interface{}) {
lvl := LOG_FATAL
if lg.level >= lvl {
if len(v) == 1 {
str, ok := v[0].(string)
if ok {
lg.output(lvl, str)
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
}
}
func (lg *Logger) Fatalf(format string, v ...interface{}) {
lvl := LOG_FATAL
if lg.level >= lvl {
lg.output(lvl, fmt.Sprintf(format, v...))
}
}
func (lg *Logger) Error(v ...interface{}) {
lvl := LOG_ERROR
if lg.level >= lvl {
if len(v) == 1 {
str, ok := v[0].(string)
if ok {
lg.output(lvl, str)
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
}
}
func (lg *Logger) Errorf(format string, v ...interface{}) {
lvl := LOG_ERROR
if lg.level >= lvl {
lg.output(lvl, fmt.Sprintf(format, v...))
}
}
func (lg *Logger) Warn(v ...interface{}) {
lvl := LOG_WARN
if lg.level >= lvl {
if len(v) == 1 {
str, ok := v[0].(string)
if ok {
lg.output(lvl, str)
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
}
}
func (lg *Logger) Warnf(format string, v ...interface{}) {
lvl := LOG_WARN
if lg.level >= lvl {
lg.output(lvl, fmt.Sprintf(format, v...))
}
}
func (lg *Logger) Info(v ...interface{}) {
lvl := LOG_INFO
if lg.level >= lvl {
if len(v) == 1 {
str, ok := v[0].(string)
if ok {
lg.output(lvl, str)
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
}
}
func (lg *Logger) Infof(format string, v ...interface{}) {
lvl := LOG_INFO
if lg.level >= lvl {
lg.output(lvl, fmt.Sprintf(format, v...))
}
}
func (lg *Logger) Debug(v ...interface{}) {
lvl := LOG_DEBUG
if lg.level >= lvl {
if len(v) == 1 {
str, ok := v[0].(string)
if ok {
lg.output(lvl, str)
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
} else {
lg.output(lvl, fmt.Sprintln(v...))
}
}
}
func (lg *Logger) Debugf(format string, v ...interface{}) {
lvl := LOG_DEBUG
if lg.level >= lvl {
lg.output(lvl, fmt.Sprintf(format, v...))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/haming123/log4go.git
[email protected]:haming123/log4go.git
haming123
log4go
log4go
main

搜索帮助