logger

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 6 Imported by: 5

README

Go version

go-logger

Basic logger library with the following features:

  • timestamp
  • level support (INFO, ERROR, WARNING and FATAL)
  • verbose mode
  • log to channel

Info log messages are redirected to stdout, stderr otherwise.

Installation

go get -u github.com/dmachard/go-logger

Usage example

Create logger with verbose mode enabled.

import (
    "github.com/dmachard/go-logger"
)

lg := logger.New(true)
lg.Info("just a basic message!")

Output example:

INFO: 2021/07/04 14:18:22.270971 just a basic message

Usage example with channel

Create logger with channel

import (
    "github.com/dmachard/go-logger"
)

logsChan := make(chan logger.LogEntry, 10)

lg := logger.New(false)
lg.SetOutputChannel((logsChan))

lg.Info("just a basic message!")

entry := <-logsChan
fmt.Println(entry.Level, entry.Message)

Testing

$ go test -v
=== RUN   TestLogInfo
--- PASS: TestLogInfo (0.00s)
=== RUN   TestLogError
--- PASS: TestLogError (0.00s)
=== RUN   TestVerbose
--- PASS: TestVerbose (0.00s)
PASS
ok      github.com/dmachard/go-logger   0.002s

Benchmark

$ go test -bench=.
goos: linux
goarch: amd64
pkg: github.com/dmachard/go-logger
cpu: Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
BenchmarkLogInfo-4       4094160               278.4 ns/op
PASS
ok      github.com/dmachard/go-logger   1.449s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Level

type Level int
const (
	INFO Level = iota
	WARNING
	ERROR
	FATAL
)

type LogEntry added in v0.4.0

type LogEntry struct {
	Timestamp time.Time
	Level     Level
	Message   string
}

type Logger

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

func New

func New(verbose bool) *Logger

Init loggers for each log levels

func (*Logger) Error

func (l *Logger) Error(format string, v ...interface{})

func (*Logger) ErrorLogger added in v0.2.0

func (l *Logger) ErrorLogger() *log.Logger

Return Error Logger

func (*Logger) Fatal

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

func (*Logger) Info

func (l *Logger) Info(format string, v ...interface{})

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

Sets the output destination for the loggers By default destination is stdout, you can change that with this function

func (*Logger) SetOutputChannel added in v0.4.0

func (l *Logger) SetOutputChannel(c chan LogEntry)

Sets the output to a channel

func (*Logger) SetVerbose

func (l *Logger) SetVerbose(verbose bool)

enable the verbose mode

func (*Logger) Warning added in v1.0.0

func (l *Logger) Warning(format string, v ...interface{})

Jump to

Keyboard shortcuts

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