wlog

package module
v0.0.0-...-7c63b0a Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2016 License: MIT Imports: 5 Imported by: 85

README

wlog

Simple log level based Go logger. Provides an io.Writer that filters log messages based on a log level prefix. Valid log levels are: DEBUG, INFO, WARN, ERROR, OFF. Log messages need to begin with a L! where L is one of D, I, W, or E.

Usage

Create a *log.Logger via wlog.New:

package main

import (
    "log"
    "os"

    "github.com/influxdata/wlog"
)

func main() {
    var logger *log.Logger
    logger = wlog.New(os.Stderr, "prefix", log.LstdFlags)
    logger.Println("I! initialized logger")
}

Create a *log.Logger explicitly using wlog.Writer:

package main

import (
    "log"
    "os"

    "github.com/influxdata/wlog"
)

func main() {
    var logger *log.Logger
    logger = log.New(wlog.NewWriter(os.Stderr), "prefix", log.LstdFlags)
    logger.Println("I! initialized logger")
}

Prefix log messages with a log level char and the ! delimiter.

logger.Println("D! this is a debug log")
logger.Println("I! this is an info log")
logger.Println("W! this is a warn log")
logger.Println("E! this is an error log")

The log level can be changed via the SetLevel or the SetLevelFromName functions.

package main

import (
    "log"
    "os"

    "github.com/influxdata/wlog"
)

func main() {
    var logger *log.Logger
    logger = wlog.New(os.Stderr, "prefix", log.LstdFlags)
    wlog.SetLevel(wlog.DEBUG)
    logger.Println("D! initialized logger")
    wlog.SetLevelFromName("INFO")
    logger.Println("D! this message will be dropped")
    logger.Println("I! this message will be printed")
}

Documentation

Overview

Provides an io.Writer that filters log messages based on a log level.

Valid log levels are: DEBUG, INFO, WARN, ERROR.

Log messages need to begin with a L! where L is one of D, I, W, or E.

Examples:

log.Println("D! this is a debug log")
log.Println("I! this is an info log")
log.Println("W! this is a warn log")
log.Println("E! this is an error log")

Simply pass a instance of wlog.Writer to log.New or use the helper wlog.New function.

The log level can be changed via the SetLevel or the SetLevelFromName functions.

Index

Constants

View Source
const Delimiter = '!'

Variables

View Source
var Levels = map[byte]Level{
	'D': DEBUG,
	'I': INFO,
	'W': WARN,
	'E': ERROR,
}
View Source
var ReverseLevels map[Level]byte
View Source
var StringToLevel = map[string]Level{
	"DEBUG": DEBUG,
	"INFO":  INFO,
	"WARN":  WARN,
	"ERROR": ERROR,
	"OFF":   OFF,
}

name to Level mappings

Functions

func New

func New(w io.Writer, prefix string, flag int) *log.Logger

Create a new *log.Logger wrapping w in a wlog.Writer

func SetLevel

func SetLevel(l Level)

Set the current logging Level.

func SetLevelFromName

func SetLevelFromName(level string) error

Set the log level via a string name. To set it directly use 'logLevel'.

Types

type Level

type Level int
const (
	DEBUG Level
	INFO
	WARN
	ERROR
	OFF
)

func LogLevel

func LogLevel() Level

Retrieve the current logging Level.

type StaticLevelWriter

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

StaticLevelWriter prefixes all log messages with a static log level.

func NewStaticLevelWriter

func NewStaticLevelWriter(w io.Writer, level Level) *StaticLevelWriter

Create a writer that always append a static log prefix to all messages. Usefult for supplying a *log.Logger to a package that doesn't prefix log messages itself.

func (*StaticLevelWriter) Write

func (w *StaticLevelWriter) Write(buf []byte) (int, error)

type Writer

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

Implements io.Writer. Checks first byte of write for log level and drops the log if necessary

func NewWriter

func NewWriter(w io.Writer) *Writer

Create a new wlog.Writer wrapping w.

func (*Writer) Write

func (w *Writer) Write(buf []byte) (int, error)

Implements the io.Writer method.

Jump to

Keyboard shortcuts

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