log

package module
v0.0.0-...-3baba84 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 6 Imported by: 0

README

Another Go logging library

Usage

Simple

You can use default console handler and default log file handler

package main

import "github.com/a-was/go-log"

func main() {
	log.UseDefaultConsoleHandler()
	log.UseDefaultFileHandler("app.log")

	log.Debug("Some debug message", 123)
	log.Debugf("Some debug message %d", 123)
	log.Debugw("Some debug message", log.V{"value": 123})

	log.Info("Some info message", 123)
	log.Infof("Some info message %d", 123)
	log.Infow("Some info message", log.V{"value": 123})

	log.Warn("Some warn message", 123)
	log.Warnf("Some warn message %d", 123)
	log.Warnw("Some warn message", log.V{"value": 123})

	log.Error("Some error message", 123)
	log.Errorf("Some error message %d", 123)
	log.Errorw("Some error message", log.V{"value": 123})

	// this only panics when in Development mode
	log.DevelopmentMode()
	log.DPanic("Some development panic message", 123)
	log.DPanicf("Some development panic message %d", 123)
	log.DPanicw("Some development panic message", log.V{"value": 123})

	log.Panic("Some panic message", 123)
	log.Panicf("Some panic message %d", 123)
	log.Panicw("Some panic message", log.V{"value": 123})

	log.Fatal("Some fatal message", 123)
	log.Fatalf("Some fatal message %d", 123)
	log.Fatalw("Some fatal message", log.V{"value": 123})
}

Advanced

See default.go file or examples folder for advanced usage examples.

Documentation

Index

Constants

View Source
const (
	DefaultConsoleStdoutHandlerName = "default-console-stdout-handler"
	DefaultConsoleStderrHandlerName = "default-console-stderr-handler"
	DefaultFileHandlerName          = "default-file-handler"
)

Variables

This section is empty.

Functions

func DPanic

func DPanic(v ...any)

func DPanicf

func DPanicf(format string, v ...any)

func DPanicw

func DPanicw(msg string, vars V)

func Debug

func Debug(v ...any)

func Debugf

func Debugf(format string, v ...any)

func Debugw

func Debugw(msg string, vars V)

func DevelopmentMode

func DevelopmentMode()

DevelopmentMode puts the logger in development mode, which makes DPanic-level logs panic instead of simply logging an error.

func Error

func Error(v ...any)

func Errorf

func Errorf(format string, v ...any)

func Errorw

func Errorw(msg string, vars V)

func Fatal

func Fatal(v ...any)

func Fatalf

func Fatalf(format string, v ...any)

func Fatalw

func Fatalw(msg string, vars V)

func Info

func Info(v ...any)

func Infof

func Infof(format string, v ...any)

func Infow

func Infow(msg string, vars V)

func Log

func Log(level zapcore.Level, v []any)

func Logf

func Logf(level zapcore.Level, format string, v []any)

func Logw

func Logw(level zapcore.Level, msg string, vars V)

func Panic

func Panic(v ...any)

func Panicf

func Panicf(format string, v ...any)

func Panicw

func Panicw(msg string, vars V)

func RegisterHandler

func RegisterHandler(name string, h *Handler)

RegisterHandler allows to register new logging handler.

func UnregisterHandler

func UnregisterHandler(name string)

UnregisterHandler allows to unregister existing logging handler.

func UseDefaultConsoleHandler

func UseDefaultConsoleHandler()

func UseDefaultFileHandler

func UseDefaultFileHandler(path string)

func UseOptions

func UseOptions(opts ...zap.Option)

func Warn

func Warn(v ...any)

func Warnf

func Warnf(format string, v ...any)

func Warnw

func Warnw(msg string, vars V)

func WithCaller

func WithCaller()

WithCaller configures the logger to annotate each message with the filename, line number, and function name (it needs to be enabled by setting function key).

func WithStacktrace

func WithStacktrace()

WithStacktrace configures the logger to record a stack trace for all messages at or above Error level.

Types

type Handler

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

func NewHandler

func NewHandler(c HandlerConfig) *Handler

type HandlerConfig

type HandlerConfig struct {
	// Type allows to choose how messages should be encoded.
	//
	// Default: HandlerTypeText
	Type HandlerType
	// Writer defines where to write messages.
	Writer io.Writer
	// WriterSynced defines if writer is already synced.
	// If false, it is wrapped with mutex to make it safe for concurrent use.
	// *os.Files must have this set to false.
	WriterSynced bool

	// Define how to name logger keys.
	// Empty string is treated as default value.
	// If You want to diable some key, set its value to "-".
	Keys HandlerKeys
	// Define how to encode logger fields.
	// nil value falls back to default value.
	// if You want to disable some encoder, You have to disable its key.
	Encoders HandlerEncoders
	// ConsoleSeparator between line elements. Default " : " (TIME : LEVEL : MESSAGE).
	ConsoleSeparator string

	// When to log message. Most of the times it simply will be log level. Default to info level.
	Enabler zapcore.LevelEnabler
}

type HandlerEncoders

type HandlerEncoders struct {
	Level    zapcore.LevelEncoder    // Default: zapcore.CapitalLevelEncoder
	Time     zapcore.TimeEncoder     // Default: zapcore.ISO8601TimeEncoder
	Duration zapcore.DurationEncoder // Default: zapcore.StringDurationEncoder
	Caller   zapcore.CallerEncoder   // Default: zapcore.ShortCallerEncoder
	Name     zapcore.NameEncoder     // Optional
}

type HandlerKeys

type HandlerKeys struct {
	Time       string // Default: time
	Level      string // Default: level
	Name       string // Default: logger
	Caller     string // Default: caller
	Function   string // Default: - (disabled)
	Message    string // Default: message
	Stacktrace string // Default: stacktrace
}

type HandlerType

type HandlerType int
const (
	HandlerTypeText HandlerType = iota
	HandlerTypeJSON
)

type V

type V map[string]any

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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