Skip to content

Commit 7f4da0c

Browse files
committed
moved logger config to its own package and made logs work while in tests
1 parent e5aab33 commit 7f4da0c

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

executor/cmd_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/picostack/pico/executor"
1111
"github.com/picostack/pico/secret/memory"
1212
"github.com/picostack/pico/task"
13+
14+
_ "github.com/picostack/pico/logger"
1315
)
1416

1517
func TestMain(m *testing.M) {

logger/logger.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package logger
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"strings"
7+
8+
"go.uber.org/zap"
9+
"go.uber.org/zap/zapcore"
10+
)
11+
12+
func init() {
13+
// constructs a logger and replaces the default global logger
14+
var config zap.Config
15+
if d, e := strconv.ParseBool(os.Getenv("DEVELOPMENT")); d && e == nil || isInTests() {
16+
config = zap.NewDevelopmentConfig()
17+
} else {
18+
config = zap.NewProductionConfig()
19+
}
20+
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
21+
config.DisableStacktrace = true
22+
if d, e := strconv.ParseBool(os.Getenv("DEBUG")); d && e == nil || isInTests() {
23+
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
24+
}
25+
logger, err := config.Build()
26+
if err != nil {
27+
panic(err)
28+
}
29+
zap.ReplaceGlobals(logger)
30+
}
31+
32+
func isInTests() bool {
33+
for _, arg := range os.Args {
34+
if strings.HasPrefix(arg, "-test.v=") {
35+
return true
36+
}
37+
}
38+
return false
39+
}

main.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,19 @@ import (
44
"context"
55
"os"
66
"os/signal"
7-
"strconv"
87
"time"
98

109
_ "github.com/joho/godotenv/autoload"
1110
"github.com/pkg/errors"
1211
"github.com/urfave/cli"
1312
"go.uber.org/zap"
14-
"go.uber.org/zap/zapcore"
1513

14+
_ "github.com/picostack/pico/logger"
1615
"github.com/picostack/pico/service"
1716
)
1817

1918
var version = "master"
2019

21-
func init() {
22-
// constructs a logger and replaces the default global logger
23-
var config zap.Config
24-
if d, e := strconv.ParseBool(os.Getenv("DEVELOPMENT")); d && e == nil {
25-
config = zap.NewDevelopmentConfig()
26-
} else {
27-
config = zap.NewProductionConfig()
28-
}
29-
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
30-
config.DisableStacktrace = true
31-
if d, e := strconv.ParseBool(os.Getenv("DEBUG")); d && e == nil {
32-
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
33-
}
34-
logger, err := config.Build()
35-
if err != nil {
36-
panic(err)
37-
}
38-
zap.ReplaceGlobals(logger)
39-
}
40-
4120
func main() {
4221
app := cli.NewApp()
4322

watcher/git_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import (
66
"time"
77

88
"github.com/picostack/pico/task"
9+
10+
_ "github.com/picostack/pico/logger"
911
)
1012

1113
var w *GitWatcher
1214
var bus chan task.ExecutionTask
1315

1416
func TestMain(m *testing.M) {
15-
os.Setenv("DEBUG", "1")
1617
bus = make(chan task.ExecutionTask, 16)
1718
w = NewGitWatcher(".test", bus, time.Second, nil)
1819

0 commit comments

Comments
 (0)