Back to godoc.org

Package log

v0.12.5
Latest Go to latest

The latest major version is .

Published: Mar 4, 2020 | License: MIT | Module: aahframe.work

Overview

Package log simple logger and provides capabilities to fulfill application use cases. It supports two receivers `console` and `file` and extensible by interface and Hook.

Also provides standard logger crossover binding (drop-in replacement for standard go logger) for unified logging.

log.Info("Welcome ", "to ", "aah ", "logger")
log.Infof("%v, %v, %v", "simple", "flexible", "logger")

// Output:
2016-07-03 19:22:11.504 INFO  Welcome to aah logger
2016-07-03 19:22:11.504 INFO  simple, flexible, logger

Index

Constants

const (
	FmtFlagLevel ess.FmtFlag = iota
	FmtFlagAppName
	FmtFlagInstanceName
	FmtFlagRequestID
	FmtFlagPrincipal
	FmtFlagTime
	FmtFlagUTCTime
	FmtFlagLongfile
	FmtFlagShortfile
	FmtFlagLine
	FmtFlagMessage
	FmtFlagFields
	FmtFlagCustom
	FmtFlagUnknown
)

Format flags used to define log message format for each log entry

const (
	LevelFatal level = iota
	LevelPanic
	LevelError
	LevelWarn
	LevelInfo
	LevelDebug
	LevelTrace
	LevelUnknown
)

Log Level definition

Variables

var (
	// DefaultPattern is default log entry pattern in aah/log. Only applicable to
	// text formatter.
	// For e.g:
	//    2006-01-02 15:04:05.000 INFO  This is my message
	DefaultPattern = "%time:2006-01-02 15:04:05.000 %level:-5 %message"

	// FmtFlags is the list of log format flags supported by aah log library
	// Usage of flag order is up to format composition.
	//    level     - outputs ERROR, WARN, INFO, DEBUG, TRACE
	//    appname   - outputs Application Name
	//    insname   - outputs Application Instance Name
	//    reqid     - outputs Request ID from HTTP header
	//    principal - outputs Logged-In subject primary principal value
	//    time      - outputs local time as per format supplied
	//    utctime   - outputs UTC time as per format supplied
	//    longfile  - outputs full file name: /a/b/c/d.go
	//    shortfile - outputs final file name element: d.go
	//    line      - outputs file line number: L23
	//    message   - outputs given message along supplied arguments if they present
	//    fields    - outputs field values into log entry
	//    custom    - outputs string as-is into log entry
	FmtFlags = map[string]ess.FmtFlag{
		"level":     FmtFlagLevel,
		"appname":   FmtFlagAppName,
		"insname":   FmtFlagInstanceName,
		"reqid":     FmtFlagRequestID,
		"principal": FmtFlagPrincipal,
		"time":      FmtFlagTime,
		"utctime":   FmtFlagUTCTime,
		"longfile":  FmtFlagLongfile,
		"shortfile": FmtFlagShortfile,
		"line":      FmtFlagLine,
		"message":   FmtFlagMessage,
		"fields":    FmtFlagFields,
		"custom":    FmtFlagCustom,
	}
)
var (
	// ErrLogReceiverIsNil returned when suppiled receiver is nil.
	ErrLogReceiverIsNil = errors.New("log: receiver is nil")

	// ErrHookFuncIsNil is returned when hook function is nil.
	ErrHookFuncIsNil = errors.New("log: hook func is nil")
)

func AddContext

func AddContext(fields Fields)

AddContext method to add context values into default logger. These context values gets logged with each log entry.

func AddHook

func AddHook(name string, hook HookFunc) error

AddHook method is to add logger hook function.

func Debug

func Debug(v ...interface{})

Debug logs message as `DEBUG`. Arguments handled in the mananer of `fmt.Print`.

func Debugf

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

Debugf logs message as `DEBUG`. Arguments handled in the mananer of `fmt.Printf`.

func Error

func Error(v ...interface{})

Error logs message as `ERROR`. Arguments handled in the mananer of `fmt.Print`.

func Errorf

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

Errorf logs message as `ERROR`. Arguments handled in the mananer of `fmt.Printf`.

func Fatal

func Fatal(v ...interface{})

Fatal logs message as `FATAL` and call to os.Exit(1).

func Fatalf

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

Fatalf logs message as `FATAL` and call to os.Exit(1).

