Skip to content

Commit

Permalink
Setup logger
Browse files Browse the repository at this point in the history
  • Loading branch information
etabak committed Apr 27, 2020
1 parent 4118e9d commit 771a325
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/tzvatot/injector

go 1.13

require (
github.com/onrik/logrus v0.5.1
github.com/sirupsen/logrus v1.5.0
)
34 changes: 29 additions & 5 deletions injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ package injector

import (
"fmt"
"os"
"reflect"
"strings"
"time"

"github.com/sirupsen/logrus"
)

const (
// Default log format will output [INFO]: 2006-01-02T15:04:05Z07:00 - Log message
defaultLogFormat = "[%lvl%]: %time% - %msg%"
defaultTimestampFormat = time.RFC3339
)

type Injector interface {
Expand All @@ -22,6 +32,19 @@ func NewEngine() *Engine {
}
}

var log *logrus.Logger

func init() {
setupLogger()
}

func setupLogger() {
logrus.SetFormatter(&logrus.TextFormatter{})
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.InfoLevel)
log = logrus.StandardLogger()
}

func (inj *Engine) Register(beans ...interface{}) error {
for _, bean := range beans {
inj.beans = append(inj.beans, bean)
Expand All @@ -31,7 +54,7 @@ func (inj *Engine) Register(beans ...interface{}) error {
}
structType := val.Type()
typeFullName := inj.getTypeFullName(structType)
fmt.Printf("type full name: %s\n", typeFullName)
log.Debugf("type full name: %s\n", typeFullName)
inj.typeToBean[typeFullName] = bean
}
return nil
Expand Down Expand Up @@ -59,24 +82,25 @@ func (inj *Engine) injectBean(bean interface{}) error {
for _, field := range fields {
fieldName := field.Name
fieldType := field.Type
fmt.Printf("Name: %s, Type: %s\n", fieldName, fieldType)
log.Debugf("Name: %s, Type: %s\n", fieldName, fieldType)
if fieldType == structType {
fmt.Println("not supporting self injection")
log.Infof("not supporting self injection")
continue
}
childName := inj.getContainingTypeFullName(field)
fmt.Printf("chile name: %s\n", childName)
log.Debugf("chile name: %s\n", childName)
child := inj.typeToBean[childName]
if child == nil {
if childName, ok := field.Tag.Lookup("inject"); ok {
log.Debugf("chile name: %s\n", childName)
child = inj.typeToBean[childName]
}
}
if child != nil {
f := val.FieldByName(fieldName)
if f.CanAddr() && f.IsValid() {
f.Set(reflect.ValueOf(child))
fmt.Printf("Injected %s", child)
log.Debugf("Injected %s", child)
}
}
}
Expand Down

0 comments on commit 771a325

Please sign in to comment.