Help us shape the future of Go! Take the 2026 Go Developer Survey

log

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2020 License: MIT Imports: 15 Imported by: 343

README

Structured Logging for Humans

godoc license goreport gocover

Features

  • Simple & Straightforward interfaces
  • Effective, Outperforms zerolog and zap
  • Rotating/Pretty/Buffering Writers
  • No external dependences

Getting Started

Simple Logging Example
package main

import (
	"github.com/phuslu/log"
)

func main() {
	log.Debug().Str("foo", "bar").Msg("hello world")
}

// Output: {"time":"2019-07-10T16:00:19.092Z","level":"debug","foo":"bar","message":"hello world"}

Note: By default log writes to os.Stderr

Pretty logging

To log a human-friendly, colorized output, use log.ConsoleWriter:

package main

import (
	"os"
	"github.com/phuslu/log"
)

func main() {
	if log.IsTerminal(os.Stderr.Fd()) {
		log.DefaultLogger = log.Logger{
			Caller: 1,
			Writer: &log.ConsoleWriter{ANSIColor: true},
		}
	}

	log.Info().Str("foo", "bar").Msg("hello world")
}

// Output: 2019-07-11T16:41:43.256Z INF 12 pretty.go:10 > hello world foo=bar

Note: pretty logging also works on windows console

Customize the configuration and formatting:
log.DefaultLogger = log.Logger{
	Level:      log.InfoLevel,
	Caller:     1,
	TimeField:  "date",
	TimeFormat: "2006-01-02",
	Writer:     &log.Writer{},
}
log.Info().Msg("hello world")

// Output: {"date":"2019-07-04","level":"info","caller":"test.go:42","message":"hello world"}
Multi Writers:
log.DefaultLogger.Writer = io.MultiWriter(&log.Writer{Filename: "1.log"}, &log.ConsoleWriter{ANSIColor: true})
log.Info().Msg("hello world")
Rotating log files hourly
package main

import (
	"time"

	"github.com/phuslu/log"
	"github.com/robfig/cron"
)

