From 8bc338992bc9d830761ae1aa7954dfebd552138b Mon Sep 17 00:00:00 2001 From: DEV Date: Sun, 28 Apr 2024 01:00:09 +0200 Subject: [PATCH] Corregido el packer y cambiado el nombre del interface de Builder a PackBuilder. --- alogs/active.go | 22 +++--- alogs/entry_logger.go | 39 ---------- alogs/loggable.go | 35 --------- alogs/logger.go | 17 ++--- alogs/logger_core.go | 4 +- alogs/logger_manager.go | 160 ++++++++++++++++++++++------------------ alogs/logger_scope.go | 20 +++-- app/main.go | 16 +++- 8 files changed, 136 insertions(+), 177 deletions(-) delete mode 100644 alogs/entry_logger.go delete mode 100644 alogs/loggable.go diff --git a/alogs/active.go b/alogs/active.go index 1e4c866..01f0be3 100644 --- a/alogs/active.go +++ b/alogs/active.go @@ -35,11 +35,11 @@ import ( const ( - errFieldCoreName string = "core-name" - errScopeCoreName string = "scope-name" + errFieldLogger string = "logger" + errFieldScope string = "scope" - entryFieldCoreName string = "logger-core" + entryFieldLogger string = "logger" LevelNameData string = "DATA" @@ -73,10 +73,10 @@ var ( ScopeSep byte ScopeNameTransforms astrings.Transformers - FmtMsgLogLogErr string - FmtMsgLogStartErr string - FmtMsgLogStopErr string - FmtMsgLogFlushErr string + FmtMsgLogErr string + FmtMsgStartErr string + FmtMsgStopErr string + FmtMsgFlushErr string }{ ErrInvalidLevel : errs.ErrCode("invalid-level"), @@ -91,10 +91,10 @@ var ( ScopeSep : '.', ScopeNameTransforms : astrings.MakeTransformers("lower"), - FmtMsgLogLogErr : "Error logging logger %s", - FmtMsgLogStartErr : "Error starting logger %s", - FmtMsgLogStopErr : "Error stopping logger %s", - FmtMsgLogFlushErr : "Error flushing logger %s", + FmtMsgLogErr : "Error logging logger %s", + FmtMsgStartErr : "Error starting logger %s", + FmtMsgStopErr : "Error stopping logger %s", + FmtMsgFlushErr : "Error flushing logger %s", } diff --git a/alogs/entry_logger.go b/alogs/entry_logger.go deleted file mode 100644 index ee3841e..0000000 --- a/alogs/entry_logger.go +++ /dev/null @@ -1,39 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name entry_logger.go -// Created by DEV -// Modified 24/04/2024 -// -// Copyright 2024 activething.com -// ------------------------------------------------------------------------ -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// ------------------------------------------------------------------------ - -package alogs - - -type ( - - EntryLogger interface { - Name () string - - Start () error - Stop () error - Flush () error - - Log (entry *Entry) error - } - -) diff --git a/alogs/loggable.go b/alogs/loggable.go deleted file mode 100644 index 5fd661f..0000000 --- a/alogs/loggable.go +++ /dev/null @@ -1,35 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name loggable.go -// Created by DEV -// Modified 24/04/2024 -// -// Copyright 2024 activething.com -// ------------------------------------------------------------------------ -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// ------------------------------------------------------------------------ - -package alogs - - -type ( - - Loggable interface { - - Log (ent *Entry) error - } -) - - diff --git a/alogs/logger.go b/alogs/logger.go index 3c31f66..dfd4d56 100644 --- a/alogs/logger.go +++ b/alogs/logger.go @@ -28,15 +28,14 @@ type ( Logger interface { - Name () string - Level() Level - - Fatal(message string, err error, options ...EntryFnc) - Error(message string, err error, options ...EntryFnc) - Warn (message string, options ...EntryFnc) - Info (message string, options ...EntryFnc) - Debug(message string, options ...EntryFnc) - Data (message string, data any, options ...EntryFnc) + Name () string + Level () Level + + Start () error + Stop () error + Flush () error + + Log (entry *Entry) error } ) diff --git a/alogs/logger_core.go b/alogs/logger_core.go index 22ef0c0..d6163dd 100644 --- a/alogs/logger_core.go +++ b/alogs/logger_core.go @@ -25,6 +25,7 @@ package alogs import ( "errs" + "fmt" "iosys" "sync" ) @@ -181,7 +182,6 @@ func (l *LoggerCore) Data (msg string, data any, ops ...EntryFnc){ func (l *LoggerCore) log(level Level, msg string, err error, options ...EntryFnc) { l.Lock() if l.state != StateStarted { - l.handleError(G.ErrLoggerNotStarted,nil) l.Unlock() return } @@ -190,6 +190,7 @@ func (l *LoggerCore) log(level Level, msg string, err error, options ...EntryFnc return } + en := l.extenders.ExtendEntry(AcquireEntry(l,level,msg,err,options...)) if er := l.appender.AppendEntry(en); er != nil { l.handleError(er,en) @@ -214,6 +215,7 @@ func (l *LoggerCore) Log(entry *Entry) error { l.Unlock() return nil } + fmt.Printf("--> Llalmando al logger %s con la entrada logger %v\n",l.name,entry.Excludes) er := l.appender.AppendEntry(entry) l.Unlock() return er diff --git a/alogs/logger_manager.go b/alogs/logger_manager.go index 51839c9..9d772ce 100644 --- a/alogs/logger_manager.go +++ b/alogs/logger_manager.go @@ -43,7 +43,10 @@ type ( queue queue.GQueue[*Entry] scopes loggerScopes - loggers []EntryLogger + loggers []Logger + + async bool + chanLog chan *Entry level Level name string @@ -84,6 +87,7 @@ func NewLoggerManager(name string, level Level, options ...LoggerManagerFnc) *Lo func (l *LoggerManager) Name() string { return l.name } + func (l *LoggerManager) Level() Level { l.RLock() lv := l.level @@ -91,6 +95,7 @@ func (l *LoggerManager) Level() Level { return lv } + func (l *LoggerManager) SetLevel (level Level) { l.Lock() l.level = level @@ -101,6 +106,7 @@ func (l *LoggerManager) SetLevel (level Level) { func (l *LoggerManager) Count () int64 { return atomic.LoadInt64(&l.count) } + func (l *LoggerManager) incCount () { atomic.AddInt64(&l.count,1) } @@ -118,11 +124,10 @@ func (l *LoggerManager) Errors () errs.ErrorList { ls := l.errors l.RUnlock() return ls - } -func (l *LoggerManager) Start (all bool) error { +func (l *LoggerManager) Start () error { l.Lock() if l.state == StateStarted { l.Unlock() @@ -133,43 +138,50 @@ func (l *LoggerManager) Start (all bool) error { } l.state = StateStarted - if !all { - l.Unlock() - return nil + er := l.handleLoggers(nil,func(c Logger, _ *Entry) error { return c.Start() }, G.FmtMsgStartErr,) + if er != nil { + l.handleQueue() } - return l.doLoggers(nil,func(c EntryLogger, _ *Entry) error { return c.Start() }, G.FmtMsgLogStartErr,true) + l.Unlock() + return er } -func (l *LoggerManager) Stop (all bool) error { +func (l *LoggerManager) Stop () error { l.Lock() if l.state != StateStarted { l.Unlock() return G.ErrLoggerNotStarted } - l.state = StateStopped - if !all { - l.Unlock() - return nil + er := l.handleLoggers(nil,func(c Logger, _ *Entry) error { return c.Stop() }, G.FmtMsgStopErr) + if er != nil { + l.handleQueue() } - return l.doLoggers(nil,func(c EntryLogger, _ *Entry) error { return c.Stop() }, G.FmtMsgLogStopErr,true) + l.Unlock() + return er } func (l *LoggerManager) Flush () error { l.Lock() - return l.doLoggers(nil,func(c EntryLogger,_ *Entry) error { return c.Flush() }, G.FmtMsgLogFlushErr,true) -} + er := l.handleLoggers(nil,func(c Logger,_ *Entry) error { return c.Flush() }, G.FmtMsgFlushErr) + if er != nil { + l.handleQueue() + } + + l.Unlock() + return er +} -func (l *LoggerManager) GetLogger(name string) EntryLogger { +func (l *LoggerManager) GetLogger(name string) Logger { l.RLock() - var co EntryLogger + var co Logger if p := l.findLogger(name); p != -1 { co = l.loggers[p] } @@ -177,12 +189,13 @@ func (l *LoggerManager) GetLogger(name string) EntryLogger { return co } -func (l *LoggerManager) AddCore (logger EntryLogger) error { + +func (l *LoggerManager) AddLogger(logger Logger) error { l.Lock() nm := logger.Name() if p := l.findLogger(nm); p != -1 { l.Unlock() - return G.ErrDuplicatedLogger.WrapWithFieldString(errFieldCoreName,nm) + return G.ErrDuplicatedLogger.WrapWithFieldString(errFieldLogger,nm) } var er error l.loggers = append(l.loggers,logger) @@ -193,12 +206,13 @@ func (l *LoggerManager) AddCore (logger EntryLogger) error { return er } -func (l *LoggerManager) DelCore (name string) error { + +func (l *LoggerManager) DelLogger(name string) error { l.Lock() p := l.findLogger(name) if p != -1 { l.Unlock() - return G.ErrNotExistsLogger.WrapWithFieldString(entryFieldCoreName,name) + return G.ErrNotExistsLogger.WrapWithFieldString(entryFieldLogger,name) } er := l.loggers[p].Stop() l.loggers = append(l.loggers[:p],l.loggers[p+1:]...) @@ -208,10 +222,8 @@ func (l *LoggerManager) DelCore (name string) error { } - func (l *LoggerManager) GetScope(name string) *LoggerScope { ns := strings.Split(G.ScopeNameTransforms.Transform(name),g.scopeSepStr) - l.RLock() sc := l.scopes.getScope(ns[0]) if sc != nil && len(ns) > 1 { @@ -221,6 +233,7 @@ func (l *LoggerManager) GetScope(name string) *LoggerScope { return sc } + func (l *LoggerManager) AddScope(name string, options ...LoggerScopeFnc) (*LoggerScope,error) { nm := G.ScopeNameTransforms.Transform(name) ns := strings.Split(nm,g.scopeSepStr) @@ -236,7 +249,7 @@ func (l *LoggerManager) AddScope(name string, options ...LoggerScopeFnc) (*Logge } } else if len(ns) == 1 { l.Unlock() - return nil,G.ErrDuplicatedScope.WrapWithFieldString(errScopeCoreName,ns[0]) + return nil,G.ErrDuplicatedScope.WrapWithFieldString(errFieldScope,ns[0]) } l.Unlock() @@ -244,82 +257,84 @@ func (l *LoggerManager) AddScope(name string, options ...LoggerScopeFnc) (*Logge } - -func (l *LoggerManager) Fatal (msg string, err error, ops ...EntryFnc){ +func (l *LoggerManager) Fatal(msg string, err error, ops ...EntryFnc){ l.log(LevelFatal,msg,err,ops) } - -func (l *LoggerManager) Error (msg string, err error, ops ...EntryFnc){ +func (l *LoggerManager) Error(msg string, err error, ops ...EntryFnc){ l.log(LevelError,msg,err,ops) } - -func (l *LoggerManager) Warn (msg string, ops ...EntryFnc){ +func (l *LoggerManager) Warn (msg string, ops ...EntryFnc){ l.log(LevelWarn,msg,nil,ops) } - -func (l *LoggerManager) Info (msg string, ops ...EntryFnc){ +func (l *LoggerManager) Info (msg string, ops ...EntryFnc){ l.log(LevelInfo,msg,nil,ops) } - -func (l *LoggerManager) Debug (msg string, ops ...EntryFnc){ +func (l *LoggerManager) Debug(msg string, ops ...EntryFnc){ l.log(LevelDebug,msg,nil,ops) } - -func (l *LoggerManager) Data (msg string, data any, ops ...EntryFnc){ +func (l *LoggerManager) Data (msg string, data any, ops ...EntryFnc){ l.log(LevelData,msg,nil,[]EntryFnc{EntryWithData(data,ops...)}) } -func (l *LoggerManager) Log (entry *Entry) error { +func (l *LoggerManager) Log (entry *Entry) error { if entry == nil { return nil } l.Lock() + if l.state != StateStarted { l.Unlock() return G.ErrLoggerNotStarted } - if !l.level.Has(entry.Level) || !l.filters.Filter(entry) { + if !l.level.Has(entry.Level) { l.Unlock() return nil } - if entry.Logger == nil { - //viene de un scope - entry.Logger = l - l.extenders.ExtendEntry(entry) + + er := l.logEntry(entry,false) + if er != nil { + l.handleQueue() } - return l.doLoggers(entry,func(c EntryLogger, e *Entry) error { return c.Log(e) },G.FmtMsgLogLogErr,false) + l.Unlock() + return er } func (l *LoggerManager) log (level Level, msg string, err error,options []EntryFnc) { l.Lock() + if l.state != StateStarted || !l.level.Has(level){ l.Unlock() return } - l.incCount() - en := AcquireEntry(l,level,msg,err,options...) - _ = l.doLoggers(en,func(c EntryLogger, e *Entry) error { return c.Log(e) },G.FmtMsgLogLogErr,true) - - if level == LevelFatal && l.onFatalFn != nil { - l.onFatalFn(l,en) + er := l.logEntry(AcquireEntry(l,level,msg,err,options...),true) + if er != nil { + l.handleQueue() } - ReleaseEntry(en) -} + l.Unlock() + return -func (l *LoggerManager) handleEntry (entry *Entry, self bool) error { +} - if self || entry.Logger == nil { - l.extenders.ExtendEntry(entry) - if entry.Logger == nil { - entry.Logger = l - } +func (l *LoggerManager) logEntry(entry *Entry, self bool) error { + if !l.filters.Filter(entry) { + return nil } + if self { + l.incCount() + } else if entry.Logger == nil { + entry.Logger = l + } + l.extenders.ExtendEntry(entry) + + + er := l.handleLoggers(entry,func(c Logger, e *Entry) error { return c.Log(e) },G.FmtMsgLogErr) + if self { if entry.Level == LevelFatal && l.onFatalFn != nil { l.onFatalFn(l, entry) @@ -327,7 +342,7 @@ func (l *LoggerManager) handleEntry (entry *Entry, self bool) error { ReleaseEntry(entry) } - return nil + return er } @@ -335,7 +350,7 @@ func (l *LoggerManager) handleError (err error, entry *Entry) { if err == nil { return } - l.Lock() + l.errors = l.errors.Append(err) if l.logErrors { if entry == nil { @@ -343,7 +358,6 @@ func (l *LoggerManager) handleError (err error, entry *Entry) { } l.queue.Push(entry) } - l.Unlock() if l.onErrorFn != nil { l.onErrorFn(l,err) @@ -351,17 +365,15 @@ func (l *LoggerManager) handleError (err error, entry *Entry) { } - - -func (l *LoggerManager) enqueue() { +func (l *LoggerManager) handleQueue() { for en := l.queue.Pop(); en != nil; { - _ = l.doLoggers(en,func(c EntryLogger, e *Entry) error { return c.Log(e) },G.FmtMsgLogLogErr,true) - ReleaseEntry(en) + _ = l.logEntry(en,true) + en = l.queue.Pop() } } -func (l *LoggerManager) doLoggers(entry *Entry, doFn func(c EntryLogger, e *Entry) error, msg string, unlock bool) error { +func (l *LoggerManager) handleLoggers(entry *Entry, doFn func(c Logger, e *Entry) error, msg string) error { var el errs.ErrorList for _,co := range l.loggers { @@ -374,14 +386,12 @@ func (l *LoggerManager) doLoggers(entry *Entry, doFn func(c EntryLogger, e *Entr en = AcquireEntry(l,LevelError,ms,ne, EntryWithExtender(l.extenders), EntryWithExclude (nm), - EntryWithField (entryFieldCoreName,nm,true)) + EntryWithField (entryFieldLogger,nm,true)) } l.handleError(er,en) } } - if unlock { - l.Unlock() - } + return errs.NewErrMultiIf(el) } @@ -396,6 +406,16 @@ func (l *LoggerManager) findLogger(name string) int { } +func (l *LoggerManager) runLogger () { + for { + select { + case e := <- l.chanLog : + //todo + } + } +} + + func LoggerManagerWithFilters (configFilters EntryFiltersFnc) LoggerManagerFnc { return func(a *LoggerManager) { configFilters(&a.filters) } } diff --git a/alogs/logger_scope.go b/alogs/logger_scope.go index fcdf9cb..f727ac9 100644 --- a/alogs/logger_scope.go +++ b/alogs/logger_scope.go @@ -43,7 +43,7 @@ type ( nodes [][]byte mode ScopeMode - parent Loggable + parent Logger scopes loggerScopes } @@ -52,7 +52,7 @@ type ( ) -func newLoggerScope (name string, level Level, parent Loggable, path string, nodes [][]byte, options []LoggerScopeFnc) *LoggerScope { +func newLoggerScope (name string, level Level, parent Logger, path string, nodes [][]byte, options []LoggerScopeFnc) *LoggerScope { if path != "" { path = fmt.Sprintf("%s%c%s",path,G.ScopeSep,name) } else { @@ -112,6 +112,14 @@ func (l *LoggerScope) SetMode (mode ScopeMode) { l.Unlock() } +// Start Debe de implementarse para que cumpla con el interface Logger +func (_ *LoggerScope)Start () error { return nil } +// Stop Debe de implementarse para que cumpla con el interface Logger +func (_ *LoggerScope)Stop () error { return nil } +// Flush Debe de implementarse para que cumpla con el interface Logger +func (_ *LoggerScope)Flush () error { return nil } + + func (l *LoggerScope) GetScope (name string) *LoggerScope { return l.getScope(strings.Split(strings.ToLower(name),g.scopeSepStr)) } @@ -141,7 +149,7 @@ func (l *LoggerScope) newScope (path []string, options []LoggerScopeFnc) (sc *Lo } } else if len(path) == 1 { l.Unlock() - return nil,G.ErrDuplicatedScope.WrapWithFieldString(errScopeCoreName,path[0]) + return nil,G.ErrDuplicatedScope.WrapWithFieldString(errFieldScope,path[0]) } sc,er = sc.newScope(path[1:],options) l.Unlock() @@ -149,26 +157,22 @@ func (l *LoggerScope) newScope (path []string, options []LoggerScopeFnc) (sc *Lo } + func (l *LoggerScope) Fatal (msg string, err error, ops ...EntryFnc){ l.log(LevelFatal,msg,err,ops ...) } - func (l *LoggerScope) Error (msg string, err error, ops ...EntryFnc){ l.log(LevelError,msg,err,ops ...) } - func (l *LoggerScope) Warn (msg string, ops ...EntryFnc){ l.log(LevelWarn,msg,nil,ops ...) } - func (l *LoggerScope) Info (msg string, ops ...EntryFnc){ l.log(LevelInfo,msg,nil,ops...) } - func (l *LoggerScope) Debug (msg string, ops ...EntryFnc){ l.log(LevelDebug,msg,nil,ops...) } - func (l *LoggerScope) Data (msg string, data any, ops ...EntryFnc){ l.log(LevelData,msg,nil,EntryWithData(data,ops...)) } diff --git a/app/main.go b/app/main.go index 82e51fe..162d473 100644 --- a/app/main.go +++ b/app/main.go @@ -28,16 +28,24 @@ func Append (entry *alogs.Entry) error { } +func Append2 (_ *alogs.Entry) error { + return errs.NewErrMessage(errs.ErrCode("UnError"),"Esto es el mensaje del error") +} + + + func main() { l := alogs.NewLoggerManager("Main",alogs.LevelAll) - l.AddCore(alogs.NewLoggerCore("MiLogger",alogs.LevelAll,alogs.LoggerCoreWithAppender(alogs.AppendEntryFnc(Append)))) - l.Start(true) + l.AddLogger(alogs.NewLoggerCore("MiLogger",alogs.LevelAll,alogs.LoggerCoreWithAppender(alogs.AppendEntryFnc(Append)))) + l.AddLogger(alogs.NewLoggerCore("LoggerProblematico",alogs.LevelAll,alogs.LoggerCoreWithAppender(alogs.AppendEntryFnc(Append2)))) + + l.Start() l.Warn("Esto es un mensage") - l.Error("Esto es un mensage",errs.ErrCode("UnError")) - l.Fatal("Fatalidad",errs.NewErrPanic(errs.ErrCode("unca cauisa"))) + //l.Error("Esto es un mensage",errs.ErrCode("UnError")) + //l.Fatal("Fatalidad",errs.NewErrPanic(errs.ErrCode("unca cauisa")))