Skip to content

Commit 2eef66c

Browse files
author
dozer
committed
add psr-3 compatibility
some time the "log-level" flag value can be string, other time - int now it is possible to parse both variants @see https://www.php-fig.org/psr/psr-3/#5-psrlogloglevel
1 parent 1e9dc5e commit 2eef66c

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

cmd/docker-fpm-wrapper/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
type Config struct {
13-
LogLevel int `mapstructure:"log-level"`
13+
LogLevel string `mapstructure:"log-level"`
1414
LogEncoder string `mapstructure:"log-encoder"`
1515

1616
FpmPath string `mapstructure:"fpm"`
@@ -32,7 +32,7 @@ type Config struct {
3232
}
3333

3434
func parseCommandLineFlags() {
35-
pflag.Int8("log-level", -1, "Log level. -1 debug ")
35+
pflag.String("log-level", "-1", "Log level. -1 debug ")
3636
pflag.String("log-encoder", "auto", "Internal logging encoder")
3737

3838
pflag.StringP("fpm", "f", "", "path to php-fpm")

cmd/docker-fpm-wrapper/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ func createLoggerEncoder(eName string, encoderConfig zapcore.EncoderConfig) (zap
4949
}
5050
}
5151

52-
func createLogger(encName string, level int, output zapcore.WriteSyncer) (*zap.Logger, error) {
52+
func createLogger(encName string, level zapcore.Level, output zapcore.WriteSyncer) (*zap.Logger, error) {
5353
enc, err := createLoggerEncoder(encName, newZapEncoderConfig())
5454
if err != nil {
5555
return nil, err
5656
}
5757

58-
atomicLevel := zap.NewAtomicLevelAt(zapcore.Level(level))
58+
atomicLevel := zap.NewAtomicLevelAt(level)
5959

6060
return zap.New(zapcore.NewCore(enc, output, atomicLevel)), nil
6161
}

cmd/docker-fpm-wrapper/main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"os"
88
"os/signal"
9+
"strconv"
910
"syscall"
1011

1112
"github.com/prometheus/client_golang/prometheus"
@@ -42,7 +43,21 @@ func main() {
4243
}
4344

4445
syncStderr := zapcore.Lock(os.Stderr)
45-
log, err := createLogger(cfg.LogEncoder, cfg.LogLevel, syncStderr)
46+
// cfg.LogLevel can be either int or string
47+
// example: it can be -1 or debug
48+
// try to parse it by string
49+
logLever, err := zapcore.ParseLevel(cfg.LogLevel)
50+
if err != nil {
51+
// so, the string is not correct, try to parse it as int
52+
logLeverRaw, err := strconv.Atoi(cfg.LogLevel)
53+
if err != nil {
54+
fmt.Printf("Can't parse log level '%v': %v\n", cfg.LogLevel, err)
55+
os.Exit(1)
56+
}
57+
logLever = zapcore.Level(logLeverRaw)
58+
}
59+
60+
log, err := createLogger(cfg.LogEncoder, logLever, syncStderr)
4661

4762
if cfg.FpmPath == "" {
4863
log.Error("php-fpm path not set")

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ require (
2323
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2424
github.com/davecgh/go-spew v1.1.1 // indirect
2525
github.com/klauspost/compress v1.17.9 // indirect
26-
github.com/mitchellh/mapstructure v1.5.0 // indirect
26+
github.com/mitchellh/mapstructure v1.4.3 // indirect
2727
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2828
github.com/otiai10/mint v1.6.3 // indirect
29-
github.com/pelletier/go-toml v1.9.5 // indirect
29+
github.com/pelletier/go-toml v1.9.4 // indirect
3030
github.com/pmezard/go-difflib v1.0.0 // indirect
3131
github.com/prometheus/client_model v0.6.1 // indirect
3232
github.com/prometheus/common v0.55.0 // indirect
33-
github.com/spf13/cast v1.7.1 // indirect
33+
github.com/spf13/cast v1.4.1 // indirect
3434
github.com/spf13/jwalterweatherman v1.1.0 // indirect
3535
golang.org/x/sync v0.10.0 // indirect
3636
google.golang.org/protobuf v1.34.2 // indirect

go.sum

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
66
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
77
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
88
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9-
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
10-
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
119
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1210
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1311
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
@@ -22,16 +20,16 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
2220
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
2321
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
2422
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
25-
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
26-
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
23+
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
24+
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
2725
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
2826
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
2927
github.com/otiai10/copy v1.14.1 h1:5/7E6qsUMBaH5AnQ0sSLzzTg1oTECmcCmT6lvF45Na8=
3028
github.com/otiai10/copy v1.14.1/go.mod h1:oQwrEDDOci3IM8dJF0d8+jnbfPDllW6vUjNc3DoZm9I=
3129
github.com/otiai10/mint v1.6.3 h1:87qsV/aw1F5as1eH1zS/yqHY85ANKVMgkDrf9rcxbQs=
3230
github.com/otiai10/mint v1.6.3/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
33-
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
34-
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
31+
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
32+
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
3533
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3634
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3735
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
@@ -44,8 +42,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
4442
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
4543
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
4644
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
47-
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
48-
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
45+
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
46+
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
4947
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
5048
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
5149
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

0 commit comments

Comments
 (0)