log

package
v1.39.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultFormatter is the default formatter used and is only the message.
	DefaultFormatter = MustStringFormatter("%{message}")

	// GlogFormatter mimics the glog format
	GlogFormatter = MustStringFormatter("%{level:.1s}%{time:0102 15:04:05.999999} %{pid} %{shortfile}] %{message}")
)

Functions

func Debug

func Debug(formatOrError interface{}, args ...interface{})

func Dump

func Dump(formatOrObject interface{}, args ...interface{})

func Error

func Error(formatOrError interface{}, args ...interface{}) error

func Fatal

func Fatal(formatOrError interface{}, args ...interface{})

func Info

func Info(formatOrError interface{}, args ...interface{})

func JSON

func JSON(formatOrObject interface{}, args ...interface{})

func NewBackendFormatter

func NewBackendFormatter(b logging.Backend, f Formatter) logging.Backend

NewBackendFormatter creates a new backend which makes all records that passes through it beeing formatted by the specific formatter.

func Panic

func Panic(formatOrError interface{}, args ...interface{})

func Request

func Request(req *http.Request, args ...interface{}) error

func RequestOut

func RequestOut(req *http.Request, args ...interface{}) error

func Response

func Response(res *http.Response, args ...interface{}) error

func SetFormatter

func SetFormatter(f Formatter)

SetFormatter sets the default formatter for all new backends. A backend will fetch this value once it is needed to format a record. Note that backends will cache the formatter after the first point. For now, make sure to set the formatter before logging.

func SetVerbose

func SetVerbose(verbose bool)

func Stack

func Stack(args ...interface{})

func Verbose

func Verbose() bool

func Warn

func Warn(formatOrError interface{}, args ...interface{})

Types

type Backend

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

Custom logger backend that writes to stdout/stderr

func NewBackend

func NewBackend(ctx context.Context) *Backend

NewBackend creates a new logging backend

func (Backend) Log

func (b Backend) Log(level logging.Level, calldepth int, record *logging.Record) error

Log method that outputs to stdout/stderr

func (Backend) Verbose

func (b Backend) Verbose() bool

type Formatter

type Formatter interface {
	Format(calldepth int, r *logging.Record, w io.Writer) error
}

Formatter is the required interface for a custom log record formatter.

func MustStringFormatter

func MustStringFormatter(format string) Formatter

MustStringFormatter is equivalent to NewStringFormatter with a call to panic on error.

func NewStringFormatter

func NewStringFormatter(format string) (Formatter, error)

NewStringFormatter returns a new Formatter which outputs the log record as a string based on the 'verbs' specified in the format string.

The verbs:

General:

%{id}        Sequence number for log message (uint64).
%{pid}       Process id (int)
%{time}      Time when log occurred (time.Time)
%{level}     Log level (Level)
%{module}    Module (string)
%{program}   Basename of os.Args[0] (string)
%{message}   Message (string)
%{longfile}  Full file name and line number: /a/b/c/d.go:23
%{shortfile} Final file name element and line number: d.go:23
%{callpath}  Callpath like main.a.b.c...c  "..." meaning recursive call ~. meaning truncated path
%{color}     ANSI color based on log level

For normal types, the output can be customized by using the 'verbs' defined in the fmt package, eg. '%{id:04d}' to make the id output be '%04d' as the format string.

For time.Time, use the same layout as time.Format to change the time format when output, eg "2006-01-02T15:04:05.999Z-07:00".

For the 'color' verb, the output can be adjusted to either use bold colors, i.e., '%{color:bold}' or to reset the ANSI attributes, i.e., '%{color:reset}' Note that if you use the color verb explicitly, be sure to reset it or else the color state will persist past your log message. e.g., "%{color:bold}%{time:15:04:05} %{level:-8s}%{color:reset} %{message}" will just colorize the time and level, leaving the message uncolored.

For the 'callpath' verb, the output can be adjusted to limit the printing the stack depth. i.e. '%{callpath:3}' will print '~.a.b.c'

Colors on Windows is unfortunately not supported right now and is currently a no-op.

There's also a couple of experimental 'verbs'. These are exposed to get feedback and needs a bit of tinkering. Hence, they might change in the future.

Experimental:

%{longpkg}   Full package path, eg. github.com/go-logging
%{shortpkg}  Base package path, eg. go-logging
%{longfunc}  Full function name, eg. littleEndian.PutUint32
%{shortfunc} Base function name, eg. PutUint32
%{callpath}  Call function path, eg. main.a.b.c

type LogLevel

type LogLevel int

LogLevel represents log severity levels

const (
	LevelDebug LogLevel = iota
	LevelInfo
	LevelWarning
	LevelError
	LevelCritical
)

type Logger

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

Custom logger

func New

func New() *Logger

Create a new logger

func (*Logger) SetVerbose

func (l *Logger) SetVerbose(verbose bool)

func (*Logger) Verbose

func (l *Logger) Verbose() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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