From 5be0c42ac0728ae0007cf4492c562ec290c924c9 Mon Sep 17 00:00:00 2001 From: boyce Date: Sat, 23 Mar 2019 15:49:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=97=A5=E5=BF=97=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- originnode/node.go | 6 +++--- sysmodule/LogModule.go | 29 +++++++++++++++++++++++++---- sysservice/logservice.go | 4 ++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/originnode/node.go b/originnode/node.go index e192906..9734b6f 100644 --- a/originnode/node.go +++ b/originnode/node.go @@ -135,13 +135,13 @@ func NewOrginNode() *COriginNode { return node } -func (s *COriginNode) SetLogLevel(level uint) { +func (s *COriginNode) GetSysLog() *sysservice.LogService { logService := service.InstanceServiceMgr().FindService("syslog") if logService == nil { fmt.Printf("Cannot find syslog service!") os.Exit(-1) - return + return nil } - logService.(*sysservice.LogService).SetLogLevel(level) + return logService.(*sysservice.LogService) } diff --git a/sysmodule/LogModule.go b/sysmodule/LogModule.go index bba2dfd..fc74c3e 100644 --- a/sysmodule/LogModule.go +++ b/sysmodule/LogModule.go @@ -29,6 +29,8 @@ type ILogger interface { SetLogLevel(level uint) } +type FunListenLog func(uint, string) + type LogModule struct { service.BaseModule currentDay int @@ -38,6 +40,7 @@ type LogModule struct { openLevel uint locker sync.Mutex calldepth int + listenFun FunListenLog } func (slf *LogModule) GetCurrentFileName() string { @@ -82,6 +85,10 @@ func (slf *LogModule) Init(logfilename string, openLevel uint) { slf.calldepth = 3 } +func (slf *LogModule) SetListenLogFunc(listenFun FunListenLog) { + slf.listenFun = listenFun +} + func (slf *LogModule) GetLoggerByLevel(level uint) *log.Logger { if level >= LEVEL_MAX { level = 0 @@ -94,8 +101,15 @@ func (slf *LogModule) Printf(level uint, format string, v ...interface{}) { return } - if slf.openLevel == LEVER_DEBUG { - fmt.Println(LogPrefix[level], fmt.Sprintf(format, v...)) + if slf.openLevel == LEVER_DEBUG || slf.listenFun != nil { + strlog := fmt.Sprintf(format, v...) + if slf.openLevel == LEVER_DEBUG { + fmt.Println(LogPrefix[level], strlog) + } + + if slf.listenFun != nil { + slf.listenFun(level, fmt.Sprintf(format, v...)) + } } slf.CheckAndGenFile() @@ -107,8 +121,15 @@ func (slf *LogModule) Print(level uint, v ...interface{}) { return } - if slf.openLevel == LEVER_DEBUG { - fmt.Println(LogPrefix[level], fmt.Sprint(v...)) + if slf.openLevel == LEVER_DEBUG || slf.listenFun != nil { + strlog := fmt.Sprint(v...) + if slf.openLevel == LEVER_DEBUG { + fmt.Println(LogPrefix[level], strlog) + } + + if slf.listenFun != nil { + slf.listenFun(level, fmt.Sprint(v...)) + } } slf.CheckAndGenFile() diff --git a/sysservice/logservice.go b/sysservice/logservice.go index 63fbc14..043a1a3 100644 --- a/sysservice/logservice.go +++ b/sysservice/logservice.go @@ -31,3 +31,7 @@ func (slf *LogService) AppendCallDepth(calldepth int) { func (slf *LogService) SetLogLevel(level uint) { slf.logmodule.SetLogLevel(level) } + +func (slf *LogService) SetListenLogFunc(listenFun sysmodule.FunListenLog) { + slf.logmodule.SetListenLogFunc(listenFun) +}