logger

package
v0.0.0-...-2a10a86 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 4 Imported by: 0

README

Logger

Install

go get -u github.com/lazychanger/go-contrib/logger

How to use

package main

import (
	log "github.com/lazychanger/go-contrib/logger"
	// zerolog driver
	_ "github.com/lazychanger/go-contrib/logger/zerolog"
)

func main() {
	log.SetLevel(log.InfoLevel)
	// default is json format 
	log.SetFormat(log.Color)

	log.Trace("Hello Trace")
	log.Debug("Hello Debug")
	log.Info("Hello Info")
	log.Warn("Hello Warn")
	log.Error("Hello Error")
	log.Fatal("Hello Fatal")
	log.Panic("Hello Panic")
}

Todo

** More driver **

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string)

func DebugF

func DebugF(msg string, a ...any)

func Error

func Error(msg string)

func ErrorF

func ErrorF(msg string, a ...any)

func Fatal

func Fatal(msg string)

func FatalF

func FatalF(msg string, a ...any)

func Info

func Info(msg string)

func InfoF

func InfoF(msg string, a ...any)

func Panic

func Panic(msg string)

func PanicF

func PanicF(msg string, a ...any)

func Print

func Print(a ...any)

func Printf

func Printf(format string, a ...any)

func RegisterDefaultLogger

func RegisterDefaultLogger(logger Logger)

func SetFormat

func SetFormat(format Format)

func SetLevel

func SetLevel(lvl Level)

func Trace

func Trace(msg string)

func TraceF

func TraceF(msg string, a ...any)

func Warn

func Warn(msg string)

func WarnF

func WarnF(msg string, a ...any)

Types

type Field

type Field struct {
	Name  string
	Value any
}

type Format

type Format uint8
const (
	Color Format = iota // color
	Text                // text
	Json                // json
)

func ParseFormat

func ParseFormat(fmt string) Format

func (Format) String

func (i Format) String() string

type Level

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

	// TraceLevel defines trace log level.
	TraceLevel Level = -1 // trace

)

func ParseLevel

func ParseLevel(lvl string) Level

ParseLevel will transfer string to Level

func (Level) String

func (i Level) String() string

type Logger

type Logger interface {
	io.Writer

	// With returns a new Logger with the specified field added to the context.
	With(name string, value any) Logger

	// Withs returns a new Logger with the specified fields added to the context.
	Withs(fields ...Field) Logger

	// WithLevel returns a new Logger with the specified level.
	WithLevel(lvl Level) Logger

	// WithFormat returns a new Logger with the specified format.
	WithFormat(format Format) Logger

	// Level returns the current level of the logger.
	Level() Level

	// Format returns the current format of the logger.
	Format() Format

	// SetLevel sets the current level of the logger.
	SetLevel(lvl Level)

	// SetFormat sets the format of the logger.
	SetFormat(format Format)

	// SetWriter sets the log output
	SetWriter(writer io.Writer)

	// Log logs a message at the specified level.
	Log(lvl Level, msg string)

	// LogF logs a formatted message at the specified level.
	LogF(lvl Level, msg string, a ...any)

	// Trace logs a trace message.
	Trace(msg string)

	// Debug logs a debug message.
	Debug(msg string)

	// Info logs an info message.
	Info(msg string)

	// Warn logs a warning message.
	Warn(msg string)

	// Error logs an error message.
	Error(msg string)

	// Fatal logs a fatal message and then panics.
	Fatal(msg string)

	// Panic logs a panic message and then panics.
	Panic(msg string)

	// TraceF logs a formatted trace message.
	TraceF(msg string, a ...any)

	// DebugF logs a formatted debug message.
	DebugF(msg string, a ...any)

	// InfoF logs a formatted info message.
	InfoF(msg string, a ...any)

	// WarnF logs a formatted warning message.
	WarnF(msg string, a ...any)

	// ErrorF logs a formatted error message.
	ErrorF(msg string, a ...any)

	// FatalF logs a formatted fatal message and then panics.
	FatalF(msg string, a ...any)

	// PanicF logs a formatted panic message and then panics.
	PanicF(msg string, a ...any)
}

Logger is an interface that defines the logging functionality of a program.

func GetLogger

func GetLogger() Logger

func With

func With(name string, value any) Logger

func WithFormat

func WithFormat(format Format) Logger

func WithLevel

func WithLevel(lvl Level) Logger

func Withs

func Withs(fields ...Field) Logger

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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