Documentation
¶
Overview ¶
Package glogger provides a logger that can be used to write log messages to an output destination, such as the standard output or a file. It has various levels of log severity (e.g. debug, info, warning, error, fatal) and the following formats: inline-string, json.
The global variables output, logFormat, and logLevel control the output destination, format, and minimum severity level of log messages, respectively. You can set these variables using the functions SetOutput, SetLogFormat, and SetLogLevel.
The function _print writes the given message to the output destination. The functions Debug, Info, Warn, Error, and Fatal write log messages with the corresponding severity levels, if the log level of the message is equal to or higher than the minimum log level set.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetLogFormat ¶
func SetLogFormat(format LogFormat)
SetLogFormat sets the global variable that controls the format of the log lines being printed
func SetLogLevel ¶
func SetLogLevel(level LogLevel)
SetLogLevel sets the global variable that controls the minimum severity level required for a message to pass through. If an invalid value is passed it sets it to LogLevelDebug
func SetOutput ¶
SetOutput sets the global variable that specifies the output writer where log messages will be written.
For example, the following code configures the logger to write messages to a file named "app.log":
f, err := os.OpenFile("app.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("error opening file: %v", err) } defer f.Close() glogger.SetOutput(f)
Types ¶
type Caller ¶
type Caller struct { Service string `json:"service,omitempty"` Package string `json:"package,omitempty"` Receiver string `json:"receiver,omitempty"` Function string `json:"function,omitempty"` }
Caller is the source of the logs. It is included in the log data
type LogFormat ¶
type LogFormat uint8
LogFormat represents the format of a log message
Constants representing the available log formats.
func StrToLogFormat ¶
StrToLogFormat matches the given string to a supported LogFormat. If no matches are found it returns LogFormatInlineString
type LogLevel ¶
type LogLevel uint8
LogLevel is the type that represents the log level
Constants representing the available log levels.
func StrToLogLevel ¶
StrToLogLevel matches the given string to a supported LogLevel. If no matches are found it returns LogLevelDebug
type Logger ¶
type Logger interface { // Err sets the error field in the log entry to the given value. Err(err error) Logger // RequestID sets the requestID field in the log entry to the given value. RequestID(id string) Logger // TraceID sets the traceID field in the log entry to the given value. TraceID(id string) Logger // SpanID sets the spanID field in the log entry to the given value. SpanID(id string) Logger // Data adds a data field in the log entry as key => val. Data(key string, val interface{}) Logger // Debug logs a message at the debug severity level. Debug(msg string) // Info logs a message at the info severity level. Info(msg string) // Warn logs a message at the warning severity level. Warn(msg string) // Error logs a message at the error severity level. Error(msg string) // Fatal logs a message at the fatal severity level and terminates the program. Fatal(msg string) }
Logger is the interface that defines methods for logging messages at different severity levels.