2020-11-02 13:25:29 +03:00
|
|
|
// Package logger provides a log interface
|
2023-10-17 01:00:00 +03:00
|
|
|
package logger
|
2020-02-07 05:35:46 +08:00
|
|
|
|
2021-08-27 18:55:08 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2023-07-29 00:40:58 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v4/options"
|
2021-08-27 18:55:08 +03:00
|
|
|
)
|
2021-01-10 18:56:39 +03:00
|
|
|
|
2023-10-17 01:00:00 +03:00
|
|
|
type ContextAttrFunc func(ctx context.Context) []interface{}
|
|
|
|
|
|
|
|
var DefaultContextAttrFuncs []ContextAttrFunc
|
|
|
|
|
2020-02-20 23:57:59 -08:00
|
|
|
var (
|
2020-11-02 13:25:29 +03:00
|
|
|
// DefaultLogger variable
|
2023-10-17 01:48:50 +03:00
|
|
|
DefaultLogger = 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
|
|
|
|
DefaultCallerSkipCount = 2
|
2020-02-07 05:35:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Logger is a generic logging interface
|
|
|
|
type Logger interface {
|
2020-02-06 21:36:33 +00:00
|
|
|
// Init initialises options
|
2023-07-29 00:40:58 +03:00
|
|
|
Init(opts ...options.Option) error
|
2021-08-30 16:19:35 +03:00
|
|
|
// Clone create logger copy with new options
|
2023-07-29 00:40:58 +03:00
|
|
|
Clone(opts ...options.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 08:26:12 +00:00
|
|
|
// The Logger options
|
|
|
|
Options() Options
|
2023-10-16 23:43:39 +03:00
|
|
|
// Attrs set attrs to always be logged with keyval pairs
|
|
|
|
Attrs(attrs ...interface{}) Logger
|
2020-11-04 00:38:12 +03:00
|
|
|
// Info level message
|
2023-10-16 23:43:39 +03:00
|
|
|
Info(ctx context.Context, msg string, attrs ...interface{})
|
2020-11-10 22:18:16 +03:00
|
|
|
// Tracef level message
|
2023-10-16 23:43:39 +03:00
|
|
|
Trace(ctx context.Context, msg string, attrs ...interface{})
|
2020-11-10 22:18:16 +03:00
|
|
|
// Debug level message
|
2023-10-16 23:43:39 +03:00
|
|
|
Debug(ctx context.Context, msg string, attrs ...interface{})
|
2020-11-10 22:18:16 +03:00
|
|
|
// Warn level message
|
2023-10-16 23:43:39 +03:00
|
|
|
Warn(ctx context.Context, msg string, attrs ...interface{})
|
2020-11-10 22:18:16 +03:00
|
|
|
// Error level message
|
2023-10-16 23:43:39 +03:00
|
|
|
Error(ctx context.Context, msg string, attrs ...interface{})
|
2020-11-10 22:18:16 +03:00
|
|
|
// Fatal level message
|
2023-10-16 23:43:39 +03:00
|
|
|
Fatal(ctx context.Context, msg string, attrs ...interface{})
|
2021-01-10 18:56:39 +03:00
|
|
|
// Log logs message with needed level
|
2023-10-16 23:43:39 +03:00
|
|
|
Log(ctx context.Context, level Level, msg string, attrs ...interface{})
|
2024-03-04 23:42:15 +03:00
|
|
|
// String returns the type name of logger
|
2020-02-06 21:36:33 +00:00
|
|
|
String() string
|
2024-03-04 23:42:15 +03:00
|
|
|
// String returns the name of logger
|
|
|
|
Name() string
|
2020-02-07 05:35:46 +08:00
|
|
|
}
|
|
|
|
|
2023-10-14 18:59:27 +03:00
|
|
|
// Info writes formatted msg to default logger on info level
|
2023-10-16 23:43:39 +03:00
|
|
|
func Info(ctx context.Context, msg string, attrs ...interface{}) {
|
|
|
|
DefaultLogger.Info(ctx, msg, attrs...)
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
2020-02-07 05:35:46 +08:00
|
|
|
|
2023-10-14 18:59:27 +03:00
|
|
|
// Error writes formatted msg to default logger on error level
|
2023-10-16 23:43:39 +03:00
|
|
|
func Error(ctx context.Context, msg string, attrs ...interface{}) {
|
|
|
|
DefaultLogger.Error(ctx, msg, attrs...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
|
|
|
|
2024-03-25 21:16:24 +03:00
|
|
|
// Debug writes formatted msg to default logger on debug level
|
|
|
|
func Debug(ctx context.Context, msg string, attrs ...interface{}) {
|
2023-10-16 23:43:39 +03:00
|
|
|
DefaultLogger.Debug(ctx, msg, attrs...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
|
|
|
|
2023-10-14 18:59:27 +03:00
|
|
|
// Warn writes formatted msg to default logger on warn level
|
2023-10-16 23:43:39 +03:00
|
|
|
func Warn(ctx context.Context, msg string, attrs ...interface{}) {
|
|
|
|
DefaultLogger.Warn(ctx, msg, attrs...)
|
2020-02-07 05:35:46 +08:00
|
|
|
}
|
|
|
|
|
2023-10-14 18:59:27 +03:00
|
|
|
// Trace writes formatted msg to default logger on trace level
|
2023-10-16 23:43:39 +03:00
|
|
|
func Trace(ctx context.Context, msg string, attrs ...interface{}) {
|
|
|
|
DefaultLogger.Trace(ctx, msg, attrs...)
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
2020-02-07 05:35:46 +08:00
|
|
|
|
2023-10-14 18:59:27 +03:00
|
|
|
// Fatal writes formatted msg to default logger on fatal level
|
2023-10-16 23:43:39 +03:00
|
|
|
func Fatal(ctx context.Context, msg string, attrs ...interface{}) {
|
|
|
|
DefaultLogger.Fatal(ctx, msg, attrs...)
|
2020-02-07 05:35:46 +08:00
|
|
|
}
|
2020-02-15 10:19:28 -08:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// V returns true if passed level enabled in default logger
|
2020-11-04 00:38:12 +03:00
|
|
|
func V(level Level) bool {
|
|
|
|
return DefaultLogger.V(level)
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
// Init initialize default logger
|
2023-07-29 00:40:58 +03:00
|
|
|
func Init(opts ...options.Option) error {
|
2020-11-04 00:38:12 +03:00
|
|
|
return DefaultLogger.Init(opts...)
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
// Attrs create default logger with specific attrs
|
|
|
|
func Attrs(attrs ...interface{}) Logger {
|
|
|
|
return DefaultLogger.Attrs(attrs...)
|
2020-02-15 10:19:28 -08:00
|
|
|
}
|