Skip to content

Commit 79d4444

Browse files
committed
refactor(repo): Improve log format
1 parent cd6991d commit 79d4444

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

internal/config/config.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,33 @@ type Config struct {
2424

2525
var configFileCandidates = []string{".pr-release", ".compozy-release"}
2626

27-
// DefaultConfig returns a Config with default values.
2827
func DefaultConfig() *Config {
29-
return &Config{ToolsDir: "tools", LogLevel: "info", LogFormat: "json"}
28+
logFormat := "json"
29+
if isCI() {
30+
logFormat = "console"
31+
}
32+
return &Config{ToolsDir: "tools", LogLevel: "info", LogFormat: logFormat}
33+
}
34+
35+
func isCI() bool {
36+
ciEnvVars := []string{
37+
"CI",
38+
"CONTINUOUS_INTEGRATION",
39+
"GITHUB_ACTIONS",
40+
"GITLAB_CI",
41+
"CIRCLECI",
42+
"TRAVIS",
43+
"JENKINS_URL",
44+
"BUILDKITE",
45+
"DRONE",
46+
"TEAMCITY_VERSION",
47+
}
48+
for _, envVar := range ciEnvVars {
49+
if os.Getenv(envVar) != "" {
50+
return true
51+
}
52+
}
53+
return false
3054
}
3155

3256
// Validate validates the configuration.
@@ -126,8 +150,6 @@ func LoadConfig() (*Config, error) {
126150
v := viper.New()
127151
v.SetConfigType("yaml")
128152
v.AddConfigPath(".")
129-
v.SetDefault("log_level", "info")
130-
v.SetDefault("log_format", "json")
131153
v.AutomaticEnv()
132154
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
133155
if err := v.BindEnv(
@@ -179,6 +201,8 @@ func LoadConfig() (*Config, error) {
179201
}
180202
defaults := DefaultConfig()
181203
v.SetDefault("tools_dir", defaults.ToolsDir)
204+
v.SetDefault("log_level", defaults.LogLevel)
205+
v.SetDefault("log_format", defaults.LogFormat)
182206
for _, name := range configFileCandidates {
183207
v.SetConfigName(name)
184208
if err := v.ReadInConfig(); err != nil {

internal/logger/logger.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
78
"strings"
89
"syscall"
910

@@ -18,6 +19,11 @@ type Config struct {
1819

1920
type contextKey struct{}
2021

22+
const (
23+
formatJSON = "json"
24+
formatConsole = "console"
25+
)
26+
2127
func New(cfg Config) (*zap.Logger, error) {
2228
zapCfg, err := buildZapConfig(cfg)
2329
if err != nil {
@@ -30,9 +36,9 @@ func buildZapConfig(cfg Config) (zap.Config, error) {
3036
format := strings.ToLower(strings.TrimSpace(cfg.Format))
3137
var zapCfg zap.Config
3238
switch format {
33-
case "json":
39+
case formatJSON:
3440
zapCfg = zap.NewProductionConfig()
35-
case "console":
41+
case formatConsole:
3642
zapCfg = zap.NewDevelopmentConfig()
3743
default:
3844
return zap.Config{}, fmt.Errorf("logger: unsupported format %s", cfg.Format)
@@ -43,6 +49,14 @@ func buildZapConfig(cfg Config) (zap.Config, error) {
4349
}
4450
zapCfg.Level = zap.NewAtomicLevelAt(level)
4551
zapCfg.Encoding = format
52+
encoder := buildEncoderConfig(format)
53+
zapCfg.EncoderConfig = encoder
54+
zapCfg.OutputPaths = []string{"stdout"}
55+
zapCfg.ErrorOutputPaths = []string{"stderr"}
56+
return zapCfg, nil
57+
}
58+
59+
func buildEncoderConfig(format string) zapcore.EncoderConfig {
4660
encoder := zapcore.EncoderConfig{
4761
TimeKey: "ts",
4862
LevelKey: "level",
@@ -56,13 +70,36 @@ func buildZapConfig(cfg Config) (zap.Config, error) {
5670
EncodeDuration: zapcore.SecondsDurationEncoder,
5771
EncodeCaller: zapcore.ShortCallerEncoder,
5872
}
59-
if format == "console" {
60-
encoder.EncodeLevel = zapcore.CapitalColorLevelEncoder
73+
if format == formatConsole {
74+
if isCI() {
75+
encoder.EncodeLevel = zapcore.CapitalLevelEncoder
76+
encoder.EncodeTime = zapcore.RFC3339TimeEncoder
77+
} else {
78+
encoder.EncodeLevel = zapcore.CapitalColorLevelEncoder
79+
}
6180
}
62-
zapCfg.EncoderConfig = encoder
63-
zapCfg.OutputPaths = []string{"stdout"}
64-
zapCfg.ErrorOutputPaths = []string{"stderr"}
65-
return zapCfg, nil
81+
return encoder
82+
}
83+
84+
func isCI() bool {
85+
ciEnvVars := []string{
86+
"CI",
87+
"CONTINUOUS_INTEGRATION",
88+
"GITHUB_ACTIONS",
89+
"GITLAB_CI",
90+
"CIRCLECI",
91+
"TRAVIS",
92+
"JENKINS_URL",
93+
"BUILDKITE",
94+
"DRONE",
95+
"TEAMCITY_VERSION",
96+
}
97+
for _, envVar := range ciEnvVars {
98+
if os.Getenv(envVar) != "" {
99+
return true
100+
}
101+
}
102+
return false
66103
}
67104

68105
func parseLevel(level string) (zapcore.Level, error) {

0 commit comments

Comments
 (0)