log

package
v0.0.0-...-37e343e Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: Apache-2.0 Imports: 14 Imported by: 33

README

log

It is a simple structured logging package for Go.

Features

  • fast, easy to use, and pretty logging for development
  • low to zero allocation
  • JSON encoding format
  • colored text for text handler
  • context.Context integration

Handlers

  • Text (development use)
  • JSON (default, Production)

Installation

Use go get

go get -u github.com/nite-coder/blackbear

Get Started

package main

import (
 "os"

 "github.com/nite-coder/blackbear/pkg/log"
 "github.com/nite-coder/blackbear/pkg/log/handler/text"
)

func main() {
 // json handler
 log.Debug().Msg("Hello World") // output: {"time":"2023-06-23T06:17:43Z","level":"DEBUG","msg":"Hello World"}

 // text handler
 opts := log.HandlerOptions{
  Level:       log.DebugLevel,
  DisableTime: true,
 }
 logger := log.New(text.New(os.Stderr, &opts))
 log.SetDefault(logger)
 log.Debug().Msg("Hello World") // output: 06:17:43.991 DEBUG  Hello World
}
Fields
package main

import (
 "github.com/nite-coder/blackbear/pkg/log"
)

func main() {
    // example1
    logger := log.With().Str("app_id", "blackbear").Logger()
    logger.Debug().Msg("Hello World")

    // example2
    log.Debug().Str("request_id", "abc").Msg("cool")
}
Pass Context
package main

import (
 "github.com/nite-coder/blackbear/pkg/log"
)

func main() {
    ctx := context.Background()
    log.DebugCtx(ctx).Str("request_id", "abc").Msg("cool")
}

insipred by zerolog

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flush

func Flush()

Flush clear all handler's buffer

Types

type Context

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

Context use for meta data

func With

func With() Context

func (Context) Any

func (c Context) Any(key string, val interface{}) Context

Any adds the field key with i marshaled using reflection.

func (Context) Bool

func (c Context) Bool(key string, val bool) Context

Bool add bool field to current context

func (Context) Err

func (c Context) Err(err error) Context

Err add error field to current context

func (Context) Float32

func (c Context) Float32(key string, val float32) Context

Float32 add float32 field to current context

func (Context) Float64

func (c Context) Float64(key string, val float64) Context

Float64 add Float64 field to current context

func (Context) Int

func (c Context) Int(key string, val int) Context

Int add Int field to current context

func (Context) Int16

func (c Context) Int16(key string, val int16) Context

Int16 add Int16 field to current context

func (Context) Int32

func (c Context) Int32(key string, val int32) Context

Int32 add Int32 field to current context

func (Context) Int64

func (c Context) Int64(key string, val int64) Context

Int64 add Int64 field to current context

func (Context) Int8

func (c Context) Int8(key string, val int8) Context

Int8 add Int8 field to current context

func (Context) Ints

func (c Context) Ints(key string, val []int) Context

Ints add Int field to current context

func (Context) Logger

func (c Context) Logger() *Logger

Logger returns the logger with the context previously set.

func (Context) StackTrace

func (c Context) StackTrace() Context

StackTrace adds stack_trace field to the current context

func (Context) Str

func (c Context) Str(key string, val string) Context

Str add string field to current context

func (Context) Strs

func (c Context) Strs(key string, val []string) Context

Strs add string field to current context

func (Context) Time

func (c Context) Time(key string, val time.Time) Context

Time adds Time field to current context

func (Context) Times

func (c Context) Times(key string, val []time.Time) Context

Times adds Time field to current context

func (Context) Uint

func (c Context) Uint(key string, val uint) Context

Uint add Uint field to current context

func (Context) Uint16

func (c Context) Uint16(key string, val uint16) Context

Uint16 add Uint16 field to current context

func (Context) Uint32

func (c Context) Uint32(key string, val uint32) Context

Uint32 add Uint32 field to current context

func (Context) Uint64

func (c Context) Uint64(key string, val uint64) Context

Uint64 add Uint64 field to current context

func (Context) Uint8

func (c Context) Uint8(key string, val uint8) Context

