log

package
v3.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 12 Imported by: 4

Documentation

Overview

Package log enhanced zap logger

Index

Examples

Constants

View Source
const (
	// SampleRateDenominator sample rate = sample / SampleRateDenominator
	SampleRateDenominator = 1000
)

Variables

This section is empty.

Functions

func LevelToZap

func LevelToZap(level Level) (zapcore.Level, error)

LevelToZap

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://gq.laisky.com/query/",
	WithAlertType("hello"),
	WithAlertToken("rwkpVuAgaBZQBASKndHK"),
	WithAlertHookLevel(zap.InfoLevel),
)
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 NewAlert

func NewAlert(ctx context.Context,
	pushAPI string,
	opts ...AlertOption,
) (a *Alert, err error)

NewAlert create new Alert

func (*Alert) Close

func (a *Alert) Close()

Close close Alert

func (*Alert) GetZapHook

func (a *Alert) GetZapHook() func(zapcore.Entry, []zapcore.Field) (err error)

GetZapHook get hook for zap logger

func (*Alert) Send

func (a *Alert) Send(msg string) (err error)

Send send with default alertType and pushToken

func (*Alert) SendWithType

func (a *Alert) SendWithType(alertType, pushToken, msg string) (err error)

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 Encoding

type Encoding string

Encoding how to print log

const (
	// EncodingConsole is logger format for console
	EncodingConsole Encoding = "console"
	// EncodingJSON is logger format for json
	EncodingJSON Encoding = "json"
)

func (Encoding) String

func (e Encoding) String() string

String convert encoding to string

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

func LevelFromZap(level zapcore.Level) (Level, error)

LevelFromZap convert from zap level

func (Level) String

func (l Level) String() string

String convert to string

func (Level) Zap

func (l Level) Zap() zapcore.Level

Zap convert to 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

type LoggerT struct {
	*zap.Logger
	// contains filtered or unexported fields
}

LoggerT extend from zap.Logger

var (
	/*Shared logging tool.

	* Info(msg string, fields ...Field)
	* Debug(msg string, fields ...Field)
	* Warn(msg string, fields ...Field)
	* Error(msg string, fields ...Field)
	* Panic(msg string, fields ...Field)
	* DebugSample(sample int, msg string, fields ...zapcore.Field)
	* InfoSample(sample int, msg string, fields ...zapcore.Field)
	* WarnSample(sample int, msg string, fields ...zapcore.Field)
	 */
	Shared *LoggerT
)

func New

func New(optfs ...Option) (l *LoggerT, err error)

New create new logger

func NewConsoleWithName

func NewConsoleWithName(name string, level Level, opts ...zap.Option) (l *LoggerT, err error)

NewConsoleWithName create new logger with name

func NewWithName

func NewWithName(name string, level Level, opts ...zap.Option) (l *LoggerT, err error)

NewWithName create new logger with name

func (*LoggerT) ChangeLevel added in v3.1.0

func (l *LoggerT) ChangeLevel(level Level) (err error)

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

func (l *LoggerT) DebugSample(sample int, msg string, fields ...zapcore.Field)

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

func (l *LoggerT) InfoSample(sample int, msg string, fields ...zapcore.Field)

InfoSample emit info log with propability sample/SampleRateDenominator

func (*LoggerT) Level added in v3.1.0

func (l *LoggerT) Level() Level

Level get current level of logger

func (*LoggerT) Named added in v3.1.0

func (l *LoggerT) Named(s string) *LoggerT

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

func (l *LoggerT) WarnSample(sample int, msg string, fields ...zapcore.Field)

WarnSample emit warn log with propability sample/SampleRateDenominator

func (*LoggerT) With added in v3.1.0

func (l *LoggerT) With(fields ...zapcore.Field) *LoggerT

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

func (l *LoggerT) WithOptions(opts ...zap.Option) *LoggerT

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

func (*LoggerT) Zap added in v3.1.0

func (l *LoggerT) Zap() *zap.Logger

Zap return internal z*ap.Logger

type Option

type Option func(l *option) error

Option logger options

func WithEncoding

func WithEncoding(format Encoding) Option

WithEncoding set logger encoding formet

func WithErrorOutputPaths

func WithErrorOutputPaths(paths []string) Option

WithErrorOutputPaths set error logs output path

like "stderr"

func WithLevel

func WithLevel(level Level) Option

WithLevel set logger level

func WithName

func WithName(name string) Option

WithName set logger name

func WithOutputPaths

func WithOutputPaths(paths []string) Option

WithOutputPaths set output path

like "stdout"

func WithZapOptions

func WithZapOptions(opts ...zap.Option) Option

WithZapOptions set logger with zap.Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL