ezlog

package module
v0.0.0-...-03b4296 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2017 License: BSD-3-Clause Imports: 7 Imported by: 1

README

ezlog

GoDocBuild Status

ezlog is a leveled log library with an api similar to stdlib's log package. The main difference is that stdlib's log.Output method isn't exposed and there are additional log line output methods corresponding to the level of the log lines that they are writing.

Ezlog provides leveled log lines using the Error[f|ln], Info[f|ln], and Debug[f|ln] methods. Log lines will be prefixed with either the level name or the first character of the level name, depending on the logger's configuration. Log lines are only written for log levels less than or equal to the logger's configured level, all other lines are discarded. If the logger's level is set to LogNone, all output will be discarded.

Fatal[f|ln] and Panic[f|ln] methods that prefix the log lines with "FATAL" or "F" and "PANIC" or "P", respectively and then exits or panics are provided.

The log flags are the same as stdlib's constants for log flags.

Severity Levels

  • none
  • error
  • info
  • debug

Documentation

Overview

Package ezlog provides simple leveled log output using stdlib's log.Logger. A type Logger is defined with methods for leveled log line and formatted leveled log line output. For convenience, a 'standard' logger is available through the helper functions. The 'standard' logger's log level is LogError and writes output to stderr with LstdFlags.

In addition to LogNone, which discards all log lines, three common log levels are supported: error (LogError), info (LogInfo), and debug (LogDebug). Any log lines that are for log levels higher than the logger's log level are discarded.

Leveled log lines are written with the Error[f|ln], Info[f|ln], Debug[f|ln] methods. Aside from the leveled log lines, two other types of prefixed log lines can be written: Fatal[f|ln] and Panic[f|ln]. Log lines w/o levels can be written with the Print[f|ln] methods. These methods will always result in the log lines being written.

On Fatal and Panic calls, Logger can run functions prior to os.Exit or panic. These functions are added using the AddFunc call and are expected to have a func() error signature. Any errors that may occur during the execution of these functions will be ignored.

These functions can also be run by calling the Close method.

Index

Constants

View Source
const (
	// Bits or'ed together to control what's printed.
	// There is no control over the order they appear (the order listed
	// here) or the format they present (as described in the comments).
	// The prefix is followed by a colon only when Llongfile or Lshortfile
	// is specified.
	// For example, flags Ldate | Ltime (or LstdFlags) produce,
	//	2009/01/23 01:23:23 message
	// while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,
	//	2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
	Ldate         = 1 << iota     // the date in the local time zone: 2009/01/23
	Ltime                         // the time in the local time zone: 01:23:23
	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                     // full file name and line number: /a/b/c/d.go:23
	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
	LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
	LstdFlags     = Ldate | Ltime // initial values for the standard logger
)

These flags define which text to prefix to each log entry generated by the Logger.

Variables

This section is empty.

Functions

func AddFunc

func AddFunc(f func() error)

AddFunc adds a func to the standard logger that is to be run by the Close, Fatal, and Panic methods. Funcs will be run in the order they are added.

func Close

func Close()

Close runs any funcs that standard logger was given. Any errors that occurs during the execution of these funcs are ignored as this is expected to occur immediately before the application exits.

func Debug

func Debug(v ...interface{})

Debug writes a debug line to the standard logger. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Print.

func Debugf

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

Debugf writes a debug line to the standard logger using the provided format and data. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Printf.

func Debugln

func Debugln(v ...interface{})

Debugln writes a debug line to the stadard logger. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Println.

func Error

func Error(v ...interface{})

Error writes an error entry to the standard logger. If the logger's level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Print.

func Errorf

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

Errorf writes an error line to the standard logger using the provided format and data. If the level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Printf.

func Errorln

func Errorln(v ...interface{})

Errorln writes an error line to the standard logger. If the logger's level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Println.

func Fatal

func Fatal(v ...interface{})

Fatal writes a fatal line to the standard logger followed by a call to os.Exit(1). Arguments are handled in the manner of fmt.Print.

func Fatalf

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

Fatalf writes a fatal line to the standard logger using the provided format and data followed by a call to os.Exit(1).

func Fatalln

func Fatalln(v ...interface{})

Fatalln writes a fatal line to the standard logger followed by a call to os.Exit(1). Arguments are handled in the manner of fmt.Println.

func Flags

func Flags() int

Flags returns the standard logger's output flags.

func Info

func Info(v ...interface{})

Info writes an info line to the standard logger. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Print.

func Infof

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

Infof writes an info line to the standard logger using the provided format and data. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Printf.

func Infoln

func Infoln(v ...interface{})

Infoln writes an info line to the standard logger. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Println.

func Panic

func Panic(v ...interface{})

Panic writes a panic line to the standard logger followed by a call to panic(). Arguments are handled in the manner of fmt.Print.

func Panicf

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

Panicf writes a panic line to the standard logger using the provided format and data followed by a call to panic(). Arguments are handled in the manner of fmt.Printf.

func Panicln

func Panicln(v ...interface{})

Panicln writes a panic line to the standard logger followed by a call to panic(). Arguments are handled in the manner of fmt.Println.

func ParseFlag

func ParseFlag(s string) (l int, err error)

ParseFlag returns the log Flag for s. A match is found when s is equal to either the name of a Flag constant or the name of a Flag constant without the leading 'l', e.g. both "lstdflags" and "stdflags" will return the value for the LstdFlags constant. The match is case-insensitve. Empty string is treated as wanting the default LstdFlags; returning LstdFlags. An UnknownFlagError is returned if no match for s is found.

func Prefix

func Prefix() string

Prefix returns the standrd logger's prefix.

func Print

func Print(v ...interface{})

Print writes a log line to the standard logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Print.

func Printf

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

