logo

package module
Version: v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2020 License: MIT Imports: 6 Imported by: 2

README

A simple logging library for golang.

banner

I wanted to have a logger that is easy configurable with various io.Writers and a simple active and color on / off switch, so I created it.

Build Status Go Report Card Coverage cover.run go GoDoc MIT License

Explanation

A Logger object can hold multiple Receivers. Every Receiver holds an io.Writer object (f.e. os.File, os.Stderr), a Level which is minimal the log level that is logged, a boolean Active which says if the Receiver should log or not, and a boolean Color which prints output colored if turned on. Every Receiver has also its own log format.

Initilization

First you have to install the package.

go get -u github.com/mbndr/logo

After that you can import it.

import "github.com/mbndr/logo"
Simple

It's possible to create a simple logger with a io.Writer, the log level and a color boolean as parameter.

// Create a simple cli logger with activated colors which logs everything
log := logo.NewSimpleLogger(os.Stderr, logo.DEBUG, "prefix ", true)
Advanced

You can create multiple Receivers and add it to a new Logger.

// Receiver for the terminal which logs everything
cliRec := logo.NewReceiver(os.Stderr, "prefix ")
cliRec.Color = true
cliRec.Level = logo.DEBUG

// Helper function to get a os.File with the correct options
logFile, _ := logo.Open("./example/logo.log")

// Receiver for the log file
// This will log with level INFO (default) and have no colors activated
// Also the log format is simpler (f.e. ERRO: Message)
fileRec := logo.NewReceiver(logFile, "prefix ")
fileRec.Format = "%s: %s"

// Create the logger
log := logo.NewLogger(cliRec, fileRec)

Usage

If you created you logo.Logger object, there are a few methods you can use to log.

// Methods which write like log.Println()
log.Debug("First debug", " and another string to log")
log.Info("Information")
log.Warn("Warning", " message")
log.Error("Error message")
log.Fatal("Fatal error", " because of something")

// Methods which write like log.Printf()
log.Debugf("Debug value %d", 16)
log.Infof("Listening on port %d", 8080)
log.Warnf("Invalid user %s", user.Name)
log.Errorf("Couldn't load config file: %s", path)
log.Fatalf("Fatal error: %s", err.Error())

// Disable the logger
log.Active = false

Tests

There are a few unit tests written for this library. To run them cd into the project directory and run this.

go test -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(path string) (*os.File, error)

Open is a short function to open a file with needed options

Types

type Level

type Level int

Level is a log level

const (
	DEBUG Level = iota
	INFO
	WARN
	ERROR
	FATAL
)

Available log levels

func Itol

func Itol(level int) Level

Itol converts an integer to a logo.Level TODO is it possible to cast somehow?

type Logger

type Logger struct {
	Receivers []*Receiver
	Active    bool
}

Logger holds all Receivers

func NewLogger

func NewLogger(recs ...*Receiver) *Logger

NewLogger returns a new Logger filled with given Receivers

func NewSimpleLogger

func NewSimpleLogger(w io.Writer, lvl Level, prefix string, color bool) *Logger

NewSimpleLogger returns a logger with one simple Receiver

func (*Logger) Debug

func (l *Logger) Debug(a ...interface{})

Debug logs arguments

func (*Logger) Debugf

func (l *Logger) Debugf(format string, a ...interface{})

Debugf logs formated arguments

func (*Logger) Error

func (l *Logger) Error(a ...interface{})

Error logs arguments

func (*Logger) Errorf

func (l *Logger) Errorf(format string, a ...interface{})

Errorf logs formated arguments

func (*Logger) Fatal

func (l *Logger) Fatal(a ...interface{})

Fatal logs arguments

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, a ...interface{})

Fatalf logs formated arguments

func (*Logger) Info

func (l *Logger) Info(a ...interface{})

Info logs arguments

func (*Logger) Infof

func (l *Logger) Infof(format string, a ...interface{})

Infof logs formated arguments

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl Level)

SetLevel sets the log level of ALL receivers

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets the prefix of ALL receivers

func (*Logger) Warn

func (l *Logger) Warn(a ...interface{})

Warn logs arguments

func (*Logger) Warnf

func (l *Logger) Warnf(format string, a ...interface{})

Warnf logs formated arguments

type Receiver

type Receiver struct {
	Level  Level
	Color  bool
	Active bool
	Format string
	// contains filtered or unexported fields
}

Receiver holds all receiver options

func NewReceiver

func NewReceiver(w io.Writer, prefix string) *Receiver

NewReceiver returns a new Receiver object with a given Writer and sets default values

func (*Receiver) SetPrefix

func (r *Receiver) SetPrefix(prefix string)

SetPrefix sets the prefix of the logger. If a prefix is set and no trailing space is written, write one

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL