logging

package
v0.0.0-...-1ff46e3 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Example

This example uses command-line flags to demonstrate various outputs depending on the chosen log level.

package main

import (
	"flag"
	"io"
	"os"
	"time"

	logger "github.com/apollo-command-and-service-module/apollo/pkg/logging"
	"github.com/rs/zerolog"
)

func setup(deBug bool) *logger.Logger {
	var w io.Writer
	w = os.Stdout
	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}

	return logger.New(w, deBug)
}

func main() {
	l := setup(true)
	debug := flag.Bool("debug", false, "sets log level to debug")

	flag.Parse()

	// Default level for this example is info, unless debug flag is present
	zerolog.SetGlobalLevel(zerolog.InfoLevel)
	if *debug {
		zerolog.SetGlobalLevel(zerolog.DebugLevel)
	}

	l.Debug().Msg("This message appears only when log level set to Debug")
	l.Info().Msg("This message appears when log level set to Debug or Info")

	if e := l.Debug(); e.Enabled() {
		// Compute log output only if enabled.
		value := "bar"
		e.Str("foo", value).Msg("some debug message")
	}

}
Output:
{"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckIfError

func CheckIfError(err error)

CheckIfError should be used to naively panics if an error is not nil.

Types

type Logger

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

func New

func New(output io.Writer, isDebug bool) *Logger

New will instantiate a new instance of *Logger. Use this if printing logs to JSON outputs.

func NewConsole

func NewConsole(isDebug bool) *Logger

NewConsole is similar to new but will print to os.Stdout by default with terminal formatting.

func NewLogMulti

func NewLogMulti(isDebug bool) *Logger

func (*Logger) Ctx

func (l *Logger) Ctx(ctx context.Context) *Logger

Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.

func (*Logger) Debug

func (l *Logger) Debug() *zerolog.Event

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Error

func (l *Logger) Error() *zerolog.Event

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Fatal

func (l *Logger) Fatal() *zerolog.Event

Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.

func (*Logger) Hook

func (l *Logger) Hook(h zerolog.Hook) zerolog.Logger

Hook returns a logger with the h Hook.

func (*Logger) Info

func (l *Logger) Info() *zerolog.Event

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Level

func (l *Logger) Level(level zerolog.Level) zerolog.Logger

Level creates a child logger with the minimum accepted level set to level.

func (*Logger) Log

func (l *Logger) Log() *zerolog.Event

Log starts a new message with no level. Setting zerolog.GlobalLevel to zerolog.Disabled will still disable events produced by this method.

You must call Msg on the returned event in order to send the event.

func (*Logger) Output

func (l *Logger) Output(w io.Writer) zerolog.Logger

Output duplicates the global logger and sets w as its output.

func (*Logger) Panic

func (l *Logger) Panic() *zerolog.Event

Panic starts a new message with panic level. The message is also sent to the panic function.

You must call Msg on the returned event in order to send the event.

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Print.

func (*Logger) PrintError

func (l *Logger) PrintError(v ...interface{})

PrintError see comment for PrintInfo

func (*Logger) PrintErrorf

func (l *Logger) PrintErrorf(format string, v ...interface{})

PrintErrorf see comment for PrintInfo

func (*Logger) PrintFatal

func (l *Logger) PrintFatal(v ...interface{})

PrintFatal calls Fatal with os.Exit(1) and prints the error

func (*Logger) PrintFatalf

func (l *Logger) PrintFatalf(format string, v ...interface{})

PrintFatalf calls Fatal with os.Exit(1) and prints the error

func (*Logger) PrintInfo

func (l *Logger) PrintInfo(v ...interface{})

PrintInfo is a shortcut to printing out errors regardless of whether debug level is set to true or false. The usual way with zerolog for command chaining for errors and info is as follows: log.Info().Msg(). Furthermore, the default .Msg() does not take an interface as an argument. So type error messages can be passed to below. So below allows for printing errors without the need to chain if debug is disabled

func (*Logger) PrintInfof

func (l *Logger) PrintInfof(format string, v ...interface{})

PrintInfof see comment for above

func (*Logger) PrintWarn

func (l *Logger) PrintWarn(v ...interface{})

PrintWarn ... see comment for PrintInfo

func (*Logger) PrintWarnF

func (l *Logger) PrintWarnF(format string, v ...interface{})

PrintWarnF see comment for above

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

Printf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Sample

func (l *Logger) Sample(s zerolog.Sampler) zerolog.Logger

Sample returns a logger with the s sampler.

func (*Logger) Warn

func (l *Logger) Warn() *zerolog.Event

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

func (*Logger) With

func (l *Logger) With() zerolog.Context

With creates a child logger with the field added to its context.

func (*Logger) WithLevel

func (l *Logger) WithLevel(level zerolog.Level) *zerolog.Event

WithLevel starts a new message with level.

You must call Msg on the returned event in order to send the event.

Jump to

Keyboard shortcuts

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