logger

package
v4.6.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

Logger, helper around logrus

A Helper around Logrus to provide additional functionality as below:

  1. Initializer to set default log level, log location etc
  2. Initializer to automatically add hooks for stdout and file
  3. Supports log rotation with max_size and max_file params
  4. Allow command line overrides using flag

Howto use it

import(
    log "github.com/hpe-storage/common-host-libs/logger"
)

// Example1:
// initialize with nil params to consider default values
func main() {
    log.InitLogging("/var/log/hpe-storage.log", nil, false)

    // ### Use logrus as normal
    log.WithFields(log.Fields{
    "provisioner": "doryd",
    }).Error("error appears here")
}

// Example2:
// initialize with overriding log_level to debug: default(info)
func main() {
    log.InitLogging("/var/log/hpe-storage.log", &log.LogParams{Level: "debug"}, false)
    // ### Use logrus as normal
    log.WithFields(log.Fields{
    "provisioner": "doryd",
    }).Debug("debug appears here")
}


// Example3:
// initialize with logging to both file and stdout/stderr
func main() {
    log.InitLogging("/var/log/hpe-storage.log", nil, true)
    // ### Use logrus as normal
    log.WithFields(log.Fields{
    "provisioner": "doryd",
    }).Error("error appears here")
}


// Example4:
// initialize with log format as json: default(text)
func main() {
    log.InitLogging("/var/log/hpe-storage.log", &log.LogParams{Format: "json"}, false)
    // ### Use logrus as normal
    log.WithFields(log.Fields{
    "provisioner": "doryd",
    }).Error("error appears here")
}

// Example5:
// initialize with log level as trace using env params
func main() {
    os.Setenv("LOG_LEVEL", "trace")
    log.InitLogging("/var/log/hpe-storage.log", nil, false)
    // ### Use logrus as normal
    log.WithFields(log.Fields{
    "provisioner": "doryd",
    }).Trace("trace appears here")
}

Documentation

Index

Constants

View Source
const (
	DefaultLogLevel    = "info"
	DefaultLogFormat   = TextFormat
	DefaultMaxLogFiles = 10
	MaxFilesLimit      = 20
	DefaultMaxLogSize  = 100  // in MB
	MaxLogSizeLimit    = 1024 // in MB
	JsonFormat         = "json"
	TextFormat         = "text"
)

Variables

This section is empty.

Functions

func AddConsoleHook

func AddConsoleHook() error

func AddFileHook

func AddFileHook() error

func AddHook

func AddHook(hook log.Hook)

AddHook adds a hook to the standard logger hooks.

func CustomCallerPrettyfier

func CustomCallerPrettyfier(f *runtime.Frame) (string, string)

func Debug

func Debug(args ...interface{})

Debug logs a message at level Debug on the standard logger.

func Debugf

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

Debugf logs a message at level Debug on the standard logger.

func Debugln

func Debugln(args ...interface{})

Debugln logs a message at level Debug on the standard logger.

func Error

func Error(args ...interface{})

Error logs a message at level Error on the standard logger.

func Errorf

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

Errorf logs a message at level Error on the standard logger.

func Errorln

func Errorln(args ...interface{})

Errorln logs a message at level Error on the standard logger.

func Fatal

func Fatal(args ...interface{})

Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.

func Fatalf

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

Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1.

func GetLevel

func GetLevel() log.Level

GetLevel returns the standard logger level.

func HTTPLogger

func HTTPLogger(inner http.Handler, name string) http.Handler

HTTPLogger : wrapper for http logging

func Info

func Info(args ...interface{})

Info logs a message at level Info on the standard logger.

func Infof

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

Infof logs a message at level Info on the standard logger.

func Infoln

func Infoln(args ...interface{})

Infoln logs a message at level Info on the standard logger.

func InitLogging

func InitLogging(logName string, params *LogParams, alsoLogToStderr bool) (err error)

Initialize logging with given params

func IsLevelEnabled

func IsLevelEnabled(level log.Level) bool

IsLevelEnabled checks if the log level of the standard logger is greater than the level param

func IsSensitive

func IsSensitive(key string) bool

