log.v0: aahframework.org/log.v0 Index | Files

package log

import "aahframework.org/log.v0"

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

Package Files

console_receiver.go default.go entry.go file_receiver.go formatter.go log.go stats.go util.go version.go

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

const Version = "0.7.1"

Version no. of aahframework.org/log library

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 Uses

func AddContext(fields Fields)

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

func AddHook Uses

func AddHook(name string, hook HookFunc) error

AddHook method is to add logger hook function.

func Debug Uses

func Debug(v ...interface{})

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

func Debugf Uses

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

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

func Error Uses

func Error(v ...interface{})

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

func Errorf Uses

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

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

func Fatal Uses

func Fatal(v ...interface{})

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

func Fatalf Uses

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

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

func Fatalln Uses

func Fatalln(v ...interface{})

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

func Info Uses

func Info(v ...interface{})

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

func Infof Uses

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

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

func IsLevelDebug Uses

func IsLevelDebug() bool

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

func IsLevelError Uses

func IsLevelError() bool

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

func IsLevelFatal Uses

func IsLevelFatal() bool

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

func IsLevelInfo Uses

func IsLevelInfo() bool

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

func IsLevelPanic Uses

func IsLevelPanic() bool

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

func IsLevelTrace Uses

func IsLevelTrace() bool

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

func IsLevelWarn Uses

func IsLevelWarn() bool

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

func Level Uses

func Level() string

Level method returns currently enabled logging level.

func Panic Uses

func Panic(v ...interface{})

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

func Panicf Uses

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

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

func Panicln Uses

func Panicln(v ...interface{})

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

func Print Uses

func Print(v ...interface{})

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

func Printf Uses

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

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

func Println Uses

func Println(v ...interface{})

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

func SetDefaultLogger Uses

func SetDefaultLogger(l *Logger)

SetDefaultLogger method sets the given logger instance as default logger.

func SetLevel Uses

func SetLevel(level string) error

SetLevel method sets log level for default logger.

func SetPattern Uses

func SetPattern(pattern string) error

SetPattern method sets the log format pattern for default logger.

func SetWriter Uses

func SetWriter(w io.Writer)

SetWriter method sets the given writer into logger instance.

func ToGoLogger Uses

func ToGoLogger() *slog.Logger

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

func Trace Uses

func Trace(v ...interface{})

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

func Tracef Uses

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

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

func Warn Uses

func Warn(v ...interface{})

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

func Warnf Uses

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

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

func Writer Uses

func Writer() io.Writer

Writer method returns the writer of default logger.

type ConsoleReceiver Uses

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 Uses

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

Init method initializes the console logger.

func (*ConsoleReceiver) IsCallerInfo Uses

func (c *ConsoleReceiver) IsCallerInfo() bool

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

func (*ConsoleReceiver) Log Uses

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

Log method writes the log entry into os.Stderr.

func (*ConsoleReceiver) SetPattern Uses

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

SetPattern method initializes the logger format pattern.

func (*ConsoleReceiver) SetWriter Uses

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

SetWriter method sets the given writer into console receiver.

func (*ConsoleReceiver) Writer Uses

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

Writer method returns the current log writer.

type Entry Uses

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 Uses

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

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

func (*Entry) Debugf Uses

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

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

func (*Entry) Error Uses

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

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

func (*Entry) Errorf Uses

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

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

func (*Entry) Fatal Uses

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

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

func (*Entry) Fatalf Uses

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

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

func (*Entry) Fatalln Uses

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

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

func (*Entry) Info Uses

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

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

func (*Entry) Infof Uses

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

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

func (*Entry) IsLevelDebug Uses

func (e *Entry) IsLevelDebug() bool

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

func (*Entry) IsLevelError Uses

func (e *Entry) IsLevelError() bool

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

func (*Entry) IsLevelFatal Uses

func (e *Entry) IsLevelFatal() bool

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

func (*Entry) IsLevelInfo Uses

func (e *Entry) IsLevelInfo() bool

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

func (*Entry) IsLevelPanic Uses

func (e *Entry) IsLevelPanic() bool

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

func (*Entry) IsLevelTrace Uses

func (e *Entry) IsLevelTrace() bool

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

func (*Entry) IsLevelWarn Uses

func (e *Entry) IsLevelWarn() bool

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

func (*Entry) MarshalJSON Uses

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

