log

package module
v0.0.0-...-87352bb Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2016 License: Apache-2.0 Imports: 5 Imported by: 37

README

Go logging rethinked

Circle CI Build Status Coverage Status Go Report Card

GoDoc

Usage

log.Println("some log") // unconditional log
log.Trace("trace") // log only with `trace` level
log.Tracef("42: %s", "yep") // each method has it's format alternative
log.Debug("debug") // log only with `debug` level and lower
log.Info("info") // log only with `info` level and lower
log.Warning("warn") // log with `warning` level and lower
log.Error("err") // log with `error` and `fatal` level
log.Fatal("haha") // log and panic("haha")

Log adds --log-level flag to your program:

// main.go
package main

import (
    "flag"

    "github.com/yanzay/log"
)

func main() {
    flag.Parse()
    log.Info("info")
}
$ go run main.go --help
Usage:
  -log-level string
        Log level: trace|debug|info|warning|error|fatal (default "info")

Advanced Usage

Log Level

You can set logging level manually by:

log.Level = log.LevelTrace
Log Writers
DefaultWriter

DefaultWriter is just a small wrapper for log package from stdlib.

AsyncWriter

You can use AsyncWriter to write your logs asynchronously, without blocking you main execution. To use AsyncWriter, just switch it in your code like this:

log.Writer = log.NewAsyncWriter()
Your Own Writer

Also you can use your own log writer:

log.Writer = myWriter // myWriter should implement io.Writer interface

Documentation

Overview

Package log is a logging package with levels and pluggable writers

Index

Constants

This section is empty.

Variables

View Source
var (
	// Level is current log level for logger
	Level = LevelInfo
	// Writer for writing logs to. You can change it for your own writer
	Writer io.Writer = DefaultWriter{}
)

Functions

func AddWriter

func AddWriter(writer io.Writer)

func Debug

func Debug(value interface{})

Debug logging

func Debugf

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

Debugf is formatted debug logging

func Error

func Error(value interface{})

Error logging

func Errorf

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

Errorf is formatted error logging

func Fatal

func Fatal(value interface{})

Fatal logs fatal error and panic

func Fatalf

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

Fatalf logs fatal error with format and panic

func Info

func Info(value interface{})

Info logging

func Infof

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

Infof is formatted info logging

func Printf

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

Printf is unconditional formatted log

func Println

func Println(value interface{})

Println is unconditional log

func Trace

func Trace(value interface{})

Trace logging. Use it for most detailed logs

func Tracef

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

Tracef is formatted trace logging

func Warning

func Warning(value interface{})

Warning logging

func Warningf

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

Warningf is formatted warning logging

Types

type AsyncWriter

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

AsyncWriter is asynchronous writer, writes in separate goroutine

func NewAsyncWriter

func NewAsyncWriter() *AsyncWriter

NewAsyncWriter creates writer and starts writing routine

func (*AsyncWriter) Write

func (aw *AsyncWriter) Write(v []byte) (int, error)

type DefaultWriter

type DefaultWriter struct{}

DefaultWriter is simple wrapper for log from stdlib

func (DefaultWriter) Write

func (dw DefaultWriter) Write(p []byte) (int, error)

type LogLevel

type LogLevel int
const (
	// LevelTrace is most detailed logging
	LevelTrace LogLevel = 1 + iota
	// LevelDebug is level for debugging logs
	LevelDebug
	// LevelInfo is level for info logs
	LevelInfo
	// LevelWarning is level for warning logs
	LevelWarning
	// LevelError is level for error logs
	LevelError
	// LevelFatal is logging only for fatal errors
	LevelFatal
)

func (*LogLevel) Set

func (ll *LogLevel) Set(value string) error

func (LogLevel) String

func (ll LogLevel) String() string

Jump to

Keyboard shortcuts

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