func main() {
	logger := log.Logger{
		Level:      log.ParseLevel("info"),
		Writer:     &log.Writer{
			Filename:   "main.log",
			MaxSize:    50*1024*1024,
			MaxBackups: 7,
			LocalTime:  false,
		},
	}

	runner := cron.New(cron.WithSeconds(), cron.WithLocation(time.UTC), cron.WithLogger(cron.PrintfLogger(log.DefaultLogger)))
	runner.AddFunc("0 0 * * * *", func() { logger.Writer.(*log.Writer).Rotate() })
	go runner.Run()

	for {
		time.Sleep(time.Second)
		logger.Info().Msg("hello world")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogger = Logger{
	Level:      DebugLevel,
	Caller:     0,
	TimeField:  "",
	TimeFormat: "",
	HostField:  "",
	Writer:     &Writer{},
}

Functions

func Fastrandn

func Fastrandn(x uint32) uint32

func IsTerminal

func IsTerminal(fd uintptr) bool

func Print

func Print(v ...interface{})

func Printf

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

Types

type ConsoleWriter

type ConsoleWriter struct {
	ANSIColor bool
}

func (*ConsoleWriter) Write

func (w *ConsoleWriter) Write(p []byte) (int, error)

type Event

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

func Debug

func Debug() (e *Event)

func Error

func Error() (e *Event)

func Fatal

func Fatal() (e *Event)

func Info

func Info() (e *Event)

func Warn

func Warn() (e *Event)

func (*Event) Bool

func (e *Event) Bool(key string, b bool) *Event

func (*Event) Bools

func (e *Event) Bools(key string, b []bool) *Event

func (*Event) Bytes

func (e *Event) Bytes(key string, val []byte) *Event

func (*Event) Caller

func (e *Event) Caller() *Event

func (*Event) Discard

func (e *Event) Discard() *Event

func (*Event) Dur

func (e *Event) Dur(key string, d time.Duration) *Event

func (*Event) Durs

func (e *Event) Durs(key string, d []time.Duration) *Event

func (*Event) Enabled

func (e *Event) Enabled() bool

func (*Event) Err

func (e *Event) Err(err error) *Event

func (*Event) Errs

func (e *Event) Errs(key string, errs []error) *Event

func (*Event) Float32

func (e *Event) Float32(key string, f float32) *Event

func (*Event) Float64

func (e *Event) Float64(key string, f float64) *Event

func (*Event) Floats32

func (e *Event) Floats32(key string, f []float32) *Event

func (*Event) Floats64

func (e *Event) Floats64(key string, f []float64) *Event

func (*Event) Hex

func (e *Event) Hex(key string, val []byte) *Event

func (*Event) Int

func (e *Event) Int(key string, i int) *Event

func (*Event) Int8

func (e *Event) Int8(key string, i int8) *Event

func (*Event) Int16

func (e *Event) Int16(key string, i int16) *Event

func (*Event) Int32

func (e *Event) Int32(key string, i int32) *Event

func (*Event) Int64

func (e *Event) Int64(key string, i int64) *Event

func (*Event) Interface

func (e *Event) Interface(key string, i interface{}) *Event

func (*Event) Msg

func (e *Event) Msg(msg string)

func (*Event) Msgf

func (e *Event) Msgf(format string, v ...interface{})

func (*Event) RawJSON

func (e *Event) RawJSON(key string, b []byte) *Event

func (*Event) Str

func (e *Event) Str(key string, val string) *Event

func (*Event) Strs

func (e *Event) Strs(key string, vals []string) *Event

func (*Event) Time

func (e *Event) Time(key string, t time.Time) *Event

func (*Event) Timestamp

func (e *Event) Timestamp() *Event

func (*Event) Uint8

func (e *Event) Uint8(key string, i uint8) *Event

func (*Event) Uint16

func (e *Event) Uint16(key string, i uint16) *Event

func (*Event) Uint32

func (e *Event) Uint32(key string, i uint32) *Event

func (*Event) Uint64

func (e *Event) Uint64(key string, i uint64) *Event

type GrpcLogger

type GrpcLogger struct {
	Logger Logger
}

func (GrpcLogger) Error

func (l GrpcLogger) Error(args ...interface{})

func (GrpcLogger) Errorf

func (l GrpcLogger) Errorf(format string, args ...interface{})

func (GrpcLogger) Errorln

func (l GrpcLogger) Errorln(args ...interface{})

func (GrpcLogger) Fatal

func (l GrpcLogger) Fatal(args ...interface{})

func (GrpcLogger) Fatalf

func (l GrpcLogger) Fatalf(format string, args ...interface{})

func (GrpcLogger) Fatalln

func (l GrpcLogger) Fatalln(args ...interface{})

func (GrpcLogger) Info

func (l GrpcLogger) Info(args ...interface{})

func (GrpcLogger) Infof

func (l GrpcLogger) Infof(format string, args ...interface{})

func (GrpcLogger) Infoln

func (l GrpcLogger) Infoln(args ...interface{})

func (GrpcLogger) V

func (l GrpcLogger) V(level int) bool

func (GrpcLogger) Warning

func (l GrpcLogger) Warning(args ...interface{})

func (GrpcLogger) Warningf

func (l GrpcLogger) Warningf(format string, args ...interface{})

func (GrpcLogger) Warningln

func (l GrpcLogger) Warningln(args ...interface{})

type Level

type Level uint32
const (
	// DebugLevel defines debug log level.
	DebugLevel Level = iota
	// InfoLevel defines info log level.
	InfoLevel
	// WarnLevel defines warn log level.
	WarnLevel
	// ErrorLevel defines error log level.
	ErrorLevel
	// FatalLevel defines fatal log level.
	FatalLevel
	// PanicLevel defines panic log level.
	PanicLevel
	// NoLevel defines an absent log level.
	NoLevel
	// Disabled disables the logger.
	Disabled
)

func ParseLevel

func ParseLevel(s string) (level Level)

type LevelWriter

type LevelWriter struct {
	Logger Logger
	Level  Level
}

func (LevelWriter) Write

func (w LevelWriter) Write(p []byte) (int, error)

type Logger

type Logger struct {
	Level      Level
	Caller     int
	TimeField  string
	TimeFormat string
	HostField  string
	Writer     io.Writer
}

func (Logger) Debug

func (l Logger) Debug() (e *Event)

func (Logger) Error

func (l Logger) Error() (e *Event)

func (Logger) Fatal

func (l Logger) Fatal() (e *Event)

func (Logger) Info

func (l Logger) Info() (e *Event)

func (Logger) Print

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

func (Logger) Printf

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

func (Logger) Warn

func (l Logger) Warn() (e *Event)

func (Logger) WithLevel

func (l Logger) WithLevel(level Level) (e *Event)

type TSVEvent

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

func (*TSVEvent) Bool

func (e *TSVEvent) Bool(b bool) *TSVEvent

func (*TSVEvent) Bytes

func (e *TSVEvent) Bytes(val []byte) *TSVEvent

func (*TSVEvent) Float32

func (e *TSVEvent) Float32(f float32) *TSVEvent

func (*TSVEvent) Float64

func (e *TSVEvent) Float64(f float64) *TSVEvent

func (*TSVEvent) Int

func (e *TSVEvent) Int(i int) *TSVEvent

func (*TSVEvent) Int8

func (e *TSVEvent) Int8(i int8) *TSVEvent

func (*TSVEvent) Int16

func (e *TSVEvent) Int16(i int16) *TSVEvent

func (*TSVEvent) Int32

func (e *TSVEvent) Int32(i int32) *TSVEvent

func (*TSVEvent) Int64

func (e *TSVEvent) Int64(i int64) *TSVEvent

func (*TSVEvent) Msg

func (e *TSVEvent) Msg()

func (*TSVEvent) Str

func (e *TSVEvent) Str(val string) *TSVEvent

func (*TSVEvent) Timestamp

func (e *TSVEvent) Timestamp() *TSVEvent

func (*TSVEvent) Uint8

func (e *TSVEvent) Uint8(i uint8) *TSVEvent

func (*TSVEvent) Uint16

func (e *TSVEvent) Uint16(i uint16) *TSVEvent

func (*TSVEvent) Uint32

func (e *TSVEvent) Uint32(i uint32) *TSVEvent

func (*TSVEvent) Uint64

func (e *TSVEvent) Uint64(i uint64) *TSVEvent

type TSVLogger

type TSVLogger struct {
	Separator byte
	Escape    bool
	Writer    io.Writer
}

func (TSVLogger) New

func (l TSVLogger) New() (e *TSVEvent)

type Writer

type Writer struct {
	Filename   string
	MaxSize    int64
	MaxBackups int
	LocalTime  bool
	HostName   bool
	// contains filtered or unexported fields
}

func (*Writer) Close

func (w *Writer) Close() (err error)

func (*Writer) Rotate

func (w *Writer) Rotate() (err error)

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Directories

Path Synopsis
fiber module
logr module
otel module

Jump to

Keyboard shortcuts

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