github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/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 save logs as log event in time chain for the node that create log.
    12  	// Suggest that RecordID time chain by sha3.256(LogMediatypeID, NodeID, TimeRoundToDay)
    13  	Log(event LogEvent)
    14  	// 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
    15  	// LogFatal(event LogEvent)
    16  
    17  	// Log listener mechanism usually implement on some kind of services that:
    18  	// - Carry log event to desire node and show on screen e.g. in control room of the organization
    19  	// - Notify to related person about critical log that must check as soon as possible by pager, sms, email, web notification, user GUI app, ...
    20  	// - Local GUI application to notify the developers in AppMode_Dev
    21  	EventTarget
    22  }
    23  
    24  type LogEvent interface {
    25  	Event
    26  
    27  	Level() LogType
    28  	Time() Time
    29  	Domain() string
    30  	Message() string // save formated data e.g. fmt.Sprintf("Panic Exception: %s\nDebug Stack: %s", r, debug.Stack())
    31  	Stack() []byte   // if log need to trace, specially in panic situation
    32  }
    33  
    34  // LogType indicate log level that will also use as EventSubType too.
    35  type LogType uint8
    36  
    37  const (
    38  	LogEvent_Unset LogType = iota
    39  	LogEvent_Information
    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  const (
    56  	LogMode_Debug        = true
    57  	LogMode_DeepDebug    = true
    58  	LogMode_Confidential = false
    59  )