Skip to content

Commit 83fbb3f

Browse files
bietkulsiddharthlatest
authored andcommitted
fix(logs): formatting issues (#64)
* fix: users test * feat: add expired value in permissions * fix: revert isExpired method changes * fix: minor fixes * feat: better logs with logrus * fix: error logging * fix: set default log level to error * Update README.md * fix(log): formatting issues * Update README.md * fix: update readme * fix: minor fixes
1 parent 1164183 commit 83fbb3f

43 files changed

Lines changed: 370 additions & 273 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,39 +80,58 @@ You can build the binary locally by executing the following command from the pro
8080

8181
This produces an executable & plugin libraries in the root project directory. To start the Arc server, run:
8282

83-
./build/arc --log=stdout --env=config/manual.env
83+
```bash
84+
./build/arc --env=config/manual.env --log=info
85+
```
8486

8587
Alternatively, you could execute the following commands to start the server without producing an executable, (but still produce the plugin libraries):
8688

8789
make plugins
88-
go run main.go --log=stdout --env=config/manual.env
90+
go run main.go --env=config/manual.env
91+
8992

9093
**Note**: Running the executable assumes an active Elasticsearch connection whose url is to be provided in the `.env` file. manual.env configures it to be the localhost.
9194

95+
### Logging
96+
Define the run time flag (`log`) to change the default log mode, the possible options are:
97+
98+
#### debug
99+
Most verbose, use this to get logs for elasticsearch interactions.
100+
#### info
101+
Prints the basic information
102+
#### error (default)
103+
Only log the errors
104+
92105
#### TLS Support
93106

94107
You can optionally start arc to serve https requests instead of http requests using the flag https.
95108
You also need to provide the server key & certificate file location through the environment file.
96109
manual.env is configured to use demo server key & certificates, which work for localhost.
97-
go run main.go --log=stdout --env=config/manual.env --https
98-
110+
```bash
111+
go run main.go --log=info --env=config/manual.env --https
112+
```
99113
If you wish to manually test TLS support at localhost,
100114
curl needs to be also passed an extra parameter providing the cacert, in this case.
115+
```bash
101116
curl https://foo:bar@localhost:8000/_user --cacert sample/rootCA.pem
117+
```
102118

103119
#### JWT Key Loading through HTTP
104120

105121
If you wish to test loading JWT Key through HTTP, you can use the following commands to start a HTTP
106122
server serving the key
123+
```bash
107124
cd sample
108125
python -m SimpleHTTPServer 8500
109-
126+
```
110127
Then start arc using the command:
111-
go run main.go --log=stdout --env=config/manual-http-jwt.env
128+
```bash
129+
go run main.go --log=info --env=config/manual-http-jwt.env
130+
```
112131

113132
#### Run Tests
114133

115-
Currently, tests are WIP and implemented for auth and logs modules. You can run tests using:
134+
Currently, tests are WIP and implemented for auth, permissions and users modules. You can run tests using:
116135

117136
go test ./...
118137

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ require (
66
github.com/gobuffalo/packr v1.22.0
77
github.com/google/uuid v1.0.0
88
github.com/gorilla/mux v1.7.1
9-
github.com/hashicorp/go-hclog v0.10.0 // indirect
109
github.com/hashicorp/go-retryablehttp v0.6.3
1110
github.com/olivere/elastic v6.2.21+incompatible
1211
github.com/olivere/elastic/v7 v7.0.4
1312
github.com/robfig/cron v1.1.0
1413
github.com/rogpeppe/go-internal v1.2.2 // indirect
1514
github.com/rs/cors v1.6.0
15+
github.com/siddharthlatest/mustache v0.0.0-20160118163553-00029677272d
16+
github.com/sirupsen/logrus v1.4.2
1617
github.com/smartystreets/goconvey v1.6.4
1718
github.com/ulule/limiter v2.2.0+incompatible
1819
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ github.com/gorilla/sessions v1.1.2/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE
239239
github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
240240
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
241241
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
242+
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
242243
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
243244
github.com/hashicorp/go-hclog v0.10.0 h1:b86HUuA126IcSHyC55WjPo7KtCOVeTCKIjr+3lBhPxI=
244245
github.com/hashicorp/go-hclog v0.10.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
@@ -355,11 +356,14 @@ github.com/shurcooL/highlight_diff v0.0.0-20170515013008-09bb4053de1b/go.mod h1:
355356
github.com/shurcooL/highlight_go v0.0.0-20170515013102-78fb10f4a5f8/go.mod h1:UDKB5a1T23gOMUJrI+uSuH0VRDStOiUVSjBTRDVBVag=
356357
github.com/shurcooL/octicon v0.0.0-20180602230221-c42b0e3b24d9/go.mod h1:eWdoE5JD4R5UVWDucdOPg1g2fqQRq78IQa9zlOV1vpQ=
357358
github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
359+
github.com/siddharthlatest/mustache v0.0.0-20160118163553-00029677272d/go.mod h1:ej02m5sGsrB5WBnM5xLGTfxUInBAbfNFPAsoJxBjn50=
358360
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
359361
github.com/sirupsen/logrus v1.1.0/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
360362
github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
361363
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
362364
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
365+
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
366+
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
363367
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
364368
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
365369
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM=
@@ -459,6 +463,8 @@ golang.org/x/sys v0.0.0-20190102155601-82a175fd1598/go.mod h1:STP8DvDyc/dI5b8T5h
459463
golang.org/x/sys v0.0.0-20190116161447-11f53e031339/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
460464
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
461465
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
466+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
467+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
462468
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
463469
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
464470
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

main.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"flag"
77
"fmt"
88
"io"
9-
"io/ioutil"
10-
"log"
119
"net/http"
1210
"os"
11+
"path"
1312
"path/filepath"
1413
"plugin"
14+
"runtime"
1515
"strconv"
1616
"strings"
1717

@@ -23,14 +23,14 @@ import (
2323
"github.com/robfig/cron"
2424
"github.com/rs/cors"
2525

26-
"gopkg.in/natefinch/lumberjack.v2"
26+
log "github.com/sirupsen/logrus"
2727
)
2828

2929
const logTag = "[cmd]"
3030

3131
var (
3232
envFile string
33-
logFile string
33+
logMode string
3434
listPlugins bool
3535
address string
3636
port int
@@ -57,7 +57,7 @@ var (
5757

5858
func init() {
5959
flag.StringVar(&envFile, "env", ".env", "Path to file with environment variables to load in KEY=VALUE format")
60-
flag.StringVar(&logFile, "log", "", "Process log file")
60+
flag.StringVar(&logMode, "log", "", "Define to change the default log mode(error), other options are: debug(most verbose) and info")
6161
flag.BoolVar(&listPlugins, "plugins", false, "List currently registered plugins")
6262
flag.StringVar(&address, "addr", "", "Address to serve on")
6363
flag.IntVar(&port, "port", 8000, "Port number")
@@ -67,27 +67,29 @@ func init() {
6767

6868
func main() {
6969
flag.Parse()
70+
log.SetReportCaller(true)
71+
log.SetFormatter(&log.TextFormatter{
72+
FullTimestamp: true,
73+
TimestampFormat: "2006/01/02 15:04:05",
74+
DisableLevelTruncation: true,
75+
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
76+
filename := path.Base(f.File)
77+
return "", fmt.Sprintf(" %s:%d", filename, f.Line)
78+
},
79+
})
7080

71-
log.SetFlags(log.LstdFlags | log.Lshortfile)
72-
switch logFile {
73-
case "stdout":
74-
log.SetOutput(os.Stdout)
75-
case "stderr":
76-
log.SetOutput(os.Stderr)
77-
case "":
78-
log.SetOutput(ioutil.Discard)
81+
switch logMode {
82+
case "debug":
83+
log.SetLevel(log.DebugLevel)
84+
case "info":
85+
log.SetLevel(log.InfoLevel)
7986
default:
80-
log.SetOutput(&lumberjack.Logger{
81-
Filename: logFile,
82-
MaxSize: 100,
83-
MaxAge: 14,
84-
MaxBackups: 10,
85-
})
87+
log.SetLevel(log.ErrorLevel)
8688
}
8789

8890
// Load all env vars from envFile
8991
if err := LoadEnvFromFile(envFile); err != nil {
90-
log.Printf("%s: reading env file %q: %v", logTag, envFile, err)
92+
log.Errorln(logTag, ": reading env file", envFile, ": ", err)
9193
}
9294

9395
router := mux.NewRouter().StrictSlash(true)
@@ -97,7 +99,7 @@ func main() {
9799
} else {
98100
_, err := strconv.Atoi(PlanRefreshInterval)
99101
if err != nil {
100-
log.Fatal("PLAN_REFRESH_INTERVAL must be an integer")
102+
log.Fatal("PLAN_REFRESH_INTERVAL must be an integer: ", err)
101103
}
102104
}
103105

@@ -203,7 +205,7 @@ func main() {
203205

204206
// Listen and serve ...
205207
addr := fmt.Sprintf("%s:%d", address, port)
206-
log.Printf("%s: listening on %s", logTag, addr)
208+
log.Println(logTag, ":listening on", addr)
207209
if https {
208210
httpsCert := os.Getenv("HTTPS_CERT")
209211
httpsKey := os.Getenv("HTTPS_KEY")

middleware/interceptor/interceptor.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package interceptor
22

33
import (
4-
"log"
54
"net/http"
65
"net/url"
76
"os"
87

8+
log "github.com/sirupsen/logrus"
9+
910
"github.com/appbaseio/arc/errors"
1011
"github.com/appbaseio/arc/middleware"
1112
"github.com/appbaseio/arc/util"
@@ -26,12 +27,12 @@ func redirect(h http.HandlerFunc) http.HandlerFunc {
2627
rawURL := os.Getenv("ES_CLUSTER_URL")
2728
if rawURL == "" {
2829
err := errors.NewEnvVarNotSetError(envEsClusterURL)
29-
log.Printf("%s: %v", logTag, err)
30+
log.Errorln(logTag, ":", err)
3031
return
3132
}
3233
esURL, err := url.Parse(rawURL)
3334
if err != nil {
34-
log.Printf("%s: error parsing %s=%s: %v", logTag, rawURL, envEsClusterURL, err)
35+
log.Errorln(logTag, ":error parsing", rawURL, "=", envEsClusterURL, ":", err)
3536
return
3637
}
3738

@@ -41,7 +42,7 @@ func redirect(h http.HandlerFunc) http.HandlerFunc {
4142

4243
req, err := redirectRequest(r)
4344
if err != nil {
44-
log.Printf("%s: %v\n", logTag, err)
45+
log.Errorln(logTag, ":", err)
4546
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
4647
return
4748
}

middleware/logger/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package logger
22

33
import (
44
"fmt"
5-
"log"
5+
log "github.com/sirupsen/logrus"
66
"net/http"
77
"strings"
88
"time"

middleware/ratelimiter/ratelimiter.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package ratelimiter
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"net/http"
87
"sync"
98
"time"
109

10+
log "github.com/sirupsen/logrus"
11+
1112
"github.com/appbaseio/arc/middleware"
1213
"github.com/appbaseio/arc/model/category"
1314
"github.com/appbaseio/arc/model/credential"
@@ -60,7 +61,7 @@ func (rl *Ratelimiter) rateLimit(h http.HandlerFunc) http.HandlerFunc {
6061

6162
reqCredential, err := credential.FromContext(ctx)
6263
if err != nil {
63-
log.Printf("%s: %v\n", logTag, err)
64+
log.Errorln(logTag, ":", err)
6465
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
6566
return
6667
}
@@ -70,14 +71,14 @@ func (rl *Ratelimiter) rateLimit(h http.HandlerFunc) http.HandlerFunc {
7071
errMsg := "An error occurred while validating rate limit"
7172
reqPermission, err := permission.FromContext(ctx)
7273
if err != nil {
73-
log.Printf("%s: %v", logTag, err)
74+
log.Errorln(logTag, ":", err)
7475
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
7576
return
7677
}
7778

7879
reqCategory, err := category.FromContext(ctx)
7980
if err != nil {
80-
log.Printf("%s: %v", logTag, err)
81+
log.Errorln(logTag, ":", err)
8182
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
8283
return
8384
}

middleware/validate/acl.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package validate
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"net/http"
87

8+
log "github.com/sirupsen/logrus"
9+
910
"github.com/appbaseio/arc/middleware"
1011
"github.com/appbaseio/arc/model/acl"
1112
"github.com/appbaseio/arc/model/credential"
@@ -26,21 +27,21 @@ func validateACL(h http.HandlerFunc) http.HandlerFunc {
2627
errMsg := "an error occurred while validating request acl"
2728
reqACL, err := acl.FromContext(ctx)
2829
if err != nil {
29-
log.Printf("%s: %v", logTag, err)
30+
log.Errorln(logTag, ":", err)
3031
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
3132
return
3233
}
3334

3435
reqCredential, err := credential.FromContext(ctx)
3536
if err != nil {
36-
log.Printf("%s: %v", logTag, err)
37+
log.Errorln(logTag, ":", err)
3738
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
3839
return
3940
}
4041

4142
ok, err := hasACL(ctx, reqCredential, reqACL)
4243
if err != nil {
43-
log.Printf("%s: %v", logTag, err)
44+
log.Errorln(logTag, ":", err)
4445
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
4546
return
4647
}

middleware/validate/category.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package validate
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"net/http"
87

8+
log "github.com/sirupsen/logrus"
9+
910
"github.com/appbaseio/arc/middleware"
1011
"github.com/appbaseio/arc/model/category"
1112
"github.com/appbaseio/arc/model/credential"
@@ -26,21 +27,21 @@ func validateCategory(h http.HandlerFunc) http.HandlerFunc {
2627
errMsg := "an error occurred while validating request category"
2728
reqCategory, err := category.FromContext(ctx)
2829
if err != nil {
29-
log.Printf("%s: %v", logTag, err)
30+
log.Errorln(logTag, ":", err)
3031
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
3132
return
3233
}
3334

3435
reqCredential, err := credential.FromContext(ctx)
3536
if err != nil {
36-
log.Printf("%s: %v", logTag, err)
37+
log.Errorln(logTag, ":", err)
3738
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
3839
return
3940
}
4041

4142
ok, err := hasCategory(ctx, reqCredential, reqCategory)
4243
if err != nil {
43-
log.Printf("%s: %v", logTag, err)
44+
log.Errorln(logTag, ":", err)
4445
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
4546
return
4647
}

0 commit comments

Comments
 (0)