Uint8 add Uint8 field to current context

type Entry

type Entry struct {
	Logger *Logger

	Level   Level  `json:"level"`
	Message string `json:"message"`
	// contains filtered or unexported fields
}

Entry defines a single log entry

func Debug

func Debug() *Entry

Debug logs at DebugLevel

func DebugCtx

func DebugCtx(ctx context.Context) *Entry

DebugCtx logs at LevelDebug with the given context

func Error

func Error() *Entry

Error level formatted message

func ErrorCtx

func ErrorCtx(ctx context.Context) *Entry

ErrorCtx logs at ErrorLevel with the given context.

func Fatal

func Fatal() *Entry

Fatal level formatted message, followed by an exit.

func FatalCtx

func FatalCtx(ctx context.Context) *Entry

FatalCtx logs at FatalLevel with the given context.

func Info

func Info() *Entry

Info logs at InfoLevel

func InfoCtx

func InfoCtx(ctx context.Context) *Entry

InfoCtx logs at InfoLevel with the given context

func Panic

func Panic() *Entry

Panic level formatted message

func PanicCtx

func PanicCtx(ctx context.Context) *Entry

PanicCtx logs at PanicLevel with the given context.

func Warn

func Warn() *Entry

Warn logs at WarnLevel

func WarnCtx

func WarnCtx(ctx context.Context) *Entry

WarnCtx logs at WarnLevel with the given context.

func (*Entry) Any

func (e *Entry) Any(key string, val interface{}) *Entry

Any adds the field key with i marshaled using reflection.

func (*Entry) Bool

func (e *Entry) Bool(key string, val bool) *Entry

Bool add bool field to current entry

func (*Entry) Duration

func (e *Entry) Duration(key string, val time.Duration) *Entry

Duration adds Duration field to current entry

func (*Entry) Err

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

Err add error field to current context

func (*Entry) Fields

func (e *Entry) Fields() []*Field

Fields returns entry's fields

func (*Entry) Float32

func (e *Entry) Float32(key string, val float32) *Entry

Float32 add Float32 field to current entry

func (*Entry) Float64

func (e *Entry) Float64(key string, val float64) *Entry

Float64 adds Float64 field to current entry

func (*Entry) Int

func (e *Entry) Int(key string, val int) *Entry

Int adds Int field to current entry

func (*Entry) Int16

func (e *Entry) Int16(key string, val int16) *Entry

Int16 add Int16 field to current entry

func (*Entry) Int32

func (e *Entry) Int32(key string, val int32) *Entry

Int32 adds Int32 field to current entry

func (*Entry) Int64

func (e *Entry) Int64(key string, val int64) *Entry

Int64 add Int64 field to current entry

func (*Entry) Int8

func (e *Entry) Int8(key string, val int8) *Entry

Int8 add Int8 field to current entry

func (*Entry) Ints

func (e *Entry) Ints(key string, val []int) *Entry

Ints adds Int field to current entry

func (*Entry) Msg

func (e *Entry) Msg(msg string)

Msg print the message.

func (*Entry) Msgf

func (e *Entry) Msgf(msg string, v ...any)

Msgf print the formatted message.

func (*Entry) StackTrace

func (e *Entry) StackTrace() *Entry

StackTrace adds stack_trace field to the current context

func (*Entry) Str

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

Str add string field to current entry

func (*Entry) Strs

func (e *Entry) Strs(key string, val []string) *Entry

Strs add string field to current entry

func (*Entry) Time

func (e *Entry) Time(key string, val time.Time) *Entry

Time adds Time field to current entry

func (*Entry) Times

func (e *Entry) Times(key string, val []time.Time) *Entry

Times adds Time field to current entry

func (*Entry) Uint

func (e *Entry) Uint(key string, val uint) *Entry

Uint add Uint field to current entry

func (*Entry) Uint16

func (e *Entry) Uint16(key string, val uint16) *Entry

Uint16 add Uint16 field to current entry

func (*Entry) Uint32

func (e *Entry) Uint32(key string, val uint32) *Entry

Uint32 add Uint32 field to current entry

func (*Entry) Uint64

