log

package
v0.0.0-...-99e36ab Latest Latest
Warning

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

Go to latest
Published: May 14, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package log provides a structured context logger.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddGlobalFields

func AddGlobalFields(field string)

func ClearGlobalFields

func ClearGlobalFields()

func Debug

func Debug() *zerolog.Event
Example
package main

import (
	"os"
	"time"

	"github.com/Iridaceae/iridaceae/pkg/log"

	"github.com/rs/zerolog"
)

var hookFunc = log.MapperHook{}

func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 4, 20, 4, 20, 0o4, 0, time.UTC)
	}
	zlog := zerolog.New(os.Stdout).With().Timestamp().Logger().Hook(hookFunc)
	log.L = log.NewZ(zlog)
}

func main() {
	setup()
	log.Debug().Msg("hello world")
}
Output:

{"level":"DEBUG","time":1587356404,"message":"hello world"}

func Error

func Error(err error) *zerolog.Event

func Fatal

func Fatal(err error) *zerolog.Event

func GetGlobalFields

func GetGlobalFields() []string

func Goid

func Goid() uint64

Goid returns a goroutine id of given stack trace. As the word of the wise Dave Cheney you might as well go to hell by using this. This is a hacky pure Go version of dave cheney's implementation.

func Info

func Info() *zerolog.Event

func Log

func Log() *zerolog.Event
Example
package main

import (
	"os"
	"time"

	"github.com/Iridaceae/iridaceae/pkg/log"

	"github.com/rs/zerolog"
)

var hookFunc = log.MapperHook{}

func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 4, 20, 4, 20, 0o4, 0, time.UTC)
	}
	zlog := zerolog.New(os.Stdout).With().Timestamp().Logger().Hook(hookFunc)
	log.L = log.NewZ(zlog)
}

func main() {
	setup()
	log.Log().Msg("hello world")
}
Output:

{"time":1587356404,"message":"hello world"}

func Panic

func Panic() *zerolog.Event

func Print

func Print(args ...interface{})
Example
package main

import (
	"os"
	"time"

	"github.com/Iridaceae/iridaceae/pkg/log"

	"github.com/rs/zerolog"
)

var hookFunc = log.MapperHook{}

func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 4, 20, 4, 20, 0o4, 0, time.UTC)
	}
	zlog := zerolog.New(os.Stdout).With().Timestamp().Logger().Hook(hookFunc)
	log.L = log.NewZ(zlog)
}

func main() {
	setup()
	log.Print("hello world")
}
Output:

{"level":"DEBUG","time":1587356404,"message":"hello world"}

func Printf

func Printf(format string, args ...interface{})
Example
package main

import (
	"os"
	"time"

	"github.com/Iridaceae/iridaceae/pkg/log"

	"github.com/rs/zerolog"
)

var hookFunc = log.MapperHook{}

func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 4, 20, 4, 20, 0o4, 0, time.UTC)
	}
	zlog := zerolog.New(os.Stdout).With().Timestamp().Logger().Hook(hookFunc)
	log.L = log.NewZ(zlog)
}

func main() {
	setup()
	log.Printf("hello %s", "world")
}
Output:

{"level":"DEBUG","time":1587356404,"message":"hello world"}

func ResetGlobalStorage

func ResetGlobalStorage()

ResetGlobalStorage sets an empty default map.

func ScCallerEncoder

func ScCallerEncoder() func(file string, line int) string

func ScLevelEncoder

func ScLevelEncoder() func(l zerolog.Level) string

func SetGlobalFields

func SetGlobalFields(fields []string)

func Trace

func Trace() *zerolog.Event
Example
package main

import (
	"os"
	"time"

	"github.com/Iridaceae/iridaceae/pkg/log"

	"github.com/rs/zerolog"
)

var hookFunc = log.MapperHook{}

func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 4, 20, 4, 20, 0o4, 0, time.UTC)
	}
	zlog := zerolog.New(os.Stdout).With().Timestamp().Logger().Hook(hookFunc)
	log.L = log.NewZ(zlog)
}

func main() {
	setup()
	log.Trace().Msg("hello world")
}
Output:

{"level":"TRACE","time":1587356404,"message":"hello world"}

func TrimmedPath

func TrimmedPath(file string) string

func Warn

func Warn() *zerolog.Event

func Z

func Z() *zerolog.Logger

Z returns internal zerolog.Logger of our global logger.

Types

type Logger

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

Logger defines a default logger for that wraps around rs/zerolog.

var L *Logger

func New

func New() *Logger

New returns a default Logger wrapped around zerolog.

Example
package main

import (
	"os"
	"time"

	"github.com/Iridaceae/iridaceae/pkg/log"

	"github.com/rs/zerolog"
)

var hookFunc = log.MapperHook{}

func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2020, 4, 20, 4, 20, 0o4, 0, time.UTC)
	}
	zlog := zerolog.New(os.Stdout).With().Timestamp().Logger().Hook(hookFunc)
	log.L = log.NewZ(zlog)
}

func main() {
	log.New()
	setup()
	log.Info().Msg("hello world")
}
Output:

{"level":"INFO","time":1587356404,"message":"hello world"}

func NewZ

func NewZ(l zerolog.Logger) *Logger

NewZ creates a Logger from user-defined zerolog.

type MapperHook

type MapperHook struct{}

func (MapperHook) Run

func (m MapperHook) Run(e *zerolog.Event, level zerolog.Level, message string)

type Storage

type Storage struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Storage defines our key, value mapping for our context. Storage is inherently safe for concurrency and goroutines.

func InitGlobalStorage

func InitGlobalStorage() *Storage

InitGlobalStorage initializes a Storage objects with a key,value map.

func Mapper

func Mapper() *Storage

Mapper initialize a global mapper for our logger.

func (*Storage) Count

func (s *Storage) Count() int

Count returns # of k,v in our mapper mapping.

func (*Storage) Get

func (s *Storage) Get(key string) (interface{}, bool)

Get returns a given value in our mapper context key,value pair.

func (*Storage) GetString

func (s *Storage) GetString(key string) string

GetString returns a string representation of value.

func (*Storage) Has

func (s *Storage) Has(key string) bool

Has check if key is already exists in map.

func (*Storage) IsEmpty

func (s *Storage) IsEmpty() bool

IsEmpty checks if map is empty.

func (*Storage) Keys

func (s *Storage) Keys() []string

Keys returns a list of string.

func (*Storage) Remove

func (s *Storage) Remove(key string)

Remove a given key from our map.

func (*Storage) Set

func (s *Storage) Set(key string, value interface{})

Set adds key-value pair to Storage map.

func (*Storage) SetAbsent

func (s *Storage) SetAbsent(key string, value interface{}) bool

SetAbsent returns false if value is already exists in given map, true otherwise and insert into give map. This is safe concurrently.

func (*Storage) SetMap

func (s *Storage) SetMap(mp map[string]interface{})

SetMap adds given map to mapper's items.

Jump to

Keyboard shortcuts

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