Skip to content

nefeg/slog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slog

GoDoc

Simple logger. Just import and use.

Install

go get github.com/umbrella-evgeny-nefedkin/slog

Usage

Import the library and use:

import "github.com/umbrella-evgeny-nefedkin/slog"

func main(){
    ...
    slog.SetLevel(slog.LvlAll) // default LvlNone
    slog.SetFormat(slog.FormatTimmed)
    slog.Info("any info")
    ...
}

or if you need show error trace in debug mode only:

import "github.com/umbrella-evgeny-nefedkin/slog"

func main(){

    ...
    slog.SetLevel(slog.LvlAll) // default LvlNone
    slog.SetFormat(slog.FormatTimmed)


    // main thread will be alive even if goroutine failed
    // but in debug mode main theread fail too with error trace 
    go func(){
        defer func(){
            if r := recover(); r != nil{
                slog.Infoln(r) // will show every time
                slog.DebugPanic(r) // will panic if application running in debug mode
            }
            return
        }()
                    
        ... some code ...
    }()
    
	....

}

Custom setting

Custom log format

    // import "github.com/umbrella-evgeny-nefedkin/slog"
    
    var TimeFormatted slog.Format = func() string{
    
        return fmt.Sprintf(`[%s] %%s`, time.Now().Format(time.RFC822))
    }
    
    slog.SetFormat(TimeFormatted)
    
    slog.Info("any info") // Output: [23 Jan 18 21:55 UTC] any info

build-in formats:

    slog.FormatTimed
    slog.FormatTimed_RFC822
    slog.FormatDefault

Custom log function

Using wrapper:

// import . "github.com/umbrella-evgeny-nefedkin/slog"

var myCutomFunction SLogger  = func(data ...interface{}) interface{} { fmt.Sprint(data...); return nil }
var myCutomLevel             = Level{"myCutomLevel", 5}

Wrap(&myCutomFunction, myCutomLevel, false)

SetLevel(LvlAll)

myCutomFunction("some data for print")

Using binding:

// import . "github.com/umbrella-evgeny-nefedkin/slog"

var myStdBindFun SLogger  = func(data ...interface{}) interface{} { fmt.Println(data...); return nil }


var myCustomDebugFunc SLogger
var myCustomDebugLevel   = Level{"myCustomDebugLevel", 5}
Bind(&myCustomDebugFunc, myStdBindFun, myCustomDebugLevel, false)

var myCustomNoticeFunc SLogger
var myCustomNoticeLevel  = Level{"myCustomNoticeLevel", 15}
Bind(&myCustomNoticeFunc, myStdBindFun, myCustomNoticeLevel, false)

myCustomDebugFunc("some debug data")
myCustomDebugFunc("some info data")

Function overriding:

Just use override-flag:

Wrap(&myCutomFunction, LvlInfo, TRUE)

or

Bind(&myCustomNoticeFunc, myStdBindFun, LvlInfo, TRUE)

About

Simple logger. Just import and use.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages