log

package module
v0.0.0-...-9cbc96a Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: BSD-3-Clause Imports: 5 Imported by: 1

README

log

A wrapper on top of Golang log package which includes levelling and rotated file output. Complete documentation at Godoc.

Usage

Following example uses default logger implementation.

import "github.com/arknable/log"

... etc

l := log.New()

l.Debug("This is a debug message")
l.Info("This is", "an info message")
l.Warning("This is a warning message")
l.Error("This is", "an error message")

There is also package-level logger.

import "github.com/arknable/log"

... etc

log.Debug("This is a debug message")
log.Info("This is", "an info message")
log.Warning("This is a warning message")
log.Error("This is", "an error message")

By default, log messages sent to stdout. To use custom output use SetOutput() function of the logger.

import "github.com/arknable/log"

... etc

file, err := os.Open(......)
if err != nil {
    log.Fatal(err)
}
defer file.Close()

l := log.New()
l.SetOutput(io.MultiWriter(os.Stdout, file))

or you can set custom output to package-level logger.

import "github.com/arknable/log"

... etc

file, err := os.Open(......)
if err != nil {
    log.Fatal(err)
}
defer file.Close()

log.SetOutput(io.MultiWriter(os.Stdout, file))

If you need file output that has name rolled every day, there is function DailyFileOutput.

import "github.com/arknable/log"

... etc

file, err := DailyFileOutput("test_daily_file", "./")
if err != nil {
    t.Fatal(err)
}
defer file.Close()

logger := New()
logger.SetOutput(io.MultiWriter(os.Stdout, file))

Sample Output

2021/03/23 05:35:19   DEBUG This is a debug message
2021/03/23 05:35:19    INFO This is an info message
2021/03/23 05:35:19 WARNING This is a warning message
2021/03/23 05:35:19   ERROR This is an error message
2021/03/23 05:35:19   DEBUG This is a debugf message
2021/03/23 05:35:19    INFO This is an infof message
2021/03/23 05:35:19 WARNING This is a warningf message
2021/03/23 05:35:19   ERROR This is an errorf message

Documentation

Index

Constants

View Source
const (
	// DebugLevel informs information for debugging purpose
	DebugLevel = "DEBUG"

	// InfoLevel informs that there is a useful information
	InfoLevel = "INFO"

	// WarningLevel informs that we need to pay more attention on something
	WarningLevel = "WARNING"

	// ErrorLevel informs that an error occured
	ErrorLevel = "ERROR"

	// FatalLevel informs that we are having a panic
	FatalLevel = "FATAL"
)

Variables

This section is empty.

Functions

func DailyFileOutput

func DailyFileOutput(prefix, dirpath string) (*os.File, error)

DailyFileOutput creates file to output log messages which has extension '.log'. The log file will be rolled each day with name format prefix_yyyymmdd.log and saved inside dirpath. If dirpath not exists, it will be created.

func Debug

func Debug(v ...interface{})

Debug is bridge for Logger.Debug of default defaultLogger

func Debugf

func Debugf(format string, v ...interface{})

Debugf is bridge for Logger.Debugf of default defaultLogger

func Error

func Error(v ...interface{})

Error is bridge for Logger.Error of default defaultLogger

func Errorf

func Errorf(format string, v ...interface{})

Errorf is bridge for Logger.Errorf of default defaultLogger

func Fatal

func Fatal(v ...interface{})

Fatal is bridge for Logger.Fatal of default defaultLogger

func Fatalf

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

Fatalf is bridge for Logger.Fatalf of default defaultLogger

func Info

func Info(v ...interface{})

Info is bridge for Logger.Info of default defaultLogger

func Infof

func Infof(format string, v ...interface{})

Infof is bridge for Logger.Infof of default defaultLogger

func SetDefaultLogger

func SetDefaultLogger(l Logger)

SetDefaultLogger replaces default logger with given one.

func Warning

func Warning(v ...interface{})

Warning is bridge for Logger.Warning of default defaultLogger

func Warningf

func Warningf(format string, v ...interface{})

Warningf is bridge for Logger.Warningf of default defaultLogger

Types

type Level

type Level string

Level is the level of this message

func (Level) String

func (l Level) String() string

String returns string representation of this level

type Logger

type Logger interface {
	// Debug logs debug messages
	Debug(v ...interface{})

	// Debugf logs formatted debug messages
	Debugf(format string, v ...interface{})

	// Info lofs info messages
	Info(v ...interface{})

	// Infof lofs formatted info messages
	Infof(format string, v ...interface{})

	// Warning logs warning messages
	Warning(v ...interface{})

	// Warningf logs formatted warning messages
	Warningf(format string, v ...interface{})

	// Error logs error messages
	Error(v ...interface{})

	// Errorf logs formatted error messages
	Errorf(format string, v ...interface{})

	// Fatal logs fatal messages and calls os.Exit(1)
	Fatal(v ...interface{})

	// Fatalf logs formatted fatal messages and calls os.Exit(1)
	Fatalf(format string, v ...interface{})

	// SetOutput sets output writer
	SetOutput(w io.Writer)
}

Logger is required specification for a logger

func DefaultLogger

func DefaultLogger() Logger

DefaultLogger returns instance of default logger

func New

func New() Logger

New creates new defaultLogger

Jump to

Keyboard shortcuts

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