EasyLogger

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2021 License: MIT Imports: 17 Imported by: 3

README

EasyLogger

Thanks to github.com/gookit/color and github.com/natefinch/lumberjack, this simple rotating logger with colourful console output is easy to config and use. Hope you like it!

Easy Setup

Size Rotating Logger
package main

import (
	"log"
	"testing"
)

func TestNewSizeRotatingEasyLogger(t *testing.T) {
	l := NewSizeRotatingEasyLogger(
		"./Logs/test.log",           // log currentFile name
		1,                           // max currentFile size in MB
		30,                          // max currentFile backup days
		30,                          // max backup currentFile number
		true,                        // use local time
		false,                       // use compression for backup files
		log.Ldate|log.Lmicroseconds, // log line flag
		"",                          // logger name
		true,                        // use console output at the same time
	)

	for i := 0; i < 10000; i++ {
		l.Trace("hello world")
		l.Debug("hello world")
		l.Info("hello world")
		l.Warn("hello world")
		l.Error("hello world")
		l.Fatal("hello world")

		l.Tracef("f:%s", "hello world")
		l.Debugf("f:%s", "hello world")
		l.Infof("f:%s", "hello world")
		l.Warnf("f:%s", "hello world")
		l.Errorf("f:%s", "hello world")
		l.Fatalf("f:%s", "hello world")
	}
}
Time Rotating Logger
package EasyLogger

import (
	"log"
	"testing"
)

func TestNewTimeRotatingEasyLogger(t *testing.T) {
	l := NewTimeRotatingEasyLogger(
		"./Logs/",
		1,
		1,
		true,
		false,
		log.Ldate | log.Lmicroseconds,
		"",
		true)

	l.Trace("hello world")
	l.Debug("hello world")
	l.Info("hello world")
	l.Warn("hello world")
	l.Error("hello world")
	l.Fatal("hello world")

	l.Tracef("f:%s", "hello world")
	l.Debugf("f:%s", "hello world")
	l.Infof("f:%s", "hello world")
	l.Warnf("f:%s", "hello world")
	l.Errorf("f:%s", "hello world")
	l.Fatalf("f:%s", "hello world")
}

Log Example

Documentation

Index

Constants

View Source
const (
	CALL_DEPTH = 2

	TRACE = "[TRACE]"
	DEBUG = "[DEBUG]"
	INFO  = "[INFO ]"
	WARN  = "[WARN ]"
	ERROR = "[ERROR]"
	FATAL = "[FATAL]"
)
View Source
const (
	DefaultLogDir      = "./Logs/"
	FileNameTimeFormat = "2006-01-02"
	FileNameExt        = ".log"
	CompressSuffix     = ".gz"

	NanosecondPerDay = 24 * 3600 * time.Second
)

Variables

View Source
var (
	Trace = color.Cyan
	Debug = color.Blue
	Info  = color.Green
	Warn  = color.Yellow
	Error = color.Red
	Fatal = color.Magenta
)

Functions

func GetGID

func GetGID() uint64

Types

type EasyLogger

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

EasyLogger uses log.Logger inside

func NewSizeRotatingEasyLogger added in v1.1.0

func NewSizeRotatingEasyLogger(fileName string,
	maxFileSize int,
	maxBackupAge int,
	maxBackupFiles int,
	useLocalTime bool,
	useCompression bool,
	lineFlag int,
	prefixForLogger string,
	needConsoleOut bool) *EasyLogger

func NewTimeRotatingEasyLogger added in v1.1.0

func NewTimeRotatingEasyLogger(dirName string,
	rotateDays int,
	maxBackupFiles int,
	useLocalTime bool,
	useCompression bool,
	lineFlag int,
	prefixForLogger string,
	needConsoleOut bool) *EasyLogger

func (*EasyLogger) Debug

func (this *EasyLogger) Debug(a ...interface{})

func (*EasyLogger) Debugf

func (this *EasyLogger) Debugf(format string, a ...interface{})

func (*EasyLogger) Error

func (this *EasyLogger) Error(a ...interface{})

func (*EasyLogger) Errorf

func (this *EasyLogger) Errorf(format string, a ...interface{})

func (*EasyLogger) Fatal

func (this *EasyLogger) Fatal(a ...interface{})

func (*EasyLogger) Fatalf

func (this *EasyLogger) Fatalf(format string, a ...interface{})

func (*EasyLogger) Info

func (this *EasyLogger) Info(a ...interface{})

func (*EasyLogger) Infof

func (this *EasyLogger) Infof(format string, a ...interface{})

func (*EasyLogger) Trace

func (this *EasyLogger) Trace(a ...interface{})

func (*EasyLogger) Tracef

func (this *EasyLogger) Tracef(format string, a ...interface{})

func (*EasyLogger) Warn

func (this *EasyLogger) Warn(a ...interface{})

func (*EasyLogger) Warnf

func (this *EasyLogger) Warnf(format string, a ...interface{})

type Logger added in v1.1.0

type Logger struct {
	// Directory is the place to store log files.
	// Default is "./Logs/"
	Directory string

	// MaxDays is the maximum number of days to rotate.
	// The default is not rotating.
	MaxDays int

	// MaxBackups is the maximum number of files to retain.
	// The default is to retain all old files.
	MaxBackups int

	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	LocalTime bool

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool
	// contains filtered or unexported fields
}

func (*Logger) Close added in v1.1.0

func (l *Logger) Close() error

Close implements io.Closer, and closes the current logfile.

func (*Logger) Write added in v1.1.0

func (l *Logger) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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