2020-11-02 13:25:29 +03:00
|
|
|
// Package logger provides a log interface
|
2024-07-06 00:09:27 +03:00
|
|
|
package logger
|
2020-02-07 00:35:46 +03:00
|
|
|
|
2021-08-27 18:55:08 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
2021-01-10 18:56:39 +03:00
|
|
|
|
2024-03-04 23:58:31 +03:00
|
|
|
type ContextAttrFunc func(ctx context.Context) []interface{}
|
|
|
|
|
|
|
|
var DefaultContextAttrFuncs []ContextAttrFunc
|
|
|
|
|
2020-02-21 10:57:59 +03:00
|
|
|
var (
|
2020-11-02 13:25:29 +03:00
|
|
|
// DefaultLogger variable
|
2024-03-06 18:45:32 +03:00
|
|
|
DefaultLogger Logger = NewLogger()
|
2021-02-14 11:28:50 +03:00
|
|
|
// DefaultLevel used by logger
|
2022-05-03 14:38:44 +03:00
|
|
|
DefaultLevel = InfoLevel
|
2021-07-26 09:48:15 +03:00
|
|
|
// DefaultCallerSkipCount used by logger
|
2024-10-09 20:32:54 +03:00
|
|
|
DefaultCallerSkipCount = 3
|
2020-02-07 00:35:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Logger is a generic logging interface
|
|
|
|
type Logger interface {
|
2020-02-07 00:36:33 +03:00
|
|
|
// Init initialises options
|
2020-10-16 09:38:57 +03:00
|
|
|
Init(opts ...Option) error
|
2021-08-30 16:19:35 +03:00
|
|
|
// Clone create logger copy with new options
|
|
|
|
Clone(opts ...Option) Logger
|
2020-08-29 17:43:06 +03:00
|
|
|
// V compare provided verbosity level with current log level
|
|
|
|
V(level Level) bool
|
2021-08-30 16:19:35 +03:00
|
|
|
// Level sets the log level for logger
|
|
|
|
Level(level Level)
|
2020-02-20 11:26:12 +03:00
|
|
|
// The Logger options
|
|
|
|
Options() Options
|
2021-08-06 02:15:57 +03:00
|
|
|
// Fields set fields to always be logged with keyval pairs
|
|
|
|
Fields(fields ...interface{}) Logger
|
2020-11-04 00:38:12 +03:00
|
|
|
// Info level message
|
2021-01-10 18:56:39 +03:00
|
|
|
Info(ctx context.Context, args ...interface{})
|
2020-11-04 00:38:12 +03:00
|
|
|
// Trace level message
|
2021-01-10 18:56:39 +03:00
|
|
|
Trace(ctx context.Context, args ...interface{})
|
2020-11-04 00:38:12 +03:00
|
|
|
// Debug level message
|
2021-01-10 18:56:39 +03:00
|
|
|
Debug(ctx context.Context, args ...interface{})
|
2020-11-04 00:38:12 +03:00
|
|
|
// Warn level message
|
2021-01-10 18:56:39 +03:00
|
|
|
Warn(ctx context.Context, args ...interface{})
|
2020-11-04 00:38:12 +03:00
|
|
|
// Error level message
|
2021-01-10 18:56:39 +03:00
|
|
|
Error(ctx context.Context, args ...interface{})
|
2020-11-04 00:38:12 +03:00
|
|
|
// Fatal level message
|
2021-01-10 18:56:39 +03:00
|
|
|
Fatal(ctx context.Context, args ...interface{})
|
2020-11-10 22:18:16 +03:00
|
|
|
// Infof level message
|
2024-10-02 18:03:56 +03:00
|
|
|
// DEPRECATED
|
2021-01-10 18:56:39 +03:00
|
|
|
Infof(ctx context.Context, msg string, args ...interface{})
|
2020-11-10 22:18:16 +03:00
|
|
|
// Tracef level message
|
2024-10-02 18:03:56 +03:00
|
|
|
// DEPRECATED
|
2021-01-10 18:56:39 +03:00
|
|
|
Tracef(ctx context.Context, msg string, args ...interface{})
|
2024-10-02 18:03:56 +03:00
|
|
|
// Debugf level message
|
|
|
|
// DEPRECATED
|
2021-01-10 18:56:39 +03:00
|
|
|
Debugf(ctx context.Context, msg string, args ...interface{})
|
2024-10-02 18:03:56 +03:00
|
|
|
// Warnf level message
|
|
|
|
// DEPRECATED
|
2021-01-10 18:56:39 +03:00
|
|
|
Warnf(ctx context.Context, msg string, args ...interface{})
|
2024-10-02 18:03:56 +03:00
|
|
|
// Errorf level message
|
|
|
|
// DEPRECATED
|
2021-01-10 18:56:39 +03:00
|
|
|
Errorf(ctx context.Context, msg string, args ...interface{})
|
2024-10-02 18:03:56 +03:00
|
|
|
// Fatalf level message
|
|
|
|
// DEPRECATED
|
2021-01-10 18:56:39 +03:00
|
|
|
Fatalf(ctx context.Context, msg string, args ...interface{})
|
|
|
|
// Log logs message with needed level
|
|
|
|
Log(ctx context.Context, level Level, args ...interface{})
|
|
|
|
// Logf logs message with needed level
|
2024-10-02 18:03:56 +03:00
|
|
|
// DEPRECATED
|
2021-01-10 18:56:39 +03:00
|
|
|
Logf(ctx context.Context, level Level, msg string, args ...interface{})
|
2024-03-04 01:09:08 +03:00
|
|
|
// Name returns broker instance name
|
|
|
|
Name() string
|
|
|
|
// String returns the type of logger
|
2020-02-07 00:36:33 +03:00
|
|
|
String() string
|
2020-02-07 00:35:46 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 02:15:57 +03:00
|
|
|
// Field contains keyval pair
|
|
|
|
type Field interface{}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Info writes msg to default logger on info level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Info(ctx context.Context, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Info(ctx, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Error writes msg to default logger on error level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Error(ctx context.Context, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Error(ctx, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Debug writes msg to default logger on debug level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Debug(ctx context.Context, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Debug(ctx, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Warn writes msg to default logger on warn level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Warn(ctx context.Context, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Warn(ctx, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Trace writes msg to default logger on trace level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Trace(ctx context.Context, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Trace(ctx, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Fatal writes msg to default logger on fatal level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Fatal(ctx context.Context, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Fatal(ctx, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Infof writes formatted msg to default logger on info level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Infof(ctx context.Context, msg string, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Infof(ctx, msg, args...)
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
2020-02-07 00:35:46 +03:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Errorf writes formatted msg to default logger on error level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Errorf(ctx context.Context, msg string, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Errorf(ctx, msg, args...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Debugf writes formatted msg to default logger on debug level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Debugf(ctx context.Context, msg string, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Debugf(ctx, msg, args...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Warnf writes formatted msg to default logger on warn level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Warnf(ctx context.Context, msg string, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Warnf(ctx, msg, args...)
|
2020-02-07 00:35:46 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Tracef writes formatted msg to default logger on trace level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Tracef(ctx context.Context, msg string, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Tracef(ctx, msg, args...)
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
2020-02-07 00:35:46 +03:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Fatalf writes formatted msg to default logger on fatal level
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-01-10 18:56:39 +03:00
|
|
|
func Fatalf(ctx context.Context, msg string, args ...interface{}) {
|
2024-03-06 00:53:20 +03:00
|
|
|
DefaultLogger.Clone(WithCallerSkipCount(DefaultCallerSkipCount+1)).Fatalf(ctx, msg, args...)
|
2020-02-07 00:35:46 +03:00
|
|
|
}
|
2020-02-15 21:19:28 +03:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// V returns true if passed level enabled in default logger
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2020-11-04 00:38:12 +03:00
|
|
|
func V(level Level) bool {
|
|
|
|
return DefaultLogger.V(level)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initialize logger
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2020-11-04 00:38:12 +03:00
|
|
|
func Init(opts ...Option) error {
|
|
|
|
return DefaultLogger.Init(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fields create logger with specific fields
|
2024-03-06 00:53:20 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations
|
2021-08-06 02:15:57 +03:00
|
|
|
func Fields(fields ...interface{}) Logger {
|
|
|
|
return DefaultLogger.Fields(fields...)
|
2020-02-15 21:19:28 +03:00
|
|
|
}
|