zslog

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

zslog

Actions Status Go Report Card GitHub tag (latest SemVer) GitHub go.mod Go version Codecov GitHub

Simply configuring zerolog with output to console, lumberjack.v2, sentry-go, http.

Features

Example

package main

import (
    "github.com/mef13/zslog"
    "time"
)

func main() {
    fileConf := zslog.FileConfig{
        MaxSize:    1,
        MaxBackups: 10,
    }
    zslog.InitLogger(
        zslog.StdOut(zslog.NewLevels().SetMaxLevel(zslog.WarnLevel)),
        zslog.StdErr(zslog.NewLevels().SetMinLevel(zslog.ErrorLevel)),
        zslog.File("/var/log/myapp/err.log", fileConf, zslog.NewLevels().SetMinLevel(zslog.ErrorLevel)),
        zslog.Sentry(zslog.SentryConfig{
            Dsn:              "https://public@sentry.example.com/1",
            Release:          "0.0",
        }, 3*time.Second, zslog.NewLevels().SetMinLevel(zslog.ErrorLevel)))

    //Flush sentry buffer before exit
    defer zslog.Close()

    zslog.Info().Msg("hello world")
}
Set log level for output

Set specific levels:

zslog.StdOut(zslog.NewLevels(zslog.ErrorLevel, zslog.WarnLevel))

or set maximum(minimum) level:

zslog.StdOut(zslog.NewLevels().SetMaxLevel(zslog.WarnLevel))
Error Logging with Stacktrace

For add stacktrace use Stack() function:

package main

import (
	"github.com/mef13/zslog"
	"github.com/pkg/errors"
	"time"
)

func main() {
	zslog.InitLogger(
		zslog.Sentry(zslog.SentryConfig{
			Dsn:     "https://public@sentry.example.com/1",
			Release: "0.0",
		}, 3*time.Second, zslog.NewLevels().SetMinLevel(zslog.ErrorLevel)))

	//Flush sentry buffer before exit
	defer zslog.Close()

	err = outer()
	zslog.Error().Stack().Err(err).Msg("outer error")
}

func inner() error {
	return errors.New("Ah s*, here we go again")
}

func middle() error {
	err := inner()
	if err != nil {
		return err
	}
	return nil
}

func outer() error {
	err := middle()
	if err != nil {
		return err
	}
	return nil
}

Documentation

Index

Constants

View Source
const (
	// DebugLevel defines debug log level.
	DebugLevel = zerolog.DebugLevel
	// InfoLevel defines info log level.
	InfoLevel = zerolog.InfoLevel
	// WarnLevel defines warn log level.
	WarnLevel = zerolog.WarnLevel
	// ErrorLevel defines error log level.
	ErrorLevel = zerolog.ErrorLevel
	// FatalLevel defines fatal log level.
	FatalLevel = zerolog.FatalLevel
	// PanicLevel defines panic log level.
	PanicLevel = zerolog.PanicLevel
	// NoLevel defines an absent log level.
	NoLevel = zerolog.NoLevel
	// Disabled disables the logger.
	Disabled = zerolog.Disabled
	// TraceLevel defines trace log level.
	TraceLevel = zerolog.TraceLevel
)

Variables

This section is empty.

Functions

func Close

func Close()

func Debug

func Debug() *zerolog.Event

func Error

func Error() *zerolog.Event

func Fatal

func Fatal() *zerolog.Event

func File

func File(filename string, c FileConfig, l levels) zerolog.LevelWriter

func GetHttpWriter added in v0.2.0

func GetHttpWriter(method string, endpoint string, l levels) zerolog.LevelWriter

func GetLogger

func GetLogger() zerolog.Logger

func Info

func Info() *zerolog.Event

func InitLogger

func InitLogger(writers ...zerolog.LevelWriter) error

func New added in v0.2.0

func New(writers ...zerolog.LevelWriter) (zlog, error)

func NewLevels

func NewLevels(lvls ...zerolog.Level) levels

func Panic

func Panic() *zerolog.Event

func PrintWriterError added in v0.2.0

func PrintWriterError(format string, a ...interface{})

func Sentry

func Sentry(sentryConf SentryConfig, flushTimeout time.Duration, lvls levels) zerolog.LevelWriter

Sentry creates and return zerolog.LevelWriter interface. Configured by SentryConfig(heir sentry.ClientOptions).

func SentryFromClient added in v0.1.6

func SentryFromClient(sentryClient *sentry.Client, flushTimeout time.Duration, lvls levels) zerolog.LevelWriter

func SentryWithHub added in v0.1.5

func SentryWithHub(sentryConf SentryConfig, flushTimeout time.Duration, lvls levels) zerolog.LevelWriter

SentryWithHub creates and return zerolog.LevelWriter interface. Unlike Sentry() it also adds a sentry.Client to the sentry.Hub(like sentry.Init).

func SetTimeFieldFormat added in v0.2.0

func SetTimeFieldFormat(format string)

func SkipSentry added in v0.1.7

func SkipSentry() *zerolog.Logger

func StdErr

func StdErr(l levels) zerolog.LevelWriter

func StdOut

func StdOut(l levels) zerolog.LevelWriter

func Trace

func Trace() *zerolog.Event

func Warn

func Warn() *zerolog.Event

Types

type FileConfig

type FileConfig struct {
	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int

	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	LocalTime bool

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool
}

type HttpWriter added in v0.2.0

type HttpWriter struct {
	// contains filtered or unexported fields
}

func (*HttpWriter) Write added in v0.2.0

func (hw *HttpWriter) Write(p []byte) (n int, err error)

func (*HttpWriter) WriteLevel added in v0.2.0

func (hw *HttpWriter) WriteLevel(level zerolog.Level, p []byte) (n int, err error)

type LevelWriter

type LevelWriter struct {
	// contains filtered or unexported fields
}

func (*LevelWriter) Close added in v0.2.0

func (lw *LevelWriter) Close() error

func (*LevelWriter) Write

func (lw *LevelWriter) Write(p []byte) (n int, err error)

func (*LevelWriter) WriteLevel

func (lw *LevelWriter) WriteLevel(level zerolog.Level, p []byte) (n int, err error)

type SentryConfig

type SentryConfig sentry.ClientOptions

type SentryWriter

type SentryWriter struct {
	// contains filtered or unexported fields
}

func (*SentryWriter) Close

func (sw *SentryWriter) Close() error

func (*SentryWriter) Write

func (sw *SentryWriter) Write(p []byte) (n int, err error)

func (*SentryWriter) WriteLevel

func (sw *SentryWriter) WriteLevel(level zerolog.Level, p []byte) (n int, err error)

type WriterError added in v0.2.0

type WriterError interface {
	Print(format string, a ...interface{})
}
var (
	ErrInitWriter              = errors.New("One or more LevelWriters is nil(error).")
	WriterErrorLog WriterError = defaultWriterError{}
)

Jump to

Keyboard shortcuts

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