Documentation ¶
Overview ¶
Package log enhanced zap logger
Index ¶
- Constants
- func LevelToZap(level Level) (zapcore.Level, error)
- type Alert
- type AlertOption
- type Encoding
- type Level
- type Logger
- type LoggerT
- func (l *LoggerT) ChangeLevel(level Level) (err error)
- func (l *LoggerT) DebugSample(sample int, msg string, fields ...zapcore.Field)
- func (l *LoggerT) InfoSample(sample int, msg string, fields ...zapcore.Field)
- func (l *LoggerT) Level() Level
- func (l *LoggerT) Named(s string) *LoggerT
- func (l *LoggerT) WarnSample(sample int, msg string, fields ...zapcore.Field)
- func (l *LoggerT) With(fields ...zapcore.Field) *LoggerT
- func (l *LoggerT) WithOptions(opts ...zap.Option) *LoggerT
- func (l *LoggerT) Zap() *zap.Logger
- type Option
Examples ¶
Constants ¶
const (
// SampleRateDenominator sample rate = sample / SampleRateDenominator
SampleRateDenominator = 1000
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Alert ¶
type Alert struct {
// contains filtered or unexported fields
}
Alert send alert to laisky's alert API
https://github.com/Laisky/laisky-blog-graphql/tree/master/telegram
Example ¶
pusher, err := NewAlert( context.Background(), "https://blog.laisky.com/graphql/query/", WithAlertType("hello"), WithAlertToken("rwkpVuAgaBZQBASKndHK"), ) if err != nil { Shared.Panic("create alert pusher", zap.Error(err)) } defer pusher.Close() logger := Shared.WithOptions( zap.Fields(zap.String("logger", "test")), zap.HooksWithFields(pusher.GetZapHook()), ) logger.Debug("DEBUG", zap.String("yo", "hello")) logger.Info("Info", zap.String("yo", "hello")) logger.Warn("Warn", zap.String("yo", "hello")) logger.Error("Error", zap.String("yo", "hello")) time.Sleep(1 * time.Second)
Output:
func (*Alert) GetZapHook ¶
GetZapHook get hook for zap logger
func (*Alert) SendWithType ¶
SendWithType send alert with specific type, token and msg
type AlertOption ¶
type AlertOption func(*alertOption) error
AlertOption option for create AlertHook
func WithAlertHookLevel ¶
func WithAlertHookLevel(level zapcore.Level) AlertOption
WithAlertHookLevel level to trigger AlertHook
func WithAlertPushTimeout ¶
func WithAlertPushTimeout(timeout time.Duration) AlertOption
WithAlertPushTimeout set Alert HTTP timeout
func WithAlertToken ¶
func WithAlertToken(token string) AlertOption
WithAlertToken set token for alert hooker
func WithAlertType ¶
func WithAlertType(alertType string) AlertOption
WithAlertType set type for alert hooker
type Level ¶
type Level string
Level logger level
- LevelInfo
- LevelDebug
- LevelWarn
- LevelError
- LevelFatal
- LevelPanic
const ( // LevelUnspecified unknown level LevelUnspecified Level = "unspecified" // LevelInfo Logger level info LevelInfo Level = "info" // LevelDebug Logger level debug LevelDebug Level = "debug" // LevelWarn Logger level warn LevelWarn Level = "warn" // LevelError Logger level error LevelError Level = "error" // LevelFatal Logger level fatal LevelFatal Level = "fatal" // LevelPanic Logger level panic LevelPanic Level = "panic" )
func LevelFromZap ¶
LevelFromZap convert from zap level
type Logger ¶
type Logger interface { // Level get current level Level() Level // ChangeLevel change log and all its children's level ChangeLevel(level Level) (err error) // DebugSample debug with sample/1000 DebugSample(sample int, msg string, fields ...zapcore.Field) // InfoSample info with sample/1000 InfoSample(sample int, msg string, fields ...zapcore.Field) // WarnSample warn with sample/1000 WarnSample(sample int, msg string, fields ...zapcore.Field) // Named create named child logger Named(childName string) *LoggerT // With with fields With(fields ...zapcore.Field) *LoggerT // WithOptions with options WithOptions(opts ...zap.Option) *LoggerT // contains filtered or unexported methods }
Logger logger interface
type LoggerT ¶ added in v3.1.0
LoggerT extend from zap.Logger
var ( LoggerT )Shared *
func NewConsoleWithName ¶
NewConsoleWithName create new logger with name
func NewWithName ¶
NewWithName create new logger with name
func (*LoggerT) ChangeLevel ¶ added in v3.1.0
ChangeLevel change logger level
Because all children loggers share the same level as their parent logger, if you modify one logger's level, it will affect all of its parent and children loggers.
func (*LoggerT) DebugSample ¶ added in v3.1.0
DebugSample emit debug log with propability sample/SampleRateDenominator. sample could be [0, 1000], less than 0 means never, great than 1000 means certainly
func (*LoggerT) InfoSample ¶ added in v3.1.0
InfoSample emit info log with propability sample/SampleRateDenominator
func (*LoggerT) Named ¶ added in v3.1.0
Named adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed.
func (*LoggerT) WarnSample ¶ added in v3.1.0
WarnSample emit warn log with propability sample/SampleRateDenominator
func (*LoggerT) With ¶ added in v3.1.0
With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa.
func (*LoggerT) WithOptions ¶ added in v3.1.0
WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.
type Option ¶
type Option func(l *option) error
Option logger options
func WithEncoding ¶
WithEncoding set logger encoding formet
func WithZapOptions ¶
WithZapOptions set logger with zap.Option