修改日志功能

This commit is contained in:
haidy 2018-12-26 22:48:10 +08:00
parent 83b9e076f9
commit 531d708f7b
3 changed files with 57 additions and 0 deletions

19
cmd/frp/log.go Normal file
View File

@ -0,0 +1,19 @@
package frp
import "github.com/fatedier/frp/utils/log"
type FrpLogListener interface {
Log(log string)
Location() string
}
// type FrpLogListener struct {
// name string
// }
// func (l *FrpLogListener) Log(log string) {
// }
func SetFrpLogListener(l FrpLogListener) {
log.AppendListener(l)
}

View File

@ -67,22 +67,27 @@ func SetLogLevel(logLevel string) {
func Error(format string, v ...interface{}) {
Log.Error(format, v...)
CallLogListeners("[Error] " + fmt.Sprintf(format, v...))
}
func Warn(format string, v ...interface{}) {
Log.Warn(format, v...)
CallLogListeners("[Warn] " + fmt.Sprintf(format, v...))
}
func Info(format string, v ...interface{}) {
Log.Info(format, v...)
CallLogListeners("[Info] " + fmt.Sprintf(format, v...))
}
func Debug(format string, v ...interface{}) {
Log.Debug(format, v...)
CallLogListeners("[Debug] " + fmt.Sprintf(format, v...))
}
func Trace(format string, v ...interface{}) {
Log.Trace(format, v...)
CallLogListeners("[Trace] " + fmt.Sprintf(format, v...))
}
// Logger

33
utils/log/log_extend.go Normal file
View File

@ -0,0 +1,33 @@
package log
import (
"sync"
"time"
)
var logListeners = make([]LogListener, 0)
var lock sync.Mutex
type LogListener interface {
Log(log string)
Location() string
}
func AppendListener(l LogListener) {
lock.Lock()
logListeners = append(logListeners, l)
lock.Unlock()
}
func CallLogListeners(log string) {
lock.Lock()
for _, l := range logListeners {
location, _ := time.LoadLocation(l.Location())
if location == nil {
location = time.UTC
}
l.Log(time.Now().In(location).Format("2006/01/02 15:04:05") + ": " + log)
}
lock.Unlock()
}