logg

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2020 License: MIT Imports: 11 Imported by: 2

README

logg

GoDoc Tests codecov



Better log experience in golang.

Installation

go get github.com/pkgz/logg

Features

  • color logs
  • different output formats (pretty/json)
  • different levels (debug/info/error/warning/panic)
  • can be used with internal log library
  • zero allocations

Usage

There is two way how you can use this logger:

  • install globally (this option will set own writer to default log);
  • using a local logger instance;
Global logger

In this mode, the logger will set own writer to log the library. So you can use the default log library for logging. But with better output.

package main

import (
    "github.com/pkgz/logg"
    "log"
    "os"
)

func main () {
    logg.NewGlobal(os.Stdout)

    log.Print("ERR some message to log")
}
Local logger

In this case, you must define a log instance in each context (function). But in this case, you can define different settings for different scopes.

package main

import (
    "github.com/pkgz/logg"
    "os"
)

func main () {
    log := logg.New(os.Stdout)

    log.Print("ERR some message to log")
}
Json log
package main

import (
    "github.com/pkgz/logg"
    "os"
)

func main () {
    log := logg.New(os.Stdout)
    log.SetFormat(logg.Json)

    log.Print("ERR some message to log")
}
Settings

There are a few parameters which you can set:

  • flags (define time and caller format. Using the format from internal log library)
  • format (output log format. Pretty or Json)
  • color (colorize output or not)
Levels
  • Debug: DBG | DEBUG | [DBG] | [DEBUG]
  • Info: INF | INFO | [INF] | [INFO]
  • Error: ERR | ERROR | [ERR] | [ERROR]
  • Warning: WRN | WARN | [WRN] | [WARN]
  • Panic: PNC | PANIC | [PNC] | [PANIC]
API
Function Default Description
SetWriter(io.Writer) ioutil.Discard Set writer.
SetFormat(logg.format) Pretty Set output format. Can be pretty or json.
SetFlags(int) int Set time and caller flags.
MinLevel(level) Info Minimum level for logs. Logs lower this level will be not writed.
ToggleColor(bool) true Enable or disable output colorizing.
DebugMode() Will enable a debug mode. Debug mode will add milliseconds to timestamp and log caller.

Benchmarks

BenchmarkLog_Print/long_message-8         	 1799361	       703 ns/op	     592 B/op	       2 allocs/op
BenchmarkLog_Print/short_message-8        	 2444176	       491 ns/op	      80 B/op	       2 allocs/op
BenchmarkLog_Print/short_level-8          	 2451745	       494 ns/op	      80 B/op	       2 allocs/op
BenchmarkLog_Print/long_level-8           	 2364008	       493 ns/op	      80 B/op	       2 allocs/op
BenchmarkLogg_Log_Print/long_message-8    	 1443676	       825 ns/op	     593 B/op	       2 allocs/op
BenchmarkLogg_Log_Print/short_message-8   	 1701645	       713 ns/op	      80 B/op	       2 allocs/op
BenchmarkLogg_Log_Print/short_level-8     	 1882834	       636 ns/op	      80 B/op	       2 allocs/op
BenchmarkLogg_Log_Print/long_level-8      	 1862190	       676 ns/op	      80 B/op	       2 allocs/op
BenchmarkLogg_Write_Pretty/long_message-8 	13835029	        90.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogg_Write_Pretty/short_message-8         	13607654	        86.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogg_Write_Pretty/short_level-8           	16418979	        73.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogg_Write_Pretty/long_level-8            	15362362	        76.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogg_Write_Json/long_message-8            	11209905	       107 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogg_Write_Json/short_message-8           	11571448	       105 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogg_Write_Json/short_level-8             	13749747	        87.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogg_Write_Json/long_level-8              	13415761	        89.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogg_Print/long_message-8                 	 4293176	       311 ns/op	    1170 B/op	       3 allocs/op
BenchmarkLogg_Print/short_message-8                	 7748691	       155 ns/op	     144 B/op	       3 allocs/op
BenchmarkLogg_Print/short_level-8                  	 8459841	       141 ns/op	     144 B/op	       3 allocs/op
BenchmarkLogg_Print/long_level-8                   	 8349727	       143 ns/op	     144 B/op	       3 allocs/op
BenchmarkLogg_Printf/long_message-8                	 3314443	       358 ns/op	    1154 B/op	       2 allocs/op
BenchmarkLogg_Printf/short_message-8               	 7906260	       153 ns/op	     128 B/op	       2 allocs/op
BenchmarkLogg_Printf/short_level-8                 	 8624889	       144 ns/op	     128 B/op	       2 allocs/op
BenchmarkLogg_Printf/long_level-8                  	 8253075	       145 ns/op	     128 B/op	       2 allocs/op

