Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LogEntry ¶
type LogEntry struct { Timestamp string `json:"timestamp"` Message string `json:"message"` Level string `json:"level"` Fields map[string]interface{} `json:"fields,omitempty"` }
LogEntry represents a log entry structure. It defines the structure of a log entry with various fields.
- Timestamp: the timestamp when the log entry was created.
- Message: the log message.
- Level: the severity level of the log entry.
- Fields: additional fields associated with the log entry (optional). These fields provide extra context or information about the log event. They are stored as key-value pairs in a map[string]interface{}. The "omitempty" tag ensures that the "fields" field is omitted from the JSON output if no additional fields are present in the log entry.
type Logger ¶
type Logger struct { Level string Writer *json.Encoder Parent *Logger Children []*Logger EventFields map[string]interface{} Serializer func(data interface{}) interface{} }
Logger represents a JSON logger instance. It encapsulates the configuration and functionality of a logging instance. - Level: the log level for the logger. - Writer: the JSON encoder used for writing log entries. - Parent: the parent logger if this logger is a child logger. - Children: a list of child loggers created from this logger. - EventFields: additional fields associated with each log entry from this logger. - Serializer: a function for serializing log entry messages and fields (optional).
func GenerateLogger ¶
func GenerateLogger() *Logger
GenerateLogger creates a new logger instance with the specified configuration. It initializes a Logger struct with the provided log level and output writer. - level: the log level for the logger. - writer: the JSON encoder used for writing log entries. Returns the newly created logger instance. The logger is initialized with default settings for parent, children, event fields, and serializer.
func (*Logger) Child ¶
Child creates a child logger with additional event fields. It creates a new logger instance as a child of the current logger. The child logger inherits the log level and event fields from the parent logger. Additionally, it adds the specified additional event fields to its own fields. Returns the newly created child logger that can be used for logging with added context. Child loggers provide a hierarchical structure to organize and filter log entries.
func (*Logger) Log ¶
Log logs a message at the specified level, along with optional additional fields. It first checks if the logger is enabled for the given log level. If the logger is enabled, it constructs a LogEntry with a timestamp, log message, log level, and merged event fields (including additional fields passed as parameters). If a serializer is set for the logger, it applies the serializer function to the log entry before encoding. The serializer can be used to customize the formatting or modify the log entry. Finally, it encodes the log entry using the logger's JSON encoder and writes it to the output. If there is an error encoding the log entry, an error message is printed to the standard output.
func (*Logger) WithFields ¶
WithFields creates a child logger with additional event fields. It creates a new child logger instance based on the current logger. The child logger inherits the log level from the parent logger. Additionally, it adds the specified additional event fields to its own fields. Returns the newly created child logger with the added event fields. The child logger can be used to log with the inherited log level and the additional context provided by the added event fields, allowing more specific and detailed log entries.
func (*Logger) WithSerializers ¶
WithSerializers creates a child logger with custom serializers for specific fields. It creates a new child logger instance based on the current logger. The child logger inherits the log level from the parent logger. It adds custom serializers for specific fields to the child logger's serializer. The serializers are applied to the corresponding fields during log entry serialization, allowing custom formatting or modification of specific fields. Returns the newly created child logger with the added serializers. The child logger can be used to log with the inherited log level, and the added serializers provide customization for specific fields during serialization.