func Fatalln

func Fatalln(v ...interface{})

Fatalln logs message as `FATAL` and call to os.Exit(1).

func Info

func Info(v ...interface{})

Info logs message as `INFO`. Arguments handled in the mananer of `fmt.Print`.

func Infof

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

Infof logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func IsLevelDebug

func IsLevelDebug() bool

IsLevelDebug method returns true if log level is DEBUG otherwise false.

func IsLevelError

func IsLevelError() bool

IsLevelError method returns true if log level is ERROR otherwise false.

func IsLevelFatal

func IsLevelFatal() bool

IsLevelFatal method returns true if log level is FATAL otherwise false.

func IsLevelInfo

func IsLevelInfo() bool

IsLevelInfo method returns true if log level is INFO otherwise false.

func IsLevelPanic

func IsLevelPanic() bool

IsLevelPanic method returns true if log level is PANIC otherwise false.

func IsLevelTrace

func IsLevelTrace() bool

IsLevelTrace method returns true if log level is TRACE otherwise false.

func IsLevelWarn

func IsLevelWarn() bool

IsLevelWarn method returns true if log level is WARN otherwise false.

func Level

func Level() string

Level method returns currently enabled logging level.

func Panic

func Panic(v ...interface{})

Panic logs message as `PANIC` and call to panic().

func Panicf

func Panicf(format string, v ...interface{})

Panicf logs message as `PANIC` and call to panic().

func Panicln

func Panicln(v ...interface{})

Panicln logs message as `PANIC` and call to panic().

func Print

func Print(v ...interface{})

Print logs message as `INFO`. Arguments handled in the mananer of `fmt.Print`.

func Printf

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

Printf logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func Println

func Println(v ...interface{})

Println logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func SetDefaultLogger

func SetDefaultLogger(l *Logger)

SetDefaultLogger method sets the given logger instance as default logger.

func SetLevel

func SetLevel(level string) error

SetLevel method sets log level for default logger.

func SetPattern

func SetPattern(pattern string) error

SetPattern method sets the log format pattern for default logger.

func SetWriter

func SetWriter(w io.Writer)

SetWriter method sets the given writer into logger instance.

func ToGoLogger

func ToGoLogger() *slog.Logger

ToGoLogger method wraps the current log writer into Go Logger instance.

func Trace

func Trace(v ...interface{})

Trace logs message as `TRACE`. Arguments handled in the mananer of `fmt.Print`.

func Tracef

func Tracef(format string, v ...interface{})

Tracef logs message as `TRACE`. Arguments handled in the mananer of `fmt.Printf`.

func Warn

func Warn(v ...interface{})

Warn logs message as `WARN`. Arguments handled in the mananer of `fmt.Print`.

func Warnf

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

Warnf logs message as `WARN`. Arguments handled in the mananer of `fmt.Printf`.

func Writer

func Writer() io.Writer

Writer method returns the writer of default logger.

type ConsoleReceiver

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

ConsoleReceiver writes the log entry into os.Stderr. For non-windows it writes with color.

func (*ConsoleReceiver) Init

func (c *ConsoleReceiver) Init(cfg *config.Config) error

Init method initializes the console logger.

func (*ConsoleReceiver) IsCallerInfo

func (c *ConsoleReceiver) IsCallerInfo() bool

IsCallerInfo method returns true if log receiver is configured with caller info otherwise false.

func (*ConsoleReceiver) Log

func (c *ConsoleReceiver) Log(entry *Entry)

Log method writes the log entry into os.Stderr.

func (*ConsoleReceiver) SetPattern

func (c *ConsoleReceiver) SetPattern(pattern string) error

SetPattern method initializes the logger format pattern.

func (*ConsoleReceiver) SetWriter

func (c *ConsoleReceiver) SetWriter(w io.Writer)

SetWriter method sets the given writer into console receiver.

func (*ConsoleReceiver) Writer

func (c *ConsoleReceiver) Writer() io.Writer

Writer method returns the current log writer.

type Entry

type Entry struct {
	Level        level     `json:"-"`
	Line         int       `json:"line,omitempty"`
	AppName      string    `json:"app_name,omitempty"`
	InstanceName string    `json:"instance_name,omitempty"`
	RequestID    string    `json:"request_id,omitempty"`
	Principal    string    `json:"principal,omitempty"`
	Message      string    `json:"message,omitempty"`
	File         string    `json:"file,omitempty"`
	Fields       Fields    `json:"fields,omitempty"`
	Time         time.Time `json:"-"`
	// contains filtered or unexported fields
}