Licence

MIT License

Documentation

Index

Constants

View Source
const (
	DefaultFormat       = Pretty
	DefaultColorOutput  = true
	DefaultFlags        = LstdFlags
	DefaultMinimumLevel = Info

	ContextCallDepth = 4
)

Default parameters

View Source
const (
	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. Bits are 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
View Source
const (
	Pretty format = iota
	Json
)

Output formats

View Source
const (
	Debug level = iota
	Info
	Error
	Warning
	Panic

	Empty level = -1
)

Log levels.

View Source
const (
	Black int = iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)

Base colors for console

View Source
const (
	HiBlack int = iota + 60
	HiRed
	HiGreen
	HiYellow
	HiBlue
	HiMagenta
	HiCyan
	HiWhite
)

High intensity colors

View Source
const (
	Reset int = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)

Base attributes

Variables

This section is empty.

Functions

func DebugMode added in v0.3.0

func DebugMode()

func MinLevel added in v0.3.3

func MinLevel(level level)

func NewGlobal added in v0.3.0

func NewGlobal(w io.Writer)

Allows to create a new global logger.

func Print added in v0.3.0

func Print(args ...interface{})

func Printf added in v0.3.0

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

func SetFlags

func SetFlags(flags int)

func SetFormat

func SetFormat(format format)

func SetWriter added in v0.3.0

func SetWriter(w io.Writer)

func ToggleColor added in v0.3.0

func ToggleColor(value bool)

Types

type Logg

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

A Logg represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Logg's Write method.

func New added in v0.3.0

func New(w io.Writer) *Logg

Create new a new logg.

func (*Logg) Debug added in v0.3.0

func (l *Logg) Debug(args ...interface{})

func (*Logg) DebugMode added in v0.3.0

func (l *Logg) DebugMode()

func (*Logg) Debugf added in v0.3.0

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

func (*Logg) Error added in v0.3.0

func (l *Logg) Error(args ...interface{})

func (*Logg) Errorf added in v0.3.0

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

func (*Logg) Info added in v0.3.0

func (l *Logg) Info(args ...interface{})

func (*Logg) Infof added in v0.3.0

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

func (*Logg) MinLevel added in v0.3.0

func (l *Logg) MinLevel(level level)

func (*Logg) Panic added in v0.3.0

func (l *Logg) Panic(args ...interface{})

func (*Logg) Panicf added in v0.3.0

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

func (*Logg) Print added in v0.3.0

func (l *Logg) Print(args ...interface{})

func (*Logg) Printf added in v0.3.0

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

func (*Logg) SetFlags added in v0.3.0

func (l *Logg) SetFlags(flags int)

func (*Logg) SetFormat added in v0.3.0

func (l *Logg) SetFormat(format format)

func (*Logg) SetWriter added in v0.3.0

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

func (*Logg) ToggleColor added in v0.3.0

func (l *Logg) ToggleColor(value bool)

func (*Logg) Warn added in v0.3.0

func (l *Logg) Warn(args ...interface{})

func (*Logg) Warnf added in v0.3.0

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

func (*Logg) Write

func (l *Logg) Write(b []byte) (n int, err error)

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-nil error if it returns n < len(p).

func (*Logg) Writer added in v0.3.0

func (l *Logg) Writer() io.Writer

Writer returns the output destination for the standard logger.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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