Skip to content

peak/picolo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MIT License Tag godoc Go Report Card Build Status

picolo

picolo is a minimalistic logging library for go.

Usage

If no options are given, picolo assumes:

  • log level is INFO
  • output is os.Stdout
  • time format is 2006-01-02 15:04:05.000
  • prefix is empty string
l := picolo.New() // Use defaults
l.Infof("Info message")
// 2019-04-29 15:34:32.166 INFO Info message

prefix can be set with picolo.WithPrefix option.

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

Sub loggers can be created from an existing logger with picolo.NewFrom constructor function.

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

Log Levels

picolo supports 4 types of log levels:

  • DEBUG
  • INFO
  • WARNING
  • ERROR

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

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("Logger is ready.")
// ...