golog

package module
v0.0.0-...-3ac5e8e Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2019 License: MIT Imports: 8 Imported by: 0

README

golog

Simple logging library for Golang.

Installation

go get -u github.com/dafengge0913/golog

Quick start

package main

import "golog"

func main() {
	log := golog.NewLogger(golog.LevelDebug)
	log.Debug("Debug msg")
	log.Info("Info msg, id=%d, name=%s", 1, "dfg")
	log.Warn("Warn msg")
	log.Error("Error msg")
}

Output

Color

Supported in Windows and Linux for now, this feature could be canceled by this setting :

log.SetPrintColor(false)

and also could be activate anytime you want by set this to true.

File path

File path and line number are optional, if you don't want this, do that :

log.SetPrintPath(false)

Write to File

package main

import (
	"time"
	"golog"
)

func main() {
	writeCfg := golog.NewLogWriterConfig()
	// cache size of chan
	writeCfg.SetCacheSize(1024)
	// interval of flush to disk
	writeCfg.SetSaveInterval(time.Second * 5)
	// format date as a suffix to log file name when rotate
	writeCfg.SetDateFormat("2006@01@02")
	writer, err := golog.NewLogWriterFile(golog.LevelInfo, "D:/dfg", "demo_log", true, writeCfg)
	if err != nil {
		panic(err)
	}
	// only log error
	errWriter, err := golog.NewLogWriterFile(golog.LevelError, "D:/dfg", "demo_log_error", false, nil)
	if err != nil {
		panic(err)
	}
	log := golog.NewLogger(golog.LevelInfo, writer, errWriter)
	for i := 0; i < 100; i++ {
		log.Info("info i=%d", i)
		if i%10 == 0 {
			log.Error("error i=%d", i)
		}
	}
	// !! important
	log.Close()
}

You can create a file writer by call golog.NewLogWriterFile. For performance purpose, LogWriterFile is an asynchronous log recorder. When you spawn log message, it will be temporary stored in a chan, and flush to disk by a goroutine later. So it's important to call Close() before your application exit, make sure all message has been flushed in disk.

If you use all default settings for LogWriterFile, there is no need to create a logWriterConfig, you can just pass a nil like this :

writer, err := NewLogWriterFile(LevelDebug, "D:/dfg", "demo_log", true, nil)

License

Under the MIT License .

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConfig

func NewConfig() *config

func NewLogWriterConfig

func NewLogWriterConfig() *logWriterConfig

Types

type ILogWriter

type ILogWriter interface {
	Write(*logEntity) error
	Close() error
}

type IPrinter

type IPrinter interface {
	Print(level LevelType, str string) error
}

type LevelType

type LevelType uint8
const (
	LevelDebug LevelType = iota
	LevelInfo
	LevelWarn
	LevelError
)

type LogWriterFile

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

func NewLogWriterFile

func NewLogWriterFile(level LevelType, path string, fileName string, rotate bool, config *logWriterConfig) (*LogWriterFile, error)

config - using default config when nil

func (*LogWriterFile) Close

func (w *LogWriterFile) Close() error

func (LogWriterFile) SetCacheSize

func (cfg LogWriterFile) SetCacheSize(cacheSize uint32)

func (*LogWriterFile) SetDateFormat

func (w *LogWriterFile) SetDateFormat(dateFormat string)

func (LogWriterFile) SetDateTimeFormat

func (cfg LogWriterFile) SetDateTimeFormat(dateTimeFormat string)

func (LogWriterFile) SetSaveInterval

func (cfg LogWriterFile) SetSaveInterval(saveInterval time.Duration)

func (*LogWriterFile) Write

func (w *LogWriterFile) Write(logEntity *logEntity) error

type Logger

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

func NewLogger

func NewLogger(level LevelType, writer ...ILogWriter) *Logger

func (*Logger) Close

func (log *Logger) Close()

func (*Logger) Debug

func (log *Logger) Debug(msg string, args ...interface{})

func (*Logger) Error

func (log *Logger) Error(msg string, args ...interface{})

func (*Logger) Info

func (log *Logger) Info(msg string, args ...interface{})

func (Logger) SetDateFormat

func (cfg Logger) SetDateFormat(dateFormat string)

func (Logger) SetDateTimeFormat

func (cfg Logger) SetDateTimeFormat(dateTimeFormat string)

func (Logger) SetFuncSkip

func (cfg Logger) SetFuncSkip(skip int)

func (Logger) SetIsPrint

func (cfg Logger) SetIsPrint(isPrint bool)

func (*Logger) SetPrintColor

func (log *Logger) SetPrintColor(printColor bool)

func (Logger) SetPrintPath

func (cfg Logger) SetPrintPath(printPath bool)

func (*Logger) Warn

func (log *Logger) Warn(msg string, args ...interface{})

type PlainPrinter

type PlainPrinter struct {
}

func NewPlainPrinter

func NewPlainPrinter() *PlainPrinter

func (*PlainPrinter) Print

func (printer *PlainPrinter) Print(level LevelType, str string) error

type Printer

type Printer struct {
}

func NewPrinter

func NewPrinter() *Printer

func (*Printer) Print

func (printer *Printer) Print(level LevelType, str string) error

Jump to

Keyboard shortcuts

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