MarshalJSON method for formating entry to JSON.

func (*Entry) Panic Uses

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

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

func (*Entry) Panicf Uses

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

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

func (*Entry) Panicln Uses

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

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

func (*Entry) Print Uses

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

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

func (*Entry) Printf Uses

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

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

func (*Entry) Println Uses

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

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

func (*Entry) Reset Uses

func (e *Entry) Reset()

Reset method resets the `Entry` values for reuse.

func (*Entry) ToGoLogger Uses

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

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

func (*Entry) Trace Uses

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

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

func (*Entry) Tracef Uses

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

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

func (*Entry) Warn Uses

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

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

func (*Entry) Warnf Uses

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

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

func (*Entry) WithField Uses

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

WithField method to add single key-value into log

func (*Entry) WithFields Uses

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

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

type Fields Uses

type Fields map[string]interface{}

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

type FileReceiver Uses

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

FileReceiver writes the log entry into file.

func (*FileReceiver) Init Uses

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

Init method initializes the file receiver instance.

func (*FileReceiver) IsCallerInfo Uses

func (f *FileReceiver) IsCallerInfo() bool

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

func (*FileReceiver) Log Uses

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

Log method logs the given entry values into file.

func (*FileReceiver) SetPattern Uses

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

SetPattern method initializes the logger format pattern.

func (*FileReceiver) SetWriter Uses

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

SetWriter method sets the given writer into file receiver.

func (*FileReceiver) Writer Uses

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

Writer method returns the current log writer.

type FlagPart Uses

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 Uses

type HookFunc func(e Entry)

HookFunc type is aah framework logger custom hook.

type Logger Uses

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 Uses

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

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

func NewWithContext Uses

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

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

func (*Logger) AddContext Uses

func (l *Logger) AddContext(fields Fields)

AddContext method to add context values into current logger.

func (*Logger) AddHook Uses

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

AddHook method is to add logger hook function.

func (*Logger) Debug Uses

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

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

func (*Logger) Debugf Uses

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

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

func (*Logger) Error Uses

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

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

func (*Logger) Errorf Uses

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

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

func (*Logger) Fatal Uses

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

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

func (*Logger) Fatalf Uses

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

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

func (*Logger) Fatalln Uses

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

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

func (*Logger) Info Uses

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

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

func (*Logger) Infof Uses

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

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

func (*Logger) IsLevelDebug Uses

func (l *Logger) IsLevelDebug() bool

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

func (*Logger) IsLevelError Uses

func (l *Logger) IsLevelError() bool

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

func (*Logger) IsLevelFatal Uses

func (l *Logger) IsLevelFatal() bool

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

func (*Logger) IsLevelInfo Uses

func (l *Logger) IsLevelInfo() bool

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

func (*Logger) IsLevelPanic Uses

func (l *Logger) IsLevelPanic() bool

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

func (*Logger) IsLevelTrace Uses

func (l *Logger) IsLevelTrace() bool

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

func (*Logger) IsLevelWarn Uses

func (l *Logger) IsLevelWarn() bool

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

func (*Logger) Level Uses

func (l *Logger) Level() string

Level method returns currently enabled logging level.

func (*Logger) New Uses

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 Uses

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

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

func (*Logger) Panicf Uses

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

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

func (*Logger) Panicln Uses

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

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

func (*Logger) Print Uses

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

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

func (*Logger) Printf Uses

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

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

func (*Logger) Println Uses

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

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

func (*Logger) SetLevel Uses

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 Uses

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

SetPattern method sets the log format pattern.

func (*Logger) SetReceiver Uses

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

SetReceiver method sets the given receiver into logger instance.

func (*Logger) SetWriter Uses

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

SetWriter method sets the given writer into logger instance.

func (*Logger) ToGoLogger Uses

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

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

func (*Logger) Trace Uses

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

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

func (*Logger) Tracef Uses

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

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

func (*Logger) Warn Uses

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

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

func (*Logger) Warnf Uses

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

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

func (*Logger) WithField Uses

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

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

func (*Logger) WithFields Uses

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

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

type Loggerer Uses

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 Uses

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

WithField method to add single key-value into log

func WithFields Uses

func WithFields(fields Fields) Loggerer

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

type Receiver Uses

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 log imports 14 packages (graph) and is imported by 18 packages. Updated 2018-07-23. Refresh now. Tools for package owners.