func (e *Entry) Uint64(key string, val uint64) *Entry

Uint64 add Uint64 field to current entry

func (*Entry) Uint8

func (e *Entry) Uint8(key string, val uint8) *Entry

Uint8 add Uint8 field to current entry

type Field

type Field struct {
	Key   string
	Value any
}

type Flusher

type Flusher interface {
	Flush() error
}

Flusher is an interface that allow handles have the ability to clear buffer and close connection

type Handler

type Handler interface {
	Enabled(context.Context, Level) bool
	Handle(context.Context, *Entry) error
}

Handler is an interface that log handlers need to be implemented

type HandlerOptions

type HandlerOptions struct {
	Level        Level
	DisableTime  bool
	DisableColor bool
	// ErrorHandler is called whenever handler fails to write an event on its
	// output. If not set, an error is printed on the stderr. This handler must
	// be thread safe and non-blocking.
	ErrorHandler func(err error)
}

type JSONHandler

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

func NewJSONHandler

func NewJSONHandler(w io.Writer, opts *HandlerOptions) *JSONHandler

func (*JSONHandler) Enabled

func (h *JSONHandler) Enabled(_ context.Context, level Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*JSONHandler) Handle

func (h *JSONHandler) Handle(_ context.Context, e *Entry) error

Handle formats its argument Record as a JSON object on a single line.

type Level

type Level uint8

Level of the log

const (
	DebugLevel Level = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	PanicLevel
	FatalLevel
	TraceLevel
)

Log levels.

func NewLevel

func NewLevel(level string) Level

NewLevel returns Level struct

func (Level) String

func (p Level) String() string

String returns the string representation of a logging level.

type Logger

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

func Default

func Default() *Logger

func FromContext

func FromContext(ctx context.Context) *Logger

FromContext return a logger from the standard context

func New

func New(handler Handler) *Logger

func SetDefault

func SetDefault(logger *Logger) *Logger

SetDefault makes l the default Logger.

func (*Logger) Debug

func (l *Logger) Debug() *Entry

Debug logs at DebugLevel

func (*Logger) DebugCtx

func (l *Logger) DebugCtx(ctx context.Context) *Entry

DebugCtx logs at LevelDebug with the given context

func (*Logger) Error

func (l *Logger) Error() *Entry

Error logs at ErrorLevel

func (*Logger) ErrorCtx

func (l *Logger) ErrorCtx(ctx context.Context) *Entry

ErrorCtx logs at ErrorLevel with the given context.

func (*Logger) Fatal

func (l *Logger) Fatal() *Entry

Fatal logs at FatalLevel

func (*Logger) FatalCtx

func (l *Logger) FatalCtx(ctx context.Context) *Entry

FatalCtx logs at FatalLevel with the given context.

func (*Logger) Info

func (l *Logger) Info() *Entry

Info logs at InfoLevel

func (*Logger) InfoCtx

func (l *Logger) InfoCtx(ctx context.Context) *Entry

InfoCtx logs at InfoLevel with the given context

func (*Logger) Panic

func (l *Logger) Panic() *Entry

Panic logs at PanicLevel

func (*Logger) PanicCtx

func (l *Logger) PanicCtx(ctx context.Context) *Entry

PanicCtx logs at PanicLevel with the given context.

func (*Logger) Warn

func (l *Logger) Warn() *Entry

Warn logs at WarnLevel

func (*Logger) WarnCtx

func (l *Logger) WarnCtx(ctx context.Context) *Entry

WarnCtx logs at WarnLevel with the given context.

func (*Logger) With

func (l *Logger) With() Context

func (*Logger) WithContext

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

WithContext return a new context with a log context value

type TextHandler

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

TextHandler is an instance of the text handler

func NewTextHandler

func NewTextHandler(w io.Writer, opts *HandlerOptions) *TextHandler

New create a new Console instance

func (*TextHandler) Enabled

func (h *TextHandler) Enabled(_ context.Context, level Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*TextHandler) Handle

func (h *TextHandler) Handle(_ context.Context, e *Entry) error

Handle formats its argument Record as a text object on a single line.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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