github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/protocol/log.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package protocol
     4  
     5  // Logger provide logging mechanism to prevent application from runtime crashes and
     6  // save details about runtime events to check by developers to fix bugs or develope better features.
     7  type Logger interface {
     8  	// PanicHandler recover from panics in a goroutine if exist, to prevent the application unexpected stopping.
     9  	PanicHandler()
    10  
    11  	// Log suggest to:
    12  	// - First Dispatch(event).
    13  	// - Cache log events in the node that create it.
    14  	// - Save all logs per day for a node in the record with LogMediatypeID as record type and NodeID as primary key.
    15  	Log(event LogEvent) Error
    16  
    17  	// Due to expect Fatal terminate app and it brake the app, Dev must design it in the app architecture with panic and log the event with LogEvent_Fatal
    18  	// LogFatal(event LogEvent)
    19  
    20  	// Log listener mechanism usually implement on some kind of services that:
    21  	// - Carry log event to desire node and show on screen e.g. in control room of the organization
    22  	// - Notify to related person about critical log that must check as soon as possible by pager, sms, email, web notification, user GUI app, ...
    23  	// - Local GUI application to notify the developers in AppMode_Dev
    24  	EventTarget
    25  }
    26  
    27  type LogEvent interface {
    28  	Event
    29  
    30  	Level() LogType  // same as Event.SubType() just with LogType type
    31  	Message() string // save formatted data e.g. fmt.Sprintf("Panic Exception: %s\nDebug Stack: %s", r, debug.Stack())
    32  	Stack() []byte   // if log need to trace, specially in panic situation. Default fill by `debug.Stack()`
    33  }
    34  
    35  // LogType indicate log level that will also use as EventSubType too.
    36  type LogType = EventSubType
    37  
    38  const (
    39  	LogEvent_Information LogType = (1 << iota)
    40  	LogEvent_Notice
    41  	LogEvent_Debug // Detailed information on the flow through the system. Expect these to be written to logs only. Generally speaking, most lines logged by your application should be written as DEBUG.
    42  	LogEvent_DeepDebug
    43  	LogEvent_Warning // Use of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
    44  	LogEvent_Error   // Other runtime errors or unexpected conditions
    45  	LogEvent_Alert
    46  	LogEvent_Panic // in panic() it will add debug stack to trace more easily panic errors
    47  	LogEvent_Critical
    48  	LogEvent_Emergency
    49  	LogEvent_Fatal // Severe errors that cause premature termination
    50  	LogEvent_Security
    51  	LogEvent_Confidential
    52  )
    53  
    54  // If any below mode disabled, logger must not save that log type.
    55  // But logger must Dispatch() it to any client requested any types even it is not enabled.
    56  const (
    57  	LogMode LogType = LogEvent_Debug | LogEvent_DeepDebug | LogEvent_Warning | LogEvent_Error | LogEvent_Alert |
    58  		LogEvent_Panic | LogEvent_Critical | LogEvent_Emergency | LogEvent_Fatal | LogEvent_Security | LogEvent_Confidential
    59  )