Documentation
¶
Index ¶
- Variables
- type CtxKey
- type CustomLogger
- type Logger
- type Logging
- func (logger *Logging) Debug(args ...any)
- func (logger *Logging) Debugf(args ...any)
- func (logger *Logging) Error(args ...any)
- func (logger *Logging) Errorf(args ...any)
- func (logger *Logging) Fatal(args ...any)
- func (logger *Logging) Fatalf(args ...any)
- func (logger *Logging) GetLevel(level int, ctx any) (string, string, bool)
- func (logger *Logging) Info(args ...any)
- func (logger *Logging) Infof(args ...any)
- func (logger *Logging) Print(level int, args ...any)
- func (logger *Logging) Printf(level int, args ...any)
- func (logger *Logging) Starting(title string)
- func (logger *Logging) Stopping()
- func (logger *Logging) TimeToStr(t time.Time) string
- func (logger *Logging) Warn(args ...any)
- func (logger *Logging) Warnf(args ...any)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Logs Logging CtxKeyUUID CtxKey = "process-uuid" // Context key for process UUID GraylogAddr string = "" // Graylog address (when empty, Graylog is disabled) Application string = "go-application" // Application name Host string = "localhost" // Host name Facility string = "facility" // Facility name )
Functions ¶
This section is empty.
Types ¶
type CustomLogger ¶ added in v1.0.4
type CustomLogger struct {
// contains filtered or unexported fields
}
CustomLogger is a wrapper around a Logger interface that allows for custom logging implementations. It provides methods to log messages
Example ¶
cLog := &Logging{
LogLevel: 0,
UUID: "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049",
ShowTime: false,
ConsoleApp: false,
DontStop: true, // Prevent exit on fatal error
}
logs := &CustomLogger{}
logs.SetLogger(cLog)
Logs.LogLevel = 0
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050"
Logs.ShowTime = false
Logs.Starting("test")
Logs.DontStop = true // Prevent exit on fatal error
defer Logs.Stopping()
ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")
world := "World"
Logs.Debug(ctx, "Standard Logger with ctx and variable: %s", world)
Logs.Debug(ctx, "Standard Logger with ctx without variable")
Logs.Debug("Standard Logger without ctx and variable: %s", world)
Logs.Debug("Standard Logger without ctx without variable")
logs.Debug(ctx, "CustomerLogger with ctx and variable: %s", world)
logs.Debug(ctx, "CustomerLogger with ctx without variable")
logs.Debug("CustomerLogger without ctx with variable: %s", world)
logs.Debug("CustomerLogger without ctx without variable")
logs.Info(ctx, "CustomerLogger with ctx and variable: %s", world)
logs.Info(ctx, "CustomerLogger with ctx without variable")
logs.Info("CustomerLogger without ctx with variable: %s", world)
logs.Info("CustomerLogger without ctx without variable")
logs.Warn(ctx, "CustomerLogger with ctx and variable: %s", world)
logs.Warn(ctx, "CustomerLogger with ctx without variable")
logs.Warn("CustomerLogger without ctx with variable: %s", world)
logs.Warn("CustomerLogger without ctx without variable")
logs.Error(ctx, "CustomerLogger with ctx and variable: %s", world)
logs.Error(ctx, "CustomerLogger with ctx without variable")
logs.Error("CustomerLogger without ctx with variable: %s", world)
logs.Error("CustomerLogger without ctx without variable")
logs.Fatal(ctx, "CustomerLogger with ctx and variable: %s", world)
Output: INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050] test service is starting... INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050] test service is stopping... DBG [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Standard Logger with ctx and variable: World DBG [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Standard Logger with ctx without variable DBG [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050] Standard Logger without ctx and variable: World DBG [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050] Standard Logger without ctx without variable DBG [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx and variable: World DBG [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx without variable DBG [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] CustomerLogger without ctx with variable: World DBG [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] CustomerLogger without ctx without variable INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx and variable: World INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx without variable INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] CustomerLogger without ctx with variable: World INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] CustomerLogger without ctx without variable WRN [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx and variable: World WRN [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx without variable WRN [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] CustomerLogger without ctx with variable: World WRN [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] CustomerLogger without ctx without variable ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx and variable: World ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx without variable ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] CustomerLogger without ctx with variable: World ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] CustomerLogger without ctx without variable FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] CustomerLogger with ctx and variable: World
func (*CustomLogger) Debug ¶ added in v1.0.4
func (dst *CustomLogger) Debug(args ...any)
Debug logs a debug message using the provided logger or the default logging mechanism. It is useful for debugging purposes and can be used to log detailed information about the API calls and their responses.
Parameters:
- args: The debug message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print
func (*CustomLogger) Error ¶ added in v1.0.4
func (dst *CustomLogger) Error(args ...any)
Error logs an error message using the provided logger or the default logging mechanism. It is useful for logging errors encountered during API calls or other operations.
Parameters:
- args: The error message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print
func (*CustomLogger) Fatal ¶ added in v1.0.4
func (dst *CustomLogger) Fatal(args ...any)
Fatal logs a fatal error message using the provided logger or the default logging mechanism. It is useful for logging critical errors that require immediate attention and may cause the application to exit.
Parameters:
- args: The fatal error message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print
func (*CustomLogger) Info ¶ added in v1.0.4
func (dst *CustomLogger) Info(args ...any)
Info logs an informational message using the provided logger or the default logging mechanism. It is useful for logging general information about the API calls and their responses.
Parameters:
- args: The informational message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print
func (*CustomLogger) SetLogger ¶ added in v1.0.4
func (dst *CustomLogger) SetLogger(logger Logger)
SetLogger allows setting a custom logger. This is useful when you want to use a different logging mechanism instead of the default one provided by the package.
Parameters:
- logger: An instance of a type that implements the Logger interface.
func (*CustomLogger) Warn ¶ added in v1.0.4
func (dst *CustomLogger) Warn(args ...any)
Warn logs a warning message using the provided logger or the default logging mechanism. It is useful for logging potential issues or unexpected behavior in the API calls.
Parameters:
- args: The warning message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print
type Logger ¶ added in v1.0.4
type Logger interface {
// Debug logs a message at Debug level.
// The first argument is the context for the log entry,
Debug(args ...any)
// Info logs a message at Info level.
// The first argument is the context for the log entry,
Info(args ...any)
// Warn logs a message at Warning level.
// The first argument is the context for the log entry,
Warn(args ...any)
// Error logs a message at Error level.
// The first argument is the context for the log entry,
Error(args ...any)
// Fatal logs a message at Fatal level
// and process will exit with status set to 1.
// The first argument is the context for the log entry,
Fatal(args ...any)
}
Logger Interface defines the methods for logging at different levels.
type Logging ¶
type Logging struct {
UUID string
LogLevel int // Log level (0 - debug, 1 - warning, 2 - error, 3 - fatal, default 0)
ConsoleApp bool // Console application flag (do not print logs in console app)
ShowTime bool // Show time in logs
DontStop bool // Do not stop service on fatal error
// contains filtered or unexported fields
}
func (*Logging) Debug ¶
Debug logs a debug message.
Parameters:
- args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example ¶
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
defer Logs.Stopping()
ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")
testCases := []int{0, 1, 2, 3}
for _, tc := range testCases {
Logs.LogLevel = tc
Logs.Debug("Hello World")
Logs.Debugf("Hello %s", "Universe")
Logs.Debug(ctx, "Hello World")
Logs.Debugf(ctx, "Hello %s", "Universe")
}
Output: INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is starting... INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is stopping... DBG [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World DBG [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe DBG [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World DBG [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe
func (*Logging) Debugf ¶
Debugf logs a formatted debug message.
Parameters:
- args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) # args[2:] - arguments to format string
func (*Logging) Error ¶
Error logs an error message.
Parameters:
- args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example ¶
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
defer Logs.Stopping()
ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")
testCases := []int{0, 1, 2, 3}
for _, tc := range testCases {
Logs.LogLevel = tc
Logs.Error("Hello World")
Logs.Errorf("Hello %s", "Universe")
Logs.Error(ctx, "Hello World")
Logs.Errorf(ctx, "Hello %s", "Universe")
}
Output: INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is starting... INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is stopping... ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe
func (*Logging) Errorf ¶
Errorf logs a formatted error message.
Parameters:
- args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) # args[2:] - arguments to format string
func (*Logging) Fatal ¶
Fatal logs a fatal error message and exits the program.
Parameters:
- args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example ¶
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
Logs.DontStop = true // Prevent exit on fatal error
defer Logs.Stopping()
ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")
testCases := []int{0, 1, 2, 3}
for _, tc := range testCases {
Logs.LogLevel = tc
Logs.Fatal("Hello World")
Logs.Fatalf("Hello %s", "Universe")
Logs.Fatal(ctx, "Hello World")
Logs.Fatalf(ctx, "Hello %s", "Universe")
}
Output: INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is starting... INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is stopping... FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe
func (*Logging) Fatalf ¶
Fatalf logs a formatted fatal error message and exits the program.
Parameters:
- args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) # args[2:] - arguments to format string
func (*Logging) GetLevel ¶
Get level of logging by level and context if it's present
Parameters:
- level - log level
- ctx - context
func (*Logging) Info ¶
Info logs an informational message.
Parameters:
- args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example ¶
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
defer Logs.Stopping()
ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")
Logs.Info("Hello World")
Logs.Infof("Hello %s", "Universe")
Logs.Info(ctx, "Hello World")
Logs.Infof(ctx, "Hello %s", "Universe")
Output: INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is starting... INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is stopping... INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe
func (*Logging) Infof ¶
Infof logs a formatted informational message.
Parameters:
- args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) or argument to print # args[2:] - arguments to format string
func (*Logging) Print ¶
Print logs to console
Parameters:
- level - log level (0 - debug, 1 - warning, 2 - error, 3 - fatal, 4 - info)
- args - arguments to print
Example ¶
Logs.LogLevel = 0
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.ShowTime = false
Logs.Starting("test")
defer Logs.Stopping()
ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")
testCases := []int{-1, 0, 1, 2, 3, 4, 5}
for _, tc := range testCases {
Logs.Print(tc, "Hello World")
Logs.Printf(tc, "Hello %s", "Universe")
Logs.Print(tc, ctx, "Hello World")
Logs.Printf(tc, ctx, "Hello %s", "Universe")
}
Output: INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is starting... INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is stopping... DBG [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World WRN [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World DBG [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe WRN [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe ERR [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe FTL [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe DBG [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World WRN [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World DBG [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe WRN [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe ERR [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe FTL [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe INF [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe
func (*Logging) Printf ¶
Printf logs formatted output to console
Parameters:
- level - log level (0 - debug, 1 - warning, 2 - error, 3 - fatal, 4 - info)
- args - arguments to print # args[0] - format string # args[1:] - arguments to format string
func (*Logging) TimeToStr ¶
TimeToStr converts time.Time to string in format "2006/01/02 15:04:05.999" It ensures that the string is always 23 characters long by appending "00" or "0" as needed.
Parameters:
- t - time.Time object to convert
Returns:
- string: representation of the time in the specified format
func (*Logging) Warn ¶
Warn logs a warning message.
Parameters:
- args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example ¶
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
defer Logs.Stopping()
ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")
testCases := []int{0, 1, 2, 3}
for _, tc := range testCases {
Logs.LogLevel = tc
Logs.Warn("Hello World")
Logs.Warnf("Hello %s", "Universe")
Logs.Warn(ctx, "Hello World")
Logs.Warnf(ctx, "Hello %s", "Universe")
}
Output: INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is starting... INF [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] test service is stopping... WRN [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World WRN [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe WRN [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello World WRN [b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049] Hello Universe WRN [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World WRN [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe WRN [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello World WRN [4577c272-e9b8-4a19-a9d0-4ec0bde6063f] Hello Universe