log

package
v0.0.0-...-edcedff Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package log simply a wrapper of zerolog.Logger.

Example
package main

import (
	"time"

	"github.com/foodarchive/truffls/pkg/log"
	"github.com/rs/zerolog"
)

func setup() {

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC)
	}

	zerolog.CallerMarshalFunc = func(_ string, _ int) string {
		return "/app/controller/root.go:113"
	}
}

func main() {
	setup()
	log.Init(log.Config{})

	log.Info().Msg("hello world")
}
Output:

{"level":"info","time":"2020-07-18T21:01:05Z","message":"hello world"}
Example (StdLog)
package main

import (
	stdLog "log"
	"time"

	"github.com/foodarchive/truffls/pkg/log"
	"github.com/rs/zerolog"
)

func setup() {

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC)
	}

	zerolog.CallerMarshalFunc = func(_ string, _ int) string {
		return "/app/controller/root.go:113"
	}
}

func main() {
	setup()
	log.Init(log.Config{})

	stdLog.Printf("hello from standard log")
}
Output:

{"time":"2020-07-18T21:01:05Z","level":"warn","message":"hello from standard log"}
Example (WithCaller)
package main

import (
	"time"

	"github.com/foodarchive/truffls/pkg/log"
	"github.com/rs/zerolog"
)

func setup() {

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC)
	}

	zerolog.CallerMarshalFunc = func(_ string, _ int) string {
		return "/app/controller/root.go:113"
	}
}

func main() {
	setup()
	log.Init(log.Config{Caller: true})

	log.Error().Msg("something went wrong")
}
Output:

{"level":"error","time":"2020-07-18T21:01:05Z","caller":"/app/controller/root.go:113","message":"something went wrong"}
Example (WithLevel)
package main

import (
	"time"

	"github.com/foodarchive/truffls/pkg/log"
	"github.com/rs/zerolog"
)

func setup() {

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC)
	}

	zerolog.CallerMarshalFunc = func(_ string, _ int) string {
		return "/app/controller/root.go:113"
	}
}

func main() {
	setup()
	log.Init(log.Config{Level: "panic"})

	log.Fatal().Msg("debugging message")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(config Config)

Init initialize logger based on Config.

Types

type Config

type Config struct {
	// Level log level config.
	//
	// Supported values: `trace`, `debug`, `info`, `warn`, `fatal`, `panic`.
	// Default value "info".
	Level string

	// Output log output.
	//
	// Supported values: `console`, `stderr`, `stdout`.
	// For `console` print log with pretty output,
	// `stderr` and `stdout` print log with JSON format.
	Out string

	// StackTrace set this to true to enable stack stacktrace.
	StackTrace bool

	// Caller if enabled, print log caller filename and line number.
	Caller bool
}

Config logging configuration.

type Event

type Event = zerolog.Event

Event zerolog.Event alias.

func Debug

func Debug() *Event

Debug starts logging with debug level.

Example
package main

import (
	"time"

	"github.com/foodarchive/truffls/pkg/log"
	"github.com/rs/zerolog"
)

func setup() {

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC)
	}

	zerolog.CallerMarshalFunc = func(_ string, _ int) string {
		return "/app/controller/root.go:113"
	}
}

func main() {
	setup()
	log.Init(log.Config{Level: "debug"})

	log.Debug().Msg("debugging message")
}
Output:

{"level":"debug","time":"2020-07-18T21:01:05Z","message":"debugging message"}

func Error

func Error() *Event

Error starts logging with error level.

func Fatal

func Fatal() *Event

Fatal starts logging with fatal level. Note: under the hood it will call os.Exit(1).

func Info

func Info() *Event

Info starts logging with info level.

type Hook

type Hook = zerolog.Hook

Hook zerolog.Hook alias.

type Level

type Level = zerolog.Level

Level zerolog.Level alias.

type Logger

type Logger = zerolog.Logger

Logger zerolog.Logger alias.

var (
	// Log instance of zerolog.Logger.
	Log Logger
)

func WithHook

func WithHook(h Hook) Logger

WithHook returns a logger with the h Hook.

Example
package main

import (
	"time"

	"github.com/foodarchive/truffls/pkg/log"
	"github.com/rs/zerolog"
)

type copyHook struct {
}

func (h copyHook) Run(e *log.Event, level log.Level, m string) {
	e.Str("copy", m)
}

func setup() {

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC)
	}

	zerolog.CallerMarshalFunc = func(_ string, _ int) string {
		return "/app/controller/root.go:113"
	}
}

func main() {
	setup()
	log.Init(log.Config{})

	lw := log.WithHook(copyHook{})
	lw.Log().Msg("message")
}
Output:

{"time":"2020-07-18T21:01:05Z","copy":"message","message":"message"}

type NoLevelDebugHook

type NoLevelDebugHook struct{}

NoLevelDebugHook replace no level with warn log level.

func (NoLevelDebugHook) Run

func (h NoLevelDebugHook) Run(e *Event, level Level, _ string)

Run add debug level to the log message when log level not provided.

Example
setup()
log.Init(log.Config{})

hook := log.Log.Hook(log.NoLevelDebugHook{})
stdLog.SetFlags(0)
stdLog.SetOutput(hook)

stdLog.Print("debugging logs...")
Output:

{"time":"2020-07-18T21:01:05Z","level":"debug","message":"debugging logs..."}

type NoLevelErrorHook

type NoLevelErrorHook struct{}

NoLevelErrorHook replace no level with warn log level.

func (NoLevelErrorHook) Run

func (h NoLevelErrorHook) Run(e *Event, level Level, _ string)

Run add error level to the log message when log level not provided.

Example
setup()
log.Init(log.Config{})

hook := log.Log.Hook(log.NoLevelErrorHook{})
stdLog.SetFlags(0)
stdLog.SetOutput(hook)

stdLog.Print("error logs...")
Output:

{"time":"2020-07-18T21:01:05Z","level":"error","message":"error logs..."}

type NoLevelWarnHook

type NoLevelWarnHook struct{}

NoLevelWarnHook replace no level with warn log level.

func (NoLevelWarnHook) Run

func (h NoLevelWarnHook) Run(e *Event, level Level, _ string)

Run add warn level to the log message when log level not provided.

Jump to

Keyboard shortcuts

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