Allow to skip specifying logger so default log package will be used.

This commit is contained in:
Stanislav N. aka pztrn 2018-04-27 13:25:27 +05:00
parent 79495d352c
commit 1330c5f1b6
2 changed files with 24 additions and 1 deletions

View File

@ -23,12 +23,26 @@
package flagger package flagger
import (
// stdlib
"log"
"os"
)
var ( var (
logger LoggerInterface logger LoggerInterface
) )
// New creates new Flagger instance.
// If no logger will be passed - we will use default "log" module and will
// print logs to stdout.
func New(l LoggerInterface) *Flagger { func New(l LoggerInterface) *Flagger {
logger = l if l == nil {
lg := log.New(os.Stdout, "Flagger: ", log.LstdFlags)
logger = LoggerInterface(lg)
} else {
logger = l
}
f := Flagger{} f := Flagger{}
return &f return &f
} }

View File

@ -43,6 +43,15 @@ func TestFlaggerInitialization(t *testing.T) {
f.Initialize() f.Initialize()
} }
func TestFlaggerInitializationWithNilLogger(t *testing.T) {
fl := New(nil)
if f == nil {
t.Fatal("Logger initialization failed!")
t.FailNow()
}
fl.Initialize()
}
func TestFlaggerAddBoolFlag(t *testing.T) { func TestFlaggerAddBoolFlag(t *testing.T) {
flagTestBool := Flag{ flagTestBool := Flag{
Name: "testboolflag", Name: "testboolflag",