llog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2022 License: MIT Imports: 3 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Simple Leveled Logger

Documentation Go Report Card

github.com/andrewslotin/llog is a library that provides leveled logs to the Go standard library's log.Logger.

Installation

go get github.com/andrewslotin/llog

Usage

The leveled logger is implemented as an io.Writer to be provided to the stdlib's log.SetOutput(). The writer determines log record level by looking at the message prefix. For example, a message that starts with warn will be considered as a llog.WarnLevel and sent to the underlying writer, if the writer has the minimal level set to llog.WarnLevel or lower.

// configure standard logger to write warning messages and above to STDERR
log.SetOutput(llog.NewWriter(os.Stderr, llog.WarnLevel))

log.Printf("warn: failed to close the output file: %s", err) // warn level, will be printed out
log.Println("something just happened") // info level, will be ignored

If a message does not contain any known prefixes, its level is considered to be llog.InfoLevel.

Please consult the package documentation for the list of available log levels as well as how they are mapped to the message prefixes.

Performance

There is nearly no overhead in using the llog.Writer to write log messages.

Below is the benchstat output comparing the benchmarks of log.Println() with io.Discard and llog.Writer set as outputs. The llog.Writer is configured to write to io.Discard as well with the minimal log level sufficient to print out all messages used in the benchmark.

name            old time/op    new time/op    delta
Writer_Write-4     306ns ± 0%     362ns ± 0%   ~     (p=1.000 n=1+1)

name            old alloc/op   new alloc/op   delta
Writer_Write-4     16.0B ± 0%     16.0B ± 0%   ~     (all equal)

name            old allocs/op  new allocs/op  delta
Writer_Write-4      1.00 ± 0%      1.00 ± 0%   ~     (all equal)

Documentation

Overview

Example

This example demonstrates the use of llog.NewWriter() to filter out log messages based on their importance levels.

package main

import (
	"errors"
	"log"
	"os"

	"github.com/andrewslotin/llog"
)

func main() {
	// configure standard logger to write warning messages and above to STDERR
	log.SetOutput(llog.NewWriter(os.Stdout, llog.WarnLevel))

	// error level, will be printed out
	log.Println("error reading file")
	// warn level, will be printed out
	log.Printf("warn: failed to close the output file: %s", errors.New("no such file"))
	// info level, will be ignored
	log.Println("something just happened")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Level

type Level int8

Level is the log level used by the Writer.

const (
	FatalLevel Level = iota - 1
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
)

Valid log levels for the Writer

func (Level) String

func (l Level) String() string

String returns the string representation of the log level.

type Writer

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

Writer wraps an io.Writer filtering out log messages based on the current log level.

func NewWriter

func NewWriter(out io.Writer, lvl Level) *Writer

NewWriter initializes a new Writer with the given io.Writer and log level.

func (*Writer) SetLevel

func (w *Writer) SetLevel(lvl Level)

SetLevel sets the log level of the Writer.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write determines the log level of the message and writes it to the underlying io.Writer if the log level is greater than or equal to the Writer's log level. The log level of the message is determined by the case-sensitive message prefix: * messages starting with "missing" or "fatal" are considered fatal errors * messages starting with "error" or "failed" are considered errors * messages starting with "warn" are considered warnings * messages starting with "debug:" (including the colon) are considered debug messages * anything else is considered to be an info

Jump to

Keyboard shortcuts

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