Documentation
¶
Overview ¶
Package log contains log functions to send logs to stdout, files and Elasticsearch.
The log compatible with standard log package, f.e. log.Println are available.
The log messages sends to elasticsearch has json format with basic fields (timestamp, level, message) and additional fields in key-value format.
Index ¶
- Variables
- func Close()
- func Debug(v ...any)
- func Debugf(format string, v ...any)
- func Error(v ...any)
- func Errorf(format string, v ...any)
- func Fatal(v ...any)
- func Fatalf(format string, v ...any)
- func Fatalln(v ...any)
- func Info(v ...any)
- func Infof(format string, v ...any)
- func Init(config *Config)
- func PrintLevel(level LogLevel, v ...any)
- func PrintLevelf(level LogLevel, format string, v ...any)
- func Printf(format string, v ...any)
- func Println(v ...any)
- func Sdebug(v ...any) string
- func Sdebugf(format string, v ...any) string
- func Sentry(level LogLevel, v ...any) string
- func Sentryf(level LogLevel, format string, v ...any) string
- func Serror(message string, v ...any) string
- func Serrorf(format string, v ...any) string
- func SetDefaultLevel(level LogLevel)
- func SetOutput(w io.Writer)
- func Sinfo(message string, v ...any) string
- func Sinfof(format string, v ...any) string
- func Swarn(message string, v ...any) string
- func Swarnf(format string, v ...any) string
- func Warn(v ...any)
- func Warnf(format string, v ...any)
- type Config
- type EsConfig
- type Fields
- type FileConfig
- type LogEntry
- type LogLevel
Constants ¶
This section is empty.
Variables ¶
var LevelDefault = LevelDebug
Default log level uset in standart log calls, f.e. log.Println
Functions ¶
func Close ¶ added in v0.0.2
func Close()
Close closes the Elasticsearch logger and the file logger. It is called once when the application exits. It stops the Elasticsearch logger and the file logger from writing log entries to Elasticsearch and/or to disk.
func Debug ¶
func Debug(v ...any)
Debug is a convenience function for creating log entries at the debug log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Debugf ¶
Debugf is a convenience function for creating log entries at the debug log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
func Error ¶
func Error(v ...any)
Error is a convenience function for creating log entries at the error log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Errorf ¶
Errorf is a convenience function for creating log entries at the error log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
func Fatal ¶
func Fatal(v ...any)
Fatal is a convenience function for creating log entries at the error log level and then exiting the program with a non-zero exit code.
It takes a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The first map in the list is used as the fields for the log entry.
The function calls Error with the given values and then exits the program with a non-zero exit code.
func Fatalf ¶
Fatalf is a convenience function for creating log entries at the error log level and then exiting the program with a non-zero exit code.
It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
The function calls Errorf with the given format string and values and then exits the program with a non-zero exit code.
func Fatalln ¶
func Fatalln(v ...any)
Fatalln is a convenience function for creating log entries at the error log level and then exiting the program with a non-zero exit code.
It takes a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The first map in the list is used as the fields for the log entry.
The function calls Fatal with the given values and then exits the program with a non-zero exit code.
func Info ¶
func Info(v ...any)
Info is a convenience function for creating log entries at the info log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Infof ¶
Infof is a convenience function for creating log entries at the info log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
func Init ¶
func Init(config *Config)
Init sets up the loggers and sets the output for the default application logger and for some additional loggers. It is called once when the application starts. apptype is the application type, which is used to prefix log messages. useStdout is a boolean that indicates whether to write log messages to stdout. esConfig is a pointer to an EsConfig struct, which holds information about how to send log entries to Elasticsearch. If esConfig is not nil, the loggers are set up to write log entries to Elasticsearch. The Elasticsearch logger config is set to the provided esConfig. The Elasticsearch logger config is set to the provided esConfig. If the MaxOverflowBuffer is 0, it is set to 2x the entryChannel size, or a fixed number if entryChannel size is 0. If the MaxRetryBuffer is 0, it is set to 100 batches. The Elasticsearch logger config is set to the provided esConfig. The Elasticsearch logger config is set to the provided esConfig. The loggers are then started and the output for the default application logger and for some additional loggers is set to a customWriter, which writes log entries to stdout and/or to Elasticsearch. Finally, a message is printed to indicate that the loggers have been initialized.
func PrintLevel ¶
PrintLevel is a convenience function for creating log entries at the given log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func PrintLevelf ¶
PrintLevelf is a convenience function for creating log entries at the given log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
The format string is used to format the values passed in via the variable argument list. The resulting log entry will contain the formatted message and the fields passed in via the variable argument list.
func Printf ¶
Printf is a convenience function for creating log entries at the debug log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
The format string is used to format the values passed in via the variable argument list. The resulting log entry will contain the formatted message and the fields passed in via the variable argument list.
func Println ¶
func Println(v ...any)
Println is a convenience function for creating log entries at the debug log level. It takes a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Sdebug ¶
Sdebug is a convenience function for creating log entries at the debug log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Sdebugf ¶
Sdebugf is a convenience function for creating log entries at the debug log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
func Sentry ¶
Sentry is a convenience function for creating log entries at the given log level. It takes a message, and a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Sentryf ¶
Sentryf is a convenience function for creating log entries at the given log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
The format string is used to format the values passed in via the variable argument list. The resulting log entry will contain the formatted message and the fields passed in via the variable argument list.
func Serror ¶
Serror is a convenience function for creating log entries at the error log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
The function returns a JSON representation of the log entry as a string.
func Serrorf ¶
Serrorf is a convenience function for creating log entries at the error log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
The format string is used to format the values passed in via the variable argument list. The resulting log entry will contain the formatted message and the fields passed in via the variable argument list.
func SetDefaultLevel ¶
func SetDefaultLevel(level LogLevel)
SetDefaultLevel sets the default log level for the logger.
The default log level is DEBUG. The default log level can be changed using this function.
The default log level used in the standart log calls, f.e. log.Println. If the default log level is set to NONE, the logger will not write any the standart log calls.
It takes a logLevel value as its argument, which can be any of the following: DEBUG, INFO, WARN, ERROR, or NONE.
The logger will write log messages with the specified log level and above (i.e., if the log level is set to WARN, the logger will write log messages with the log levels WARN and ERROR).
func Sinfo ¶
Sinfo is a convenience function for creating log entries at the info log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Sinfof ¶
Sinfof is a convenience function for creating log entries at the info log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
The format string is used to format the values passed in via the variable argument list. The resulting log entry will contain the formatted message and the fields passed in via the variable argument list.
func Swarn ¶
Swarn is a convenience function for creating log entries at the warn log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Swarnf ¶
Swarnf is a convenience function for creating log entries at the warn log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
The format string is used to format the values passed in via the variable argument list. The resulting log entry will contain the formatted message and the fields passed in via the variable argument list.
func Warn ¶
func Warn(v ...any)
Warn is a convenience function for creating log entries at the warn log level. It takes a variable argument list of maps, allowing the caller to pass in any number of fields to be included in the log entry. The first map in the list is used as the fields for the log entry.
func Warnf ¶
Warnf is a convenience function for creating log entries at the warn log level. It takes a format string and a variable argument list of values, allowing the caller to pass in any number of values to be included in the log entry. The last value in the list is expected to be a map[string]any, which is used as the fields for the log entry.
Types ¶
type Config ¶
type Config struct {
// AppShort is the short name of the application
AppShort string
// AppType is the type of the application, f.e. "DEV" or "PROD"
AppType string
// UseStdout is a boolean that indicates whether to write log messages to
// stdout
UseStdout bool
// EsConfig is the configuration for the Elasticsearch logger.
// If nil, the Elasticsearch logger is not used
*EsConfig
// File is the configuration for the file logger.
// If nil, the file logger is not used
*FileConfig
// When loger initialized it prints "logger initialized" message. If set
// this field to true, this message will not be printed.
DoesNotShowInitMessage bool
// CustomLogers is some custom loggers, it output will be parsed and added
// to this loger output.
CustomLogers []*log.Logger
// FilterLevel is a list of log levels to filter out.
FilterLevels []LogLevel
}
Config is a struct that holds configuration information for the logger.
type EsConfig ¶
type EsConfig struct {
ES_URL string // Elasticsearch URL
ES_API_KEY string // Elasticsearch API key
ES_INDEX_NAME string // Elasticsearch index name
// Time to hold before sending log entries to Elasticsearch.
// If not set, Default is 5 seconds.
TimeToHold time.Duration
// Maximum number of log entries to hold before sending them to Elasticsearch.
// If not set, Default is 1000.
EntriesToHold int
// Directory to store failed batches on disk.
// If not set, Default is "/tmp/APP_SHORT_NAME/failover".
FailoverDir string
// Maximum number of failover files to keep on disk.
// If not set, Default is 10.
MaxFailoverFiles int
}
EsConfig is a struct that holds information about how to send log entries to Elasticsearch.
Elasticsearch index creation in Kibana Dev Tools:
Delete existing index:
DELETE /app-name-index
Create new index:
PUT /app-name-index
{
"mappings": {
"properties": {
"@timestamp": {
"type": "date_nanos"
},
"app_type": {
"type": "keyword"
},
"level": {
"type": "keyword"
},
"message": {
"type": "text"
},
"fields": {
"type": "object"
}
}
}
}
type FileConfig ¶
type FileConfig struct {
// Log files folder
Folder string
// Create new log file after
CreateNewAfter time.Duration
// Remove old log file after
RemoveOldAfter time.Duration
// Remove old log file suffixes, e.g. ".log", ".log.gz".
// By default if RemoveSuffixes is empty then ".log.gz" is used
RemoveSuffixes []string
// contains filtered or unexported fields
}
FileConfig is a struct that holds information about how to send log entries to a file.
type LogEntry ¶
type LogEntry struct {
AppType string `json:"app_type"` // Prod, Dev, Test or some other
Timestamp string `json:"@timestamp"`
Level LogLevel `json:"level"`
Message string `json:"message"`
Fields map[string]any `json:"fields,omitempty"`
}
LogEntry is a struct that represents a log entry.
It contains the following fields:
- Timestamp: the timestamp for the log entry.
- Level: the log level for the log entry.
- Message: the log message for the log entry.
- Fields: a map of additional fields to be included in the log entry.
The String method returns a JSON representation of the log entry.
func (*LogEntry) Json ¶
Json returns a JSON representation of a log entry.
It marshals the log entry into a JSON byte slice and returns the JSON representation of the log entry as a string.
func (*LogEntry) String ¶
String returns a string representation of a log entry.
It formats the log entry as a string in the following format:
<timestamp> [<level>] <message>[, fields: <fields>]
The timestamp is formatted as per RFC3339. The level is the log level. The message is the log message. The fields are the additional fields that were passed in when creating the log entry.