logger

package module
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 10 Imported by: 118

README

Logger

PkgGoDev Build Status

Installation

go get github.com/go-zoox/logger

Usage

logger.Info("hello")
logger.Error("something failed")

Transports

A Logger holds named transports. Each log line is sent to every transport; each transport decides what to do (console, files, etc.).

Console (default)
l := logger.New() // includes console transport
File transport with per-level paths

file.Config.FilePath is the default file. Optional Levels sends specific log levels to other files; all other levels still use FilePath.

import (
	"github.com/go-zoox/logger"
	"github.com/go-zoox/logger/transport/console"
	"github.com/go-zoox/logger/transport/file"
)

l := logger.New(func(opt *logger.Option) {
	opt.Transports = map[string]transport.Transport{
		"console": console.New(),
		"file": file.New(&file.Config{
			FilePath: "/var/log/app.log",
			Levels: map[string]string{
				"error": "/var/log/error.log",
			},
		}),
	}
})

l.Info("→ console + app.log")
l.Error("→ console + error.log")

Omit Levels (or leave it empty) to write every line to FilePath only. Level keys: debug, info, warn, error, fatal.

Multiple transports

Use several entries in opt.Transports (e.g. console + file above). No extra sink/output layer.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// LevelDebug is Level Debug
	LevelDebug = constants.LevelDebug
	// LevelInfo is Level Info
	LevelInfo = constants.LevelInfo
	// LevelWarn is Level Warn
	LevelWarn = constants.LevelWarn
	// LevelError is Level Error
	LevelError = constants.LevelError
	// LevelFatal is Level Fatal
	LevelFatal = constants.LevelFatal
)
View Source
var LogLevelEnv = "LOG_LEVEL"

LogLevelEnv ...

View Source
var Version = "1.6.4"

Version returns the current version of the logger.

Functions

func AppendTransports added in v1.3.3

func AppendTransports(transports map[string]cst.Transport) error

AppendTransports appends the transports of the logger.

func Debug

func Debug(format string, args ...interface{})

Debug logs a debug message.

func Debugf added in v1.1.5

func Debugf(format string, args ...interface{})

Debugf logs a debug message.

func Error

func Error(format string, args ...interface{})

Error logs an error message.

func Errorf added in v1.1.5

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

Errorf logs an error message (routes to sinks with levels: [error], etc.).

func Fatal

func Fatal(format string, args ...interface{})

Fatal logs a fatal message.

func Fatalf added in v1.1.5

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

Fatalf logs a fatal message.

func GetLevel added in v1.4.6

func GetLevel() string

GetLevel returns the level of the logger.

func Info

func Info(format string, args ...interface{})

Info logs an info message.

func Infof added in v1.1.5

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

Infof logs an info message.

func IsDebugLevel added in v1.4.6

func IsDebugLevel() bool

IsDebugLevel returns true if the logger level is debug.

func IsErrorLevel added in v1.4.6

func IsErrorLevel() bool

IsErrorLevel returns true if the logger level is error.

func IsFatalLevel added in v1.4.6

func IsFatalLevel() bool

IsFatalLevel returns true if the logger level is fatal.

func IsInfoLevel added in v1.4.6

func IsInfoLevel() bool

IsInfoLevel returns true if the logger level is info.

func IsWarnLevel added in v1.4.6

func IsWarnLevel() bool

IsWarnLevel returns true if the logger level is warn.

func Now added in v1.1.0

func Now() string

Now returns the current time string.

func SetDefault added in v1.6.4

func SetDefault(l *Logger)

SetDefault replaces the package-level logger (e.g. after configuring outputs).

func SetLevel

func SetLevel(level string) (err error)

SetLevel sets the level of the logger.

func SetTransports added in v1.3.3

func SetTransports(transports map[string]cst.Transport) error

SetTransports sets the transports of the logger.

it will overrides system transport, if you only want append transport, just use AppendTransports.

func Warn

func Warn(format string, args ...interface{})

Warn logs a warning message.

func Warnf added in v1.1.5

func Warnf(format string, args ...interface{})

Warnf logs a warning message (routes by level to configured sinks).

Types

type Logger added in v1.1.0

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

Logger is the main logger object.

func Default added in v1.6.4

func Default() *Logger

Default returns the package-level logger.

func New

func New(option ...func(opt *Option)) *Logger

New creates a new logger object.

func (*Logger) AppendTransports added in v1.3.3

func (l *Logger) AppendTransports(transports map[string]cst.Transport) error

AppendTransports appends the transports of the logger.

func (*Logger) Debug added in v1.1.0

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

Debug logs a debug message.

func (*Logger) Debugf added in v1.3.2

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

Debugf logs a debug message.

func (*Logger) Error added in v1.1.0

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

Error logs an error message.

func (*Logger) Errorf added in v1.3.2

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

Errorf logs an error message.

func (*Logger) Fatal added in v1.1.0

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

Fatal logs a fatal message.

func (*Logger) Fatalf added in v1.3.2

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

Fatalf logs a fatal message.

func (*Logger) GetLevel added in v1.4.6

func (l *Logger) GetLevel() string

GetLevel gets the level of the logger.

func (*Logger) Info added in v1.1.0

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

Info logs an info message.

func (*Logger) Infof added in v1.3.2

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

Infof logs an info message.

func (*Logger) IsDebugLevel added in v1.4.6

func (l *Logger) IsDebugLevel() bool

IsDebugLevel returns true if the logger level is debug.

func (*Logger) IsErrorLevel added in v1.4.6

func (l *Logger) IsErrorLevel() bool

IsErrorLevel returns true if the logger level is error.

func (*Logger) IsFatalLevel added in v1.4.6

func (l *Logger) IsFatalLevel() bool

IsFatalLevel returns true if the logger level is fatal.

func (*Logger) IsInfoLevel added in v1.4.6

func (l *Logger) IsInfoLevel() bool

IsInfoLevel returns true if the logger level is info.

func (*Logger) IsWarnLevel added in v1.4.6

func (l *Logger) IsWarnLevel() bool

IsWarnLevel returns true if the logger level is warn.

func (*Logger) Printf added in v1.4.5

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

Printf logs a message.

func (*Logger) SetLevel added in v1.1.0

func (l *Logger) SetLevel(level string) error

SetLevel sets the level of the logger.

func (*Logger) SetStdout added in v1.6.0

func (l *Logger) SetStdout(stdout io.Writer)

SetStdout sets the stdout of the logger.

func (*Logger) SetTimeFormat added in v1.1.1

func (l *Logger) SetTimeFormat(format string)

SetTimeFormat sets the time format.

func (*Logger) SetTransports added in v1.3.3

func (l *Logger) SetTransports(transports map[string]cst.Transport) error

SetTransports sets the transports of the logger.

it will overrides system transport, if you only want append transport, just use AppendTransports.

func (*Logger) Warn added in v1.1.0

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

Warn logs a warning message.

func (*Logger) Warnf added in v1.3.2

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

Warnf logs a warning message.

func (*Logger) Write added in v1.4.0

func (l *Logger) Write(p []byte) (n int, err error)

Write realizes io.Writer

type Option added in v1.6.0

type Option struct {
	Level      string
	Transports map[string]cst.Transport
	TimeFormat string
}

Option is the options for the logger.

Directories

Path Synopsis
components
transport

Jump to

Keyboard shortcuts

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