IsSensitive checks if the given key exists in the list of bad words (sensitive info)

func MapScrubber

func MapScrubber(m map[string]string) map[string]string

MapScrubber checks if the map contains any sensitive information like username/password/secret If found, then masks values for those keys, else copies the original value and returns new map

func Panic

func Panic(args ...interface{})

Panic logs a message at level Panic on the standard logger.

func Panicf

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

Panicf logs a message at level Panic on the standard logger.

func Panicln

func Panicln(args ...interface{})

Panicln logs a message at level Panic on the standard logger.

func Print

func Print(args ...interface{})

Print logs a message at level Info on the standard logger.

func Printf

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

Printf logs a message at level Info on the standard logger.

func Println

func Println(args ...interface{})

Println logs a message at level Info on the standard logger.

func Scrubber

func Scrubber(args []string) []string

Scrubber checks if the args list contains any sensitive information like username/password/secret If found, then returns masked string list, else returns the original input list unmodified.

func Trace

func Trace(args ...interface{})

Trace logs a message at level Trace on the standard logger.

func Tracef

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

Tracef logs a message at level Trace on the standard logger.

func Traceln

func Traceln(args ...interface{})

Traceln logs a message at level Trace on the standard logger.

func Warn

func Warn(args ...interface{})

Warn logs a message at level Warn on the standard logger.

func Warnf

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

Warnf logs a message at level Warn on the standard logger.

func Warning

func Warning(args ...interface{})

Warning logs a message at level Warn on the standard logger.

func Warningf

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

Warningf logs a message at level Warn on the standard logger.

func Warningln

func Warningln(args ...interface{})

Warningln logs a message at level Warn on the standard logger.

func Warnln

func Warnln(args ...interface{})

Warnln logs a message at level Warn on the standard logger.

func WithContext

func WithContext(ctx context.Context) *log.Entry

WithContext creates an entry from the standard logger and adds a context to it.

func WithError

func WithError(err error) *log.Entry

WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.

func WithField

func WithField(key string, value interface{}) *log.Entry

WithField creates an entry from the standard logger and adds a field to it. If you want multiple fields, use `WithFields`.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithFields

func WithFields(fields Fields) *log.Entry

WithFields creates an entry from the standard logger and adds multiple fields to it. This is simply a helper for `WithField`, invoking it once for each field.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithTime

func WithTime(t time.Time) *log.Entry

WithTime creats an entry from the standard logger and overrides the time of logs generated with it.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

Types

type ConsoleHook

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

ConsoleHook sends log entries to stdout.

func NewConsoleHook

func NewConsoleHook() *ConsoleHook

NewConsoleHook creates a new log hook for writing to stdout/stderr.

func (*ConsoleHook) Fire

func (hook *ConsoleHook) Fire(entry *log.Entry) error

func (*ConsoleHook) Levels

func (hook *ConsoleHook) Levels() []log.Level

type Fields

type Fields = log.Fields

type FileHook

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

FileHook sends log entries to a file.

func NewFileHook

func NewFileHook() (hook *FileHook, err error)

NewFileHook creates a new log hook for writing to a file.

func (*FileHook) Fire

func (hook *FileHook) Fire(entry *log.Entry) error

func (*FileHook) GetLocation

func (hook *FileHook) GetLocation() string

func (*FileHook) Levels

func (hook *FileHook) Levels() []log.Level

type LogParams

type LogParams struct {
	Level      string
	File       string
	MaxFiles   int
	MaxSizeMiB int
	Format     string
}

LogParams to configure logging

func (LogParams) GetFile

func (l LogParams) GetFile() string

func (LogParams) GetLevel

func (l LogParams) GetLevel() string

func (LogParams) GetLogFormat

func (l LogParams) GetLogFormat() string

func (LogParams) GetMaxFiles

func (l LogParams) GetMaxFiles() int

func (LogParams) GetMaxSize

func (l LogParams) GetMaxSize() int

func (LogParams) UseJsonFormatter

func (l LogParams) UseJsonFormatter() bool

func (LogParams) UseTextFormatter

func (l LogParams) UseTextFormatter() bool

Jump to

Keyboard shortcuts

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