logutils

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MPL-2.0 Imports: 4 Imported by: 24

README

logutils

This package is a fork of github.com/hashicorp/logutils.

logutils is a Go package that augments the standard library "log" package to make logging a bit more modern, without fragmenting the Go ecosystem with new logging packages.

The simplest thing that could possibly work

Presumably your application already uses the default log package. To switch, you'll want your code to look like the following:

package main

import (
	"log"
	"os"

	"github.com/fujiwara/logutils"
)

func main() {
	filter := &logutils.LevelFilter{
		Levels: []logutils.LogLevel{"DEBUG", "WARN", "ERROR"},
		MinLevel: logutils.LogLevel("WARN"),
		Writer: os.Stderr,
	}
	log.SetOutput(filter)

	log.Print("[DEBUG] Debugging") // this will not print
	log.Print("[WARN] Warning") // this will
	log.Print("[ERROR] Erring") // and so will this
	log.Print("Message I haven't updated") // and so will this
}

This logs to standard error exactly like go's standard logger. Any log messages you haven't converted to have a level will continue to print as before.

Color logging

package main

import (
	"log"
	"os"

	"github.com/fujiwara/logutils"
	"github.com/fatih/color"
)

func main() {
	filter := &logutils.LevelFilter{
		Levels: []logutils.LogLevel{"DEBUG", "WARN", "ERROR"},
		ModifierFuncs: []logutils.ModifierFunc{
			nil, // default
			logutils.Color(color.FgYellow),
			logutils.Color(color.FgRed, color.BgBlack),
		},
		MinLevel: logutils.LogLevel("WARN"),
		Writer: os.Stderr,
	}
	log.SetOutput(filter)

	log.Print("[DEBUG] Debugging") // this will not print
	log.Print("[WARN] Warning") // this will print as yellow font
	log.Print("[ERROR] Erring") // this will print as red font and black background
	log.Print("Message I haven't updated") // this will print as default font(no color)
}

Documentation

Overview

Package logutils augments the standard log package with levels.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LevelFilter

type LevelFilter struct {
	// Levels is the list of log levels, in increasing order of
	// severity. Example might be: {"DEBUG", "WARN", "ERROR"}.
	Levels []LogLevel

	// ModifierFuncs is the list of modifier functions to apply to each log lines.
	ModifierFuncs []ModifierFunc

	// MinLevel is the minimum level allowed through
	MinLevel LogLevel

	// The underlying io.Writer where log messages that pass the filter
	// will be set.
	Writer io.Writer
	// contains filtered or unexported fields
}

LevelFilter is an io.Writer that can be used with a logger that will filter out log messages that aren't at least a certain level.

Once the filter is in use somewhere, it is not safe to modify the structure.

func (*LevelFilter) Check

func (f *LevelFilter) Check(line []byte) bool

Check will check a given line if it would be included in the level filter.

func (*LevelFilter) SetMinLevel

func (f *LevelFilter) SetMinLevel(min LogLevel)

SetMinLevel is used to update the minimum log level

func (*LevelFilter) Write

func (f *LevelFilter) Write(p []byte) (n int, err error)

type LogLevel

type LogLevel string

type ModifierFunc

type ModifierFunc func([]byte) []byte

func Color

func Color(attr ...color.Attribute) ModifierFunc

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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