Skip to content

Commit

Permalink
Sync hashicorp modules verbosity with logger verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
iknite committed Feb 20, 2019
1 parent 58f9a6e commit 792ea13
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c
github.com/hashicorp/go-msgpack v0.0.0-20150518234257-fa3f63826f7c
github.com/hashicorp/go-sockaddr v1.0.1 // indirect
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/memberlist v0.1.0
github.com/hashicorp/raft v1.0.0
github.com/imdario/mergo v0.3.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCO
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/memberlist v0.1.0 h1:qSsCiC0WYD39lbSitKNt40e30uorm2Ss/d4JGU1hzH8=
github.com/hashicorp/memberlist v0.1.0/go.mod h1:ncdBp14cuox2iFOq3kDiquKU6fqsTBc3W6JvZwjxxsE=
github.com/hashicorp/raft v1.0.0 h1:htBVktAOtGs4Le5Z7K8SF5H2+oWsQFYVmOgH5loro7Y=
Expand Down
43 changes: 31 additions & 12 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ import (
"fmt"
"log"
"os"

"github.com/hashicorp/logutils"
)

type Level string

// Log levels constants
const (
SILENT = "silent"
ERROR = "error"
INFO = "info"
DEBUG = "debug"
SILENT Level = "silent"
ERROR Level = "error"
INFO Level = "info"
DEBUG Level = "debug"

caller = 3
)
Expand All @@ -48,8 +52,23 @@ type logger interface {
GetLogger() *log.Logger
}

func getFilter(lv Level) *logutils.LevelFilter {

mapLevel := map[Level]logutils.LogLevel{
ERROR: "ERROR",
INFO: "INFO",
DEBUG: "DEBUG",
}

return &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"},
MinLevel: mapLevel[lv],
Writer: os.Stdout,
}
}

// The default logger is an log.ERROR level.
var std logger = newError(os.Stdout, "Qed: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
var std logger = newError(getFilter(ERROR), "Qed: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)

// To allow mocking we require a switchable variable.
var osExit = os.Exit
Expand Down Expand Up @@ -122,22 +141,22 @@ func GetLogger() *log.Logger {

// SetLogger is a function that switches between verbosity loggers. Default
// is error level. Available levels are "silent", "debug", "info" and "error".
func SetLogger(namespace, level string) {
func SetLogger(namespace, lv Level) {

prefix := fmt.Sprintf("%s: ", namespace)

switch level {
switch lv {
case SILENT:
std = newSilent()
case ERROR:
std = newError(os.Stdout, prefix, log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
std = newError(getFilter(lv), prefix, log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
case INFO:
std = newInfo(os.Stdout, prefix, log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
std = newInfo(getFilter(lv), prefix, log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
case DEBUG:
std = newDebug(os.Stdout, prefix, log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
std = newDebug(getFilter(lv), prefix, log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
default:
l := newInfo(os.Stdout, prefix, log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
l.Infof("Incorrect level of verbosity (%v) fallback to log.INFO", level)
l := newInfo(getFilter(INFO), prefix, log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
l.Infof("Incorrect level of verbosity (%v) fallback to log.INFO", lv)
std = l
}

Expand Down

0 comments on commit 792ea13

Please sign in to comment.