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 2 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
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. [Loggin](#4-logging)
miry marked this conversation as resolved.
Show resolved Hide resolved
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 environmental variable `LOG_LEVEL`.
miry marked this conversation as resolved.
Show resolved Hide resolved

### 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
15 changes: 15 additions & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"syscall"
"time"

"github.com/sirupsen/logrus"

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

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

func main() {
setupLogger()

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

server.Listen(host, port)
}

func setupLogger() {
val, ok := os.LookupEnv("LOG_LEVEL")
if ok {
lvl, err := logrus.ParseLevel(val)
if err != nil {
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: is err is worth logging here, perhaps at .Error()?

Since we used LookupEnv, we know the user provided a LOG_LEVEL value. They may want feedback if that value was incorrect:

  • err will give them not a valid logrus Level: FOO_BAR
  • Overkill, but you could build a message from logrus.AllLevels to include what levels are accepted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thepwagner Updated the code to print error message in case the LOG_LEVEL is not valid.

}
logrus.SetLevel(lvl)
}
}