slog

package
v0.0.0-...-690a8d5 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2017 License: MIT Imports: 10 Imported by: 0

README

slog

slog is a micro log libray.log format is use default. log use ConsolePrinter as default(least level) ,you can use it without any configuration.

in advance,use AddPrinter config you output mode.once operated the default console printer is no longer exist.so add it if needed.

-logmode=stdout:info,file:warn
-logf_dir=./log
-logf_name=app
-logf_ksize=1024
-logf_blockmillis=10
-logf_bufferrow=1024
-logf_backup=5

Attention: when init log by flags,you must call InitByFlags. option level: debug verbose(verbo) info warn error note

Example

func init() {
	flag.Set("logmode", "stdout:debug,file:warn")
	flag.Set("logf_ksize","200")
	slog.InitByFlags()
}
func main() {
	for i := 0; i < 10000; i++ {
		<-time.After(time.Nanosecond)
		slog.Debug("debug msg:", i)
		slog.Debugf("debugf msg %d", i)
		slog.Verbose("Verbose msg:", i)
		slog.Verbosef("Verbosef msg %d", i)
		slog.Info("Info msg:", i)
		slog.Infof("Infof msg %d", i)
		slog.Warn("Warn msg:", i)
		slog.Warnf("Warnf msg %d", i)
		slog.Error("Error msg:", i)
		slog.Errorf("Errorf msg %d", i)
		slog.Note("important info, not an error:", i)
	}
	slog.Exit("wait for flush to file.")
}

Logger instance

user New() get a new Logger instance,support Print,Println,Printf,Output as std lib log.

f, _ := slog.NewFilePrinter(1024, 9, "", "", 0, 0)
logger := slog.New(slog.LevINFO, f).AddPrinter(slog.LevINFO, slog.NewConsole())
logger.Print("hello")

Documentation

Overview

slog is a micro log libray.log format is use default. log use ConsolePrinter as default(at level DEBUG) ,you can use it without any configuration.

in advance,use cmdline args configure you output mode and other options.once operated the default console printer is no longer exist.so add it if needed.

-logmode=stdout:info,file:warn
-logf_dir=.
-logf_name=app
-logf_ksize=10
-logf_blockmillis=200
-logf_bufferrow=123
-logf_backup=5

or you can set up by code:

flag.Set("logmode", "stdout:error,file:debug")
//flag.Set("logmode", "stdout:verbo,file:fatal")
slog.InitByFlags()

Attention: anywhere you must call InitByFlags.(we support setup by code which made me don't known when flags is ready ). And slog's file output use one goroutine,it not worth to support flush file by hand which means a lock is needed. if you want guarantee all log flush to file before exit,I suggest just wait for a few second .

Index

Constants

View Source
const (
	LevDEBUG   lev = 1
	LevVERBOSE lev = 2
	LevINFO    lev = 4
	LevWARN    lev = 8
	LevERROR   lev = 0x10
	LevNOTE    lev = 0x20
)

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

func Debugf

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

func Default

func Default() *logger

func Error

func Error(args ...interface{})

func Errorf

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

func Exit

func Exit(args ...interface{})

func Exitf

func Exitf(format string, args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func Info

func Info(args ...interface{})

func Infof

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

func InitByFlags

func InitByFlags()

calling when configurate by flags

func New

func New(lv lev, p Printer) *logger

func Note

func Note(args ...interface{})

func Notef

func Notef(format string, args ...interface{})

func Verbose

func Verbose(args ...interface{})

func Verbosef

func Verbosef(format string, args ...interface{})

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(format string, args ...interface{})

Types

type Console

type Console struct {
}

func NewConsole

func NewConsole() *Console

func (*Console) Print

func (this *Console) Print(s *string) error

type DefaultFormater

type DefaultFormater struct {
}

func (*DefaultFormater) Format

func (this *DefaultFormater) Format(calldepth int, tag, msg string) *string

type FilePrinter

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

func (*FilePrinter) Print

func (this *FilePrinter) Print(s *string) error

type Formater

type Formater interface {
	//calldepth Format==0
	Format(calldepth int, tag, msg string) *string
}

type Printer

type Printer interface {
	Print(s *string) error
}

func NewFilePrinter

func NewFilePrinter(sizeKB, rollCount int, dir, name string, blockMillis, bufferRows int) (Printer, error)

NewFilePrinter

sizeKB: 日志文件滚动大小,以KB为单位
rollCount:日志文件滚动个数
dir:   日志输出目录,""为当前
name:  日志文件名称,""为程序名
blockMillis:缓冲队列满时,日志线程阻塞工作线程最大毫秒数。越大则丢日志的可能性越低
buffer_rows:缓冲队列长度(记录条数)

func NewUdpPrinter

func NewUdpPrinter(addr string, maxrn int, blockMillis int) (Printer, error)

type UdpPrinter

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

func (*UdpPrinter) Print

func (this *UdpPrinter) Print(s *string) error

Jump to

Keyboard shortcuts

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