picolo

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2018 License: MIT Imports: 5 Imported by: 2

README

MIT License Tag godoc Go Report

picolo

Minimalistic logging library.

Levels

LevelDebug
LevelInfo
LevelWarning
LevelError

Options

The constructor accepts several options:

WithLevel(level Level)                     // Set level
WithOutput(output io.Writer)               // Set output
WithPrefix(prefix string)                  // Set prefix
WithTimeFormat(format string, utc bool)    // Set time format and UTC flag

Defaults

If no options are given, the following are assumed.

WithLevel(LevelInfo)
WithOutput(os.Stdout)
WithTimeFormat(DefaultTimeFormat, true)

The default time format is 2006-01-02 15:04:05.000.

Usage

l := picolo.New() // Use defaults
l.Infof("Info message")
// 2017-12-21 22:23:24.256 INFO Info message

l = picolo.New(picolo.WithPrefix("[some-prefix]")) // constructor with optional prefix
l.Infof("Info message")
// 2017-12-21 22:23:24.256 INFO [some-prefix] Info message

// Create sub-logger, appending prefix
k := picolo.NewFrom(l, "[more-prefix]")
k.Errorf("Error message: %v", err)
//  2017-12-21 23:24:25.267 ERROR [some-prefix] [more-prefix] Error message: No such file or directory

Helpers

Use picolo.LevelFromString to parse a string into a log level. This can be used like:

// ...
l := flag.String("logLevel", "debug", "Log level")
flag.Parse()

lvl, err := picolo.LevelFromString(*l)
if err != nil {
	// Unknown log level
}

logger := picolo.New(picolo.WithLevel(lvl))
logger.Infof("One two three")
// ...

Documentation

Index

Constants

View Source
const DefaultTimeFormat = "2006-01-02 15:04:05.000"

DefaultTimeFormat is our default time format

Variables

View Source
var ErrUnknownLevel = fmt.Errorf("Unknown log level")

ErrUnknownLevel is returned by LevelFromString

Functions

This section is empty.

Types

type Level

type Level int

Level is the log level

const (
	// Log levels
	LevelDebug Level = iota + 1
	LevelInfo
	LevelWarning
	LevelError
)

func LevelFromString

func LevelFromString(s string) (Level, error)

LevelFromString returns the log level from string representation

func (Level) String

func (l Level) String() string

String returns the log level in string representation

type Logger

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

Logger is our logger struct

func New

func New(opts ...Option) *Logger

New creates a new Logger

func NewFrom

func NewFrom(from *Logger, morePrefix string) *Logger

NewFrom creates a new logger from a logger, appending the prefix

func (*Logger) Debugf

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

Debugf logs formatted message in debug level

func (*Logger) Errorf

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

Errorf logs formatted message in error level

func (*Logger) Infof

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

Infof logs formatted message in info level

func (*Logger) SetLogLevel

func (l *Logger) SetLogLevel(s string) error

func (*Logger) Warningf

func (l *Logger) Warningf(format string, a ...interface{})

Warningf logs formatted message in warning level

type Option

type Option func(*options)

Option is our option type

func WithLevel

func WithLevel(level Level) Option

WithLevel sets the log level

func WithOutput

func WithOutput(output io.Writer) Option

WithOutput sets log output

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets a log prefix

func WithTimeFormat

func WithTimeFormat(format string, utc bool) Option

WithTimeFormat sets the time format. Specify empty time format to disable datetime in logs.

Jump to

Keyboard shortcuts

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