Printf writes a log line to the standard logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(v ...interface{})

Println writes a log line to the standard logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Println.

func SetFlags

func SetFlags(flags int)

SetFlags sets the standard logger's flags.

func SetLevel

func SetLevel(i Level)

SetLevel sets the maximum level for the standard logger's log output. Any log lines whose levels are higher than the standard logger's level will be discarded.

func SetLevelStringType

func SetLevelStringType(v LevelStringType)

SetLevelStringType sets what the standard logger should use for the level name (string) in log lines.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the standard logger's output destination.

func SetPrefix

func SetPrefix(prefix string)

SetPrefix sets the standard logger's prefix.

Types

type Level

type Level int32 // for sync.Atomic.Int32

Level: log levels.

const (
	LogNone  Level = iota + 1 // no logging
	LogError                  // log Error lines.
	LogInfo                   // log Info and Error lines.
	LogDebug                  // log Debug, Info, and Error lines.

)

func GetLevel

func GetLevel() Level

GetLevel returns the standard logger's level.

func LevelByName

func LevelByName(s string) (level Level, ok bool)

LevelByName gets the Level corresponding to s. A false will be returned if s doesn't match any Levels. S is uppper-cased prior to evaluation.

Supported values:

LogNone:   none, n, empty string ("")
LogError:  error, e, error
LogInfo:   info, i, inf
LogDebug:  debug, d, dbg

func (Level) String

func (l Level) String() string

type LevelStringType

type LevelStringType int

LevelStringType is the type of string output that will be used for the log level.

const (
	Full  LevelStringType = iota // use the level's full name
	Short                        // use the level's short description
	Char                         // use the first character of the level's name
)

func GetLevelStringType

func GetLevelStringType() LevelStringType

GetLevelStringType returns what the standard logger is using for the error level name (string) in log lines.

type Logger

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

Logger generates leveled log lines of output to an io.Writer if the log level is <= the logger's level. This is safe for concurrent use.

func New

func New(level Level, levelStringType LevelStringType, out io.Writer, prefix string, flag int) *Logger

New creates a new Logger. The level argument sets the Logger's log level. The levelStringType is what should be used for the level's name: the first character, Char, the short version, Short, or the full name, Full. The out argument sets the log data output destination. The prefix argument sets what each line will start with. The flag argument sets the logger's properties.

func (*Logger) AddFunc

func (l *Logger) AddFunc(f func() error)

AddFunc adds a func to the logger that is to be run by the Close, Fatal, and Panic methods. Funcs will be run in the order they are added.

func (*Logger) Close

func (l *Logger) Close()

Close runs any funcs that the logger was given. Any errors that occurs during the execution of these funcs are ignored as this is expected to occur immediately before the application exits.

func (*Logger) Debug

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

Debug writes a debug line to the logger. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Print.

func (*Logger) Debugf

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

Debugf writes a debug line to the logger using the provided format and data. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Debugln

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

Debugln writes a debug line to the logger. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Println.

func (*Logger) Error

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

Error writes an error line to the logger. If the logger's level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Print.

func (*Logger) Errorf

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

Errorf writes an error line to the logger using the provided format and data. If the level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Errorln

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

Errorln writes an error line to the logger. If the logger's level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Println.

func (*Logger) Fatal

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

Fatal writes a fatal line to the logger followed by a call to os.Exit(1). Arguments are handled in the manner of fmt.Print.

func (*Logger) Fatalf

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

Fatalf writes a fatal line to the logger using the provided format and data followed by a call to os.Exit(1).

func (*Logger) Fatalln

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

Fatalln writes a fatal line to the logger followed by a call to os.Exit(1). Arguments are handled in the manner of fmt.Println.

func (*Logger) Flags

func (l *Logger) Flags() int

Flags returns the logger's output flags.

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

GetLevel returns the logger's level.

func (*Logger) GetLevelStringType

func (l *Logger) GetLevelStringType() LevelStringType

GetLevelStringType returns what the logger is using for the error level name (string) in log lines.

func (*Logger) Info

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

Info writes an info entry to the logger. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Print.

func (*Logger) Infof

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

Infof writes an info line to the logger using the provided format and data. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Infoln

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

Infoln writes an info entry to the logger. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Println.

func (*Logger) Panic

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

Panic writes a panic line to the logger followed by a call to panic(). Arguments are handled in the manner of fmt.Print.

func (*Logger) Panicf

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

Panicf writes a panic line to the logger using the provided format and data followed by a call to panic().

func (*Logger) Panicln

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

Panicln writes a panic line to the logger followed by a call to panic(). Arguments are handled in the manner of fmt.Println.

func (*Logger) Prefix

func (l *Logger) Prefix() string

Prefix returns the logger's prefix.

func (*Logger) Print

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

Print writes a log line to the logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

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

Printf writes a log line to the logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Println

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

Println writes a log line to the logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Println.

func (*Logger) SetFlags

func (l *Logger) SetFlags(flags int)

SetFlags sets the logger's flags.

func (*Logger) SetLevel

func (l *Logger) SetLevel(i Level)

SetLevel sets the maximum level for the logger's output. Any log lines whose levels are higher than the logger's level will be discarded.

func (*Logger) SetLevelStringType

func (l *Logger) SetLevelStringType(v LevelStringType)

SetLevelStringType sets what the logger should use for the level name (string) in log lines.

func (*Logger) SetOutput

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

SetOutput sets the logger's output.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets the logger's prefix.

type UnknownFlagError

type UnknownFlagError struct {
	S string // The string that could not be parsed to a valid Flag.
}

UnknownFlagError occurs when a string cannot be parsed into a log Flag.

func (UnknownFlagError) Error

func (e UnknownFlagError) Error() string

Jump to

Keyboard shortcuts

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