Skip to content

Commit

Permalink
Allow to change log level for server. (#346)
Browse files Browse the repository at this point in the history
Add possobility to change output verbosity via environment varaible
`LOG_LEVEL`. There are several values: panic, fatal, error, warn or
warning, info, debug and trace.
By default `info` is used.

Co-authored-by: Pete Wagner <1559510+thepwagner@users.noreply.github.com>
Co-authored-by: Sergey Fedorov <oni.strech@gmail.com>
  • Loading branch information
3 people authored Dec 23, 2021
1 parent 3f2973e commit 0d52fb6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* fix: The release-test task is always success.
add: Allow to run release-test on arm machines. (#340, @miry)
* Upgrade `goreleaser`. Support `armv7` and `armv6` oses. (#339, @mitchellrj)
* Allow to change log level for server. (#346, @miry)

# [2.2.0]

Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ EXPOSE 8474
ENTRYPOINT ["/toxiproxy"]
CMD ["-host=0.0.0.0"]

ENV LOG_LEVEL=info

COPY toxiproxy-server-linux-* /toxiproxy
COPY toxiproxy-cli-linux-* /toxiproxy-cli
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ stopping you from creating a client in any other language (see
1. [Upgrading from 1.x](#upgrading-from-toxiproxy-1x)
2. [Populating](#2-populating-toxiproxy)
3. [Using](#3-using-toxiproxy)
4. [Logging](#4-logging)
5. [Toxics](#toxics)
1. [Latency](#latency)
2. [Down](#down)
Expand Down Expand Up @@ -360,6 +361,11 @@ toxiproxy-cli toxic add -t latency -a latency=1000 shopify_test_redis_master

Please consult your respective client library on usage.

### 4. Logging

There are the following log levels: panic, fatal, error, warn or warning, info, debug and trace.
The level could be updated via environment variable `LOG_LEVEL`.

### Toxics

Toxics manipulate the pipe between the client and upstream. They can be added
Expand Down
25 changes: 13 additions & 12 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,20 @@ func (server *ApiServer) PopulateConfig(filename string) {
"config": filename,
"error": err,
}).Error("Error reading config file")
return
}

proxies, err := server.Collection.PopulateJson(file)
if err != nil {
logrus.WithFields(logrus.Fields{
"config": filename,
"error": err,
}).Error("Failed to populate proxies from file")
} else {
proxies, err := server.Collection.PopulateJson(file)
if err != nil {
logrus.WithFields(logrus.Fields{
"config": filename,
"error": err,
}).Error("Failed to populate proxies from file")
} else {
logrus.WithFields(logrus.Fields{
"config": filename,
"proxies": len(proxies),
}).Info("Populated proxies from file")
}
logrus.WithFields(logrus.Fields{
"config": filename,
"proxies": len(proxies),
}).Info("Populated proxies from file")
}
}

Expand Down
2 changes: 1 addition & 1 deletion bin/e2e
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ go run testing/endpoint.go 2>&1 | sed -e 's/^/[web] /' &

echo "=== Starting Toxiproxy"

./dist/toxiproxy-server 2>&1 | sed -e 's/^/[toxiproxy] /' &
LOG_LEVEL=debug ./dist/toxiproxy-server 2>&1 | sed -e 's/^/[toxiproxy] /' &

echo "=== Wait when service are available"

Expand Down
5 changes: 3 additions & 2 deletions cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"strconv"
"strings"

toxiproxyServer "github.com/Shopify/toxiproxy/v2"
toxiproxy "github.com/Shopify/toxiproxy/v2/client"
"github.com/urfave/cli/v2"
terminal "golang.org/x/term"

toxiproxyServer "github.com/Shopify/toxiproxy/v2"
toxiproxy "github.com/Shopify/toxiproxy/v2/client"
)

const (
Expand Down
26 changes: 26 additions & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/sirupsen/logrus"

"github.com/Shopify/toxiproxy/v2"
)

Expand All @@ -27,6 +30,8 @@ func init() {
}

func main() {
setupLogger()

server := toxiproxy.NewServer()
if len(config) > 0 {
server.PopulateConfig(config)
Expand All @@ -42,3 +47,24 @@ func main() {

server.Listen(host, port)
}

func setupLogger() {
val, ok := os.LookupEnv("LOG_LEVEL")
if !ok {
return
}

lvl, err := logrus.ParseLevel(val)
if err == nil {
logrus.SetLevel(lvl)
return
}

valid_levels := make([]string, len(logrus.AllLevels))
for i, level := range logrus.AllLevels {
valid_levels[i] = level.String()
}
levels := strings.Join(valid_levels, ",")

logrus.Errorf("unknown LOG_LEVEL value: \"%s\", use one of: %s", val, levels)
}

0 comments on commit 0d52fb6

Please sign in to comment.