Entry represents a log entry and contains the timestamp when the entry was created, level, etc.

func (*Entry) Debug

func (e *Entry) Debug(v ...interface{})

Debug logs message as `DEBUG`. Arguments handled in the mananer of `fmt.Print`.

func (*Entry) Debugf

func (e *Entry) Debugf(format string, v ...interface{})

Debugf logs message as `DEBUG`. Arguments handled in the mananer of `fmt.Printf`.

func (*Entry) Error

func (e *Entry) Error(v ...interface{})

Error logs message as `ERROR`. Arguments handled in the mananer of `fmt.Print`.

func (*Entry) Errorf

func (e *Entry) Errorf(format string, v ...interface{})

Errorf logs message as `ERROR`. Arguments handled in the mananer of `fmt.Printf`.

func (*Entry) Fatal

func (e *Entry) Fatal(v ...interface{})

Fatal logs message as `FATAL` and call to os.Exit(1).

func (*Entry) Fatalf

func (e *Entry) Fatalf(format string, v ...interface{})

Fatalf logs message as `FATAL` and call to os.Exit(1).

func (*Entry) Fatalln

func (e *Entry) Fatalln(v ...interface{})

Fatalln logs message as `FATAL` and call to os.Exit(1).

func (*Entry) Info

func (e *Entry) Info(v ...interface{})

Info logs message as `INFO`. Arguments handled in the mananer of `fmt.Print`.

func (*Entry) Infof

func (e *Entry) Infof(format string, v ...interface{})

Infof logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func (*Entry) IsLevelDebug

func (e *Entry) IsLevelDebug() bool

IsLevelDebug method returns true if log level is DEBUG otherwise false.

func (*Entry) IsLevelError

func (e *Entry) IsLevelError() bool

IsLevelError method returns true if log level is ERROR otherwise false.

func (*Entry) IsLevelFatal

func (e *Entry) IsLevelFatal() bool

IsLevelFatal method returns true if log level is FATAL otherwise false.

func (*Entry) IsLevelInfo

func (e *Entry) IsLevelInfo() bool

IsLevelInfo method returns true if log level is INFO otherwise false.

func (*Entry) IsLevelPanic

func (e *Entry) IsLevelPanic() bool

IsLevelPanic method returns true if log level is PANIC otherwise false.

func (*Entry) IsLevelTrace

func (e *Entry) IsLevelTrace() bool

IsLevelTrace method returns true if log level is TRACE otherwise false.

func (*Entry) IsLevelWarn

func (e *Entry) IsLevelWarn() bool

IsLevelWarn method returns true if log level is WARN otherwise false.

func (*Entry) MarshalJSON

func (e *Entry) MarshalJSON() ([]byte, error)

MarshalJSON method for formating entry to JSON.

func (*Entry) Panic

func (e *Entry) Panic(v ...interface{})

Panic logs message as `PANIC` and call to panic().

func (*Entry) Panicf

func (e *Entry) Panicf(format string, v ...interface{})

Panicf logs message as `PANIC` and call to panic().

func (*Entry) Panicln

func (e *Entry) Panicln(v ...interface{})

Panicln logs message as `PANIC` and call to panic().

func (*Entry) Print

func (e *Entry) Print(v ...interface{})

Print logs message as `INFO`. Arguments handled in the mananer of `fmt.Print`.

func (*Entry) Printf

func (e *Entry) Printf(format string, v ...interface{})

Printf logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func (*Entry) Println

func (e *Entry) Println(v ...interface{})

Println logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func (*Entry) Reset

func (e *Entry) Reset()

Reset method resets the `Entry` values for reuse.

func (*Entry) ToGoLogger

func (e *Entry) ToGoLogger() *slog.Logger

ToGoLogger method wraps the current log writer into Go Logger instance.

func (*Entry) Trace

func (e *Entry) Trace(v ...interface{})

Trace logs message as `TRACE`. Arguments handled in the mananer of `fmt.Print`.

func (*Entry) Tracef

func (e *Entry) Tracef(format string, v ...interface{})

Tracef logs message as `TRACE`. Arguments handled in the mananer of `fmt.Printf`.

