Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Apr 1, 2019
1 parent 6b7dd3c commit c58f6c4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions _example/go.mod
@@ -0,0 +1,7 @@
module github.com/go-pkgz/lgr/_example

go 1.12

require github.com/go-pkgz/lgr v0.0.0

replace github.com/go-pkgz/lgr => ../
6 changes: 6 additions & 0 deletions _example/go.sum
@@ -0,0 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-pkgz/lgr v0.6.1 h1:poohUbv/iguoQ6bzJ5j/Ubl1VcsjU+rzTbbxsKbkGXk=
github.com/go-pkgz/lgr v0.6.1/go.mod h1:hBM1NM/SoYdlrykgdgJWGrZ/TM/XaZIjRbJfx7NkMm8=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
37 changes: 37 additions & 0 deletions _example/main.go
@@ -0,0 +1,37 @@
package main

import "github.com/go-pkgz/lgr"

// Logger defines application's logger interface. Note - it doesn't introduce any dependency on lgr
// and can be replaced with anything providing Logf function
type Logger interface {
Logf(format string, args ...interface{})
}

func main() {
l := lgr.New(lgr.Format(lgr.FullDebug)) // create lgr instance
logConsumer(l) // pass logger to consumer

l2 := lgr.New(lgr.Debug, lgr.Format(lgr.ShortDebug)) // create lgr instance, debug enabled
logConsumer(l2) // pass logger to consumer

logWithGlobal() // logging with default global logger
lgr.Setup(lgr.Msec, lgr.LevelBraces) // change settings of global logger
logWithGlobal() // logging with modified global logger

// 2019/04/01 02:43:20.590 INFO (_example/main.go:31 main.logConsumer) test 12345
// 2019/04/01 02:43:20.591 INFO (_example/main.go:31) test 12345
// 2019/04/01 02:43:20.591 DEBUG (_example/main.go:32) something
// 2019/04/01 02:43:20 WARN test 9876543
// 2019/04/01 02:43:20.591 [WARN] test 9876543
}

// consumer example with Logger passed in
func logConsumer(l Logger) {
l.Logf("INFO test 12345")
l.Logf("DEBUG something") // will be printed for logger with Debug enabled
}

func logWithGlobal() {
lgr.Printf("WARN test 9876543") // print to default logger
}

0 comments on commit c58f6c4

Please sign in to comment.