github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/log/log.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package log
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"../event"
     9  	"../protocol"
    10  )
    11  
    12  type Logger struct {
    13  	event.EventTarget
    14  }
    15  
    16  // PanicHandler recover from panics if exist to prevent app stop.
    17  // Call it by defer in any goroutine due to >> https://github.com/golang/go/issues/20161
    18  func (l *Logger) PanicHandler() {
    19  	var r = recover()
    20  	if r != nil {
    21  		var logEvent protocol.LogEvent
    22  		switch message := r.(type) {
    23  		case protocol.LogEvent:
    24  			logEvent = message
    25  		case protocol.Error:
    26  			logEvent = PanicEvent("Unknown protocol.Error Domain", message.ToString())
    27  		case error:
    28  			logEvent = PanicEvent("Unknown Error Domain", message.Error())
    29  		case string:
    30  			logEvent = PanicEvent("Unknown string Domain", message)
    31  		case protocol.Stringer:
    32  			logEvent = PanicEvent("Unknown Stringer Domain", message.ToString())
    33  		default:
    34  			logEvent = PanicEvent("Unknown Domain", fmt.Sprint(r))
    35  		}
    36  		l.Log(logEvent)
    37  	}
    38  }
    39  
    40  func (l *Logger) Log(event protocol.LogEvent) {
    41  	// TODO::: it is a huge performance impact to check each logging, force caller to check before call log?
    42  	// This func can inline means constants check on compile time?
    43  	if (!protocol.AppMode_Dev && event.Level() == protocol.LogEvent_Unset) ||
    44  		(!protocol.LogMode_Debug && event.Level() == protocol.LogEvent_Debug) ||
    45  		(!protocol.LogMode_DeepDebug && event.Level() == protocol.LogEvent_DeepDebug) {
    46  		return
    47  	}
    48  
    49  	l.DispatchEvent(event)
    50  	l.saveEvent(event)
    51  }
    52  
    53  func (l *Logger) saveEvent(event protocol.LogEvent) {
    54  	// TODO::: First save locally as cache to reduce network trip for non important data??
    55  
    56  	// TODO::: Each day, Save logs to storage in one object with this ID: sha3.256(mediatype.LOG.ID(), NodeID, TimeRoundToDay)
    57  	// protocol.App.Objects().
    58  }