Skip to content

Commit

Permalink
add default logger, support parse environment variables automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuah committed Aug 20, 2019
1 parent 9e4d2e8 commit f1e8c14
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/cosiner/go-di

go 1.4
13 changes: 13 additions & 0 deletions inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package di
import (
"errors"
"fmt"
"os"
"reflect"
"regexp"
"sync"
Expand Down Expand Up @@ -36,6 +37,18 @@ func New() *Injector {
}
}

func NewAndParseEnv(prefix string) *Injector {
inj := New()

if os.Getenv(prefix+"DI_SYNC") == "true" {
inj.UseRunner(syncRunner{})
}
if os.Getenv(prefix+"DI_LOG") == "true" {
inj.UseLogger(DefaultLogger{})
}
return inj
}

func (j *Injector) UseRunner(r Runner) *Injector {
j.runner = r
return j
Expand Down
12 changes: 12 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package di

import (
"fmt"
"log"
"runtime"
"sync"
"time"
Expand All @@ -11,6 +12,7 @@ type Logger interface {
Begin(name string, at time.Time)
End(name string, at time.Time, dur time.Duration)
}

type nopLogger struct{}

func (nopLogger) Begin(name string, at time.Time) {}
Expand Down Expand Up @@ -145,3 +147,13 @@ func (a *asyncRunner) waitDone() error {
a.mu.Unlock()
return err
}

type DefaultLogger struct{}

func (DefaultLogger) Begin(name string, at time.Time) {
log.Printf("Begin %s\n", name)
}

func (DefaultLogger) End(name string, at time.Time, dur time.Duration) {
log.Printf("End %s - %s\n", name, dur)
}

0 comments on commit f1e8c14

Please sign in to comment.