代码拉取完成,页面将自动刷新
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...))
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。