Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to change log level for server. #346

Merged
merged 6 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}