logging

package module
Version: v0.0.0-...-1a693bd Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2019 License: MPL-2.0 Imports: 6 Imported by: 62

README

Logging

A simple leveled logging library with coloured output.

Travis Status for RichardKnop/logging godoc for RichardKnop/logging


Log levels:

  • INFO (blue)
  • WARNING (pink)
  • ERROR (red)
  • FATAL (red)

Formatters:

  • DefaultFormatter
  • ColouredFormatter

Example usage. Create a new package log in your app such that:

package log

import (
	"github.com/RichardKnop/logging"
)

var (
	logger = logging.New(nil, nil, new(logging.ColouredFormatter))

	// INFO ...
	INFO = logger[logging.INFO]
	// WARNING ...
	WARNING = logger[logging.WARNING]
	// ERROR ...
	ERROR = logger[logging.ERROR]
	// FATAL ...
	FATAL = logger[logging.FATAL]
)

Then from your app you could do:

package main

import (
	"github.com/yourusername/yourapp/log"
)

func main() {
	log.INFO.Print("log message")
}

Documentation

Index

Constants

View Source
const (
	// DEBUG level
	DEBUG level = iota
	// INFO level
	INFO
	// WARNING level
	WARNING
	// ERROR level
	ERROR
	// FATAL level
	FATAL
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ColouredFormatter

type ColouredFormatter struct {
}

ColouredFormatter colours log messages with ASCI escape codes and adds filename and line number before the log message See https://en.wikipedia.org/wiki/ANSI_escape_code

func (*ColouredFormatter) Format

func (f *ColouredFormatter) Format(lvl level, v ...interface{}) []interface{}

Format adds filename and line number before the log message

func (*ColouredFormatter) GetPrefix

func (f *ColouredFormatter) GetPrefix(lvl level) string

GetPrefix returns colour escape code

func (*ColouredFormatter) GetSuffix

func (f *ColouredFormatter) GetSuffix(lvl level) string

GetSuffix returns reset sequence code

type DefaultFormatter

type DefaultFormatter struct {
}

DefaultFormatter adds filename and line number before the log message

func (*DefaultFormatter) Format

func (f *DefaultFormatter) Format(lvl level, v ...interface{}) []interface{}

Format adds filename and line number before the log message

func (*DefaultFormatter) GetPrefix

func (f *DefaultFormatter) GetPrefix(lvl level) string

GetPrefix returns ""

func (*DefaultFormatter) GetSuffix

func (f *DefaultFormatter) GetSuffix(lvl level) string

GetSuffix returns ""

type Formatter

type Formatter interface {
	GetPrefix(lvl level) string
	Format(lvl level, v ...interface{}) []interface{}
	GetSuffix(lvl level) string
}

Formatter interface

type Logger

type Logger map[level]LoggerInterface

Logger ...

func New

func New(out, errOut io.Writer, f Formatter) Logger

New returns instance of Logger

type LoggerInterface

type LoggerInterface interface {
	Print(...interface{})
	Printf(string, ...interface{})
	Println(...interface{})

	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fatalln(...interface{})

	Panic(...interface{})
	Panicf(string, ...interface{})
	Panicln(...interface{})
}

LoggerInterface will accept stdlib logger and a custom logger. There's no standard interface, this is the closest we get, unfortunately.

type Wrapper

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

Wrapper ...

func (*Wrapper) Fatal

func (w *Wrapper) Fatal(v ...interface{})

Fatal ...

func (*Wrapper) Fatalf

func (w *Wrapper) Fatalf(format string, v ...interface{})

Fatalf ...

func (*Wrapper) Fatalln

func (w *Wrapper) Fatalln(v ...interface{})

Fatalln ...

func (*Wrapper) Panic

func (w *Wrapper) Panic(v ...interface{})

Panic ...

func (*Wrapper) Panicf

func (w *Wrapper) Panicf(format string, v ...interface{})

Panicf ...

func (*Wrapper) Panicln

func (w *Wrapper) Panicln(v ...interface{})

Panicln ...

func (*Wrapper) Print

func (w *Wrapper) Print(v ...interface{})

Print ...

func (*Wrapper) Printf

func (w *Wrapper) Printf(format string, v ...interface{})

Printf ...

func (*Wrapper) Println

func (w *Wrapper) Println(v ...interface{})

Println ...

Jump to

Keyboard shortcuts

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