func (*Entry) Warn

func (e *Entry) Warn(v ...interface{})

Warn logs message as `WARN`. Arguments handled in the mananer of `fmt.Print`.

func (*Entry) Warnf

func (e *Entry) Warnf(format string, v ...interface{})

Warnf logs message as `WARN`. Arguments handled in the mananer of `fmt.Printf`.

func (*Entry) WithField

func (e *Entry) WithField(key string, value interface{}) Loggerer

WithField method to add single key-value into log

func (*Entry) WithFields

func (e *Entry) WithFields(fields Fields) Loggerer

WithFields method to add multiple key-value pairs into log.

type Fields

type Fields map[string]interface{}

Fields type is used to log fields values in the logger.

type FileReceiver

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

FileReceiver writes the log entry into file.

func (*FileReceiver) Init

func (f *FileReceiver) Init(cfg *config.Config) error

Init method initializes the file receiver instance.

func (*FileReceiver) IsCallerInfo

func (f *FileReceiver) IsCallerInfo() bool

IsCallerInfo method returns true if log receiver is configured with caller info otherwise false.

func (*FileReceiver) Log

func (f *FileReceiver) Log(entry *Entry)

Log method logs the given entry values into file.

func (*FileReceiver) SetPattern

func (f *FileReceiver) SetPattern(pattern string) error

SetPattern method initializes the logger format pattern.

func (*FileReceiver) SetWriter

func (f *FileReceiver) SetWriter(w io.Writer)

SetWriter method sets the given writer into file receiver.

func (*FileReceiver) Writer

func (f *FileReceiver) Writer() io.Writer

Writer method returns the current log writer.

type FlagPart

type FlagPart struct {
	Flag   ess.FmtFlag
	Name   string
	Format string
}

FlagPart is indiviual flag details

For e.g.:
  part := FlagPart{
    Flag:   fmtFlagTime,
    Name:   "time",
    Format: "2006-01-02 15:04:05.000",
  }

type HookFunc

type HookFunc func(e Entry)

HookFunc type is aah framework logger custom hook.

type Logger

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

Logger is the object which logs the given message into recevier as per deifned format flags. Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Receivers.

func New

func New(cfg *config.Config) (*Logger, error)

New method creates the aah logger based on supplied `config.Config`.

func NewWithContext

func NewWithContext(cfg *config.Config, ctx Fields) (*Logger, error)

NewWithContext method creates the aah logger based on supplied `config.Config`.

func (*Logger) AddContext

func (l *Logger) AddContext(fields Fields)

AddContext method to add context values into current logger.

func (*Logger) AddHook

func (l *Logger) AddHook(name string, hook HookFunc) error

AddHook method is to add logger hook function.

func (*Logger) Debug

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

Debug logs message as `DEBUG`. Arguments handled in the mananer of `fmt.Print`.

func (*Logger) Debugf

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

Debugf logs message as `DEBUG`. Arguments handled in the mananer of `fmt.Printf`.

func (*Logger) Error

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

Error logs message as `ERROR`. Arguments handled in the mananer of `fmt.Print`.

func (*Logger) Errorf

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

Errorf logs message as `ERROR`. Arguments handled in the mananer of `fmt.Printf`.

func (*Logger) Fatal

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

Fatal logs message as `FATAL` and call to os.Exit(1).

func (*Logger) Fatalf

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

Fatalf logs message as `FATAL` and call to os.Exit(1).

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Fatalln logs message as `FATAL` and call to os.Exit(1).

func (*Logger) Info

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

Info logs message as `INFO`. Arguments handled in the mananer of `fmt.Print`.

func (*Logger) Infof

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

Infof logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func (*Logger) IsLevelDebug

func (l *Logger) IsLevelDebug() bool

IsLevelDebug method returns true if log level is DEBUG otherwise false.

func (*Logger) IsLevelError

func (l *Logger) IsLevelError() bool

IsLevelError method returns true if log level is ERROR otherwise false.

func (*Logger) IsLevelFatal

func (l *Logger) IsLevelFatal() bool

IsLevelFatal method returns true if log level is FATAL otherwise false.

func (*Logger) IsLevelInfo

func (l *Logger) IsLevelInfo() bool

IsLevelInfo method returns true if log level is INFO otherwise false.

func (*Logger) IsLevelPanic

func (l *Logger) IsLevelPanic() bool

IsLevelPanic method returns true if log level is PANIC otherwise false.

func (*Logger) IsLevelTrace

func (l *Logger) IsLevelTrace() bool

IsLevelTrace method returns true if log level is TRACE otherwise false.

func (*Logger) IsLevelWarn

func (l *Logger) IsLevelWarn() bool

IsLevelWarn method returns true if log level is WARN otherwise false.

func (*Logger) Level

func (l *Logger) Level() string

Level method returns currently enabled logging level.

func (*Logger) New

func (l *Logger) New(fields Fields) *Logger

New method creates a child logger and adds structured context to it. Child logger inherits parent logger context value on creation. Fields added to the child don't affect the parent logger and vice versa. These context values gets logged with each log entry.

Also you can use method `AddContext` to context to the current logger.

func (*Logger) Panic

func (l *Logger) Panic(v ...interface{})

Panic logs message as `PANIC` and call to panic().

func (*Logger) Panicf

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

Panicf logs message as `PANIC` and call to panic().

func (*Logger) Panicln

func (l *Logger) Panicln(v ...interface{})

Panicln logs message as `PANIC` and call to panic().

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print logs message as `INFO`. Arguments handled in the mananer of `fmt.Print`.

func (*Logger) Printf

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

Printf logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func (*Logger) Println

func (l *Logger) Println(v ...interface{})

Println logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.

func (*Logger) SetLevel

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

SetLevel method sets the given logging level for the logger. For e.g.: INFO, WARN, DEBUG, etc. Case-insensitive.

func (*Logger) SetPattern

func (l *Logger) SetPattern(pattern string) error

SetPattern method sets the log format pattern.

func (*Logger) SetReceiver

func (l *Logger) SetReceiver(receiver Receiver) error

SetReceiver method sets the given receiver into logger instance.

func (*Logger) SetWriter

func (l *Logger) SetWriter(w io.Writer)

SetWriter method sets the given writer into logger instance.

func (*Logger) ToGoLogger

func (l *Logger) ToGoLogger() *slog.Logger

ToGoLogger method wraps the current log writer into Go Logger instance.

func (*Logger) Trace

func (l *Logger) Trace(v ...interface{})

Trace logs message as `TRACE`. Arguments handled in the mananer of `fmt.Print`.

func (*Logger) Tracef

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

Tracef logs message as `TRACE`. Arguments handled in the mananer of `fmt.Printf`.

func (*Logger) Warn

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

Warn logs message as `WARN`. Arguments handled in the mananer of `fmt.Print`.

func (*Logger) Warnf

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

Warnf logs message as `WARN`. Arguments handled in the mananer of `fmt.Printf`.

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) Loggerer

WithField method to add single key-value into log entry.

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fields) Loggerer

WithFields method to add multiple key-value pairs into log entry.

type Loggerer

type Loggerer interface {
	Error(v ...interface{})
	Errorf(format string, v ...interface{})
	Warn(v ...interface{})
	Warnf(format string, v ...interface{})
	Info(v ...interface{})
	Infof(format string, v ...interface{})
	Debug(v ...interface{})
	Debugf(format string, v ...interface{})
	Trace(v ...interface{})
	Tracef(format string, v ...interface{})

	// Context/Field methods
	WithFields(fields Fields) Loggerer
	WithField(key string, value interface{}) Loggerer

	// Level Info
	IsLevelInfo() bool
	IsLevelError() bool
	IsLevelWarn() bool
	IsLevelDebug() bool
	IsLevelTrace() bool
	IsLevelFatal() bool
	IsLevelPanic() bool

	// For standard logger drop-in replacement
	ToGoLogger() *slog.Logger
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})
	Panic(v ...interface{})
	Panicf(format string, v ...interface{})
	Panicln(v ...interface{})
}

Loggerer interface is for Logger and Entry log method implementation.

func WithField

func WithField(key string, value interface{}) Loggerer

WithField method to add single key-value into log

func WithFields

func WithFields(fields Fields) Loggerer

WithFields method to add multiple key-value pairs into log.

type Receiver

type Receiver interface {
	Init(cfg *config.Config) error
	SetPattern(pattern string) error
	SetWriter(w io.Writer)
	IsCallerInfo() bool
	Writer() io.Writer
	Log(e *Entry)
}

Receiver is the interface for pluggable log receiver. For e.g: Console, File

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier