Skip to content

Commit 90aea45

Browse files
Revert "feat: improve logging with logrus (#60)" (#63)
This reverts commit ecf84ba.
1 parent ecf84ba commit 90aea45

42 files changed

Lines changed: 214 additions & 294 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: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,56 +80,39 @@ 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 --env=config/manual.env
83+
./build/arc --log=stdout --env=config/manual.env
8484

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

8787
make plugins
88-
go run main.go --env=config/manual.env
89-
88+
go run main.go --log=stdout --env=config/manual.env
9089

9190
**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.
9291

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

10594
You can optionally start arc to serve https requests instead of http requests using the flag https.
10695
You also need to provide the server key & certificate file location through the environment file.
10796
manual.env is configured to use demo server key & certificates, which work for localhost.
108-
```bash
10997
go run main.go --log=stdout --env=config/manual.env --https
110-
```
98+
11199
If you wish to manually test TLS support at localhost,
112100
curl needs to be also passed an extra parameter providing the cacert, in this case.
113-
```bash
114101
curl https://foo:bar@localhost:8000/_user --cacert sample/rootCA.pem
115-
```
116102

117103
#### JWT Key Loading through HTTP
118104

119105
If you wish to test loading JWT Key through HTTP, you can use the following commands to start a HTTP
120106
server serving the key
121-
```bash
122107
cd sample
123108
python -m SimpleHTTPServer 8500
124-
```
109+
125110
Then start arc using the command:
126-
```bash
127111
go run main.go --log=stdout --env=config/manual-http-jwt.env
128-
```
129112

130113
#### Run Tests
131114

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

134117
go test ./...
135118

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ require (
1313
github.com/robfig/cron v1.1.0
1414
github.com/rogpeppe/go-internal v1.2.2 // indirect
1515
github.com/rs/cors v1.6.0
16-
github.com/siddharthlatest/mustache v0.0.0-20160118163553-00029677272d
17-
github.com/sirupsen/logrus v1.4.2
1816
github.com/smartystreets/goconvey v1.6.4
1917
github.com/ulule/limiter v2.2.0+incompatible
2018
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,11 @@ github.com/shurcooL/highlight_diff v0.0.0-20170515013008-09bb4053de1b/go.mod h1:
355355
github.com/shurcooL/highlight_go v0.0.0-20170515013102-78fb10f4a5f8/go.mod h1:UDKB5a1T23gOMUJrI+uSuH0VRDStOiUVSjBTRDVBVag=
356356
github.com/shurcooL/octicon v0.0.0-20180602230221-c42b0e3b24d9/go.mod h1:eWdoE5JD4R5UVWDucdOPg1g2fqQRq78IQa9zlOV1vpQ=
357357
github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
358-
github.com/siddharthlatest/mustache v0.0.0-20160118163553-00029677272d/go.mod h1:ej02m5sGsrB5WBnM5xLGTfxUInBAbfNFPAsoJxBjn50=
359358
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
360359
github.com/sirupsen/logrus v1.1.0/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
361360
github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
362361
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
363362
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
364-
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
365-
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
366363
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
367364
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
368365
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM=
@@ -462,7 +459,6 @@ golang.org/x/sys v0.0.0-20190102155601-82a175fd1598/go.mod h1:STP8DvDyc/dI5b8T5h
462459
golang.org/x/sys v0.0.0-20190116161447-11f53e031339/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
463460
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
464461
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
465-
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
466462
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
467463
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
468464
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

main.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"flag"
66
"fmt"
77
"io"
8+
"io/ioutil"
9+
"log"
810
"net/http"
911
"os"
1012
"path/filepath"
@@ -20,14 +22,14 @@ import (
2022
"github.com/robfig/cron"
2123
"github.com/rs/cors"
2224

23-
log "github.com/sirupsen/logrus"
25+
"gopkg.in/natefinch/lumberjack.v2"
2426
)
2527

2628
const logTag = "[cmd]"
2729

2830
var (
2931
envFile string
30-
logMode string
32+
logFile string
3133
listPlugins bool
3234
address string
3335
port int
@@ -47,7 +49,7 @@ var (
4749

4850
func init() {
4951
flag.StringVar(&envFile, "env", ".env", "Path to file with environment variables to load in KEY=VALUE format")
50-
flag.StringVar(&logMode, "log", "", "Process log file")
52+
flag.StringVar(&logFile, "log", "", "Process log file")
5153
flag.BoolVar(&listPlugins, "plugins", false, "List currently registered plugins")
5254
flag.StringVar(&address, "addr", "", "Address to serve on")
5355
flag.IntVar(&port, "port", 8000, "Port number")
@@ -58,23 +60,26 @@ func init() {
5860
func main() {
5961
flag.Parse()
6062

61-
log.SetFormatter(&log.TextFormatter{
62-
FullTimestamp: true,
63-
TimestampFormat: "2006/01/02 15:04:05",
64-
})
65-
66-
switch logMode {
67-
case "debug":
68-
log.SetLevel(log.DebugLevel)
69-
case "info":
70-
log.SetLevel(log.InfoLevel)
63+
log.SetFlags(log.LstdFlags | log.Lshortfile)
64+
switch logFile {
65+
case "stdout":
66+
log.SetOutput(os.Stdout)
67+
case "stderr":
68+
log.SetOutput(os.Stderr)
69+
case "":
70+
log.SetOutput(ioutil.Discard)
7171
default:
72-
log.SetLevel(log.ErrorLevel)
72+
log.SetOutput(&lumberjack.Logger{
73+
Filename: logFile,
74+
MaxSize: 100,
75+
MaxAge: 14,
76+
MaxBackups: 10,
77+
})
7378
}
7479

7580
// Load all env vars from envFile
7681
if err := LoadEnvFromFile(envFile); err != nil {
77-
log.Error(logTag, ": reading env file", envFile, ": ", err)
82+
log.Printf("%s: reading env file %q: %v", logTag, envFile, err)
7883
}
7984

8085
router := mux.NewRouter().StrictSlash(true)
@@ -84,7 +89,7 @@ func main() {
8489
} else {
8590
_, err := strconv.Atoi(PlanRefreshInterval)
8691
if err != nil {
87-
log.Fatal("PLAN_REFRESH_INTERVAL must be an integer: ", err)
92+
log.Fatal("PLAN_REFRESH_INTERVAL must be an integer")
8893
}
8994
}
9095

middleware/interceptor/interceptor.go

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

33
import (
4+
"log"
45
"net/http"
56
"net/url"
67
"os"
78

8-
log "github.com/sirupsen/logrus"
9-
109
"github.com/appbaseio/arc/errors"
1110
"github.com/appbaseio/arc/middleware"
1211
"github.com/appbaseio/arc/util"
@@ -27,12 +26,12 @@ func redirect(h http.HandlerFunc) http.HandlerFunc {
2726
rawURL := os.Getenv("ES_CLUSTER_URL")
2827
if rawURL == "" {
2928
err := errors.NewEnvVarNotSetError(envEsClusterURL)
30-
log.Error(logTag, ": ", err)
29+
log.Printf("%s: %v", logTag, err)
3130
return
3231
}
3332
esURL, err := url.Parse(rawURL)
3433
if err != nil {
35-
log.Error(logTag, ": error parsing ", rawURL, "=", envEsClusterURL, ": ", err)
34+
log.Printf("%s: error parsing %s=%s: %v", logTag, rawURL, envEsClusterURL, err)
3635
return
3736
}
3837

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

4342
req, err := redirectRequest(r)
4443
if err != nil {
45-
log.Error(logTag, ": ", err)
44+
log.Printf("%s: %v\n", logTag, err)
4645
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
4746
return
4847
}

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 "github.com/sirupsen/logrus"
5+
"log"
66
"net/http"
77
"strings"
88
"time"

middleware/ratelimiter/ratelimiter.go

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

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

6261
reqCredential, err := credential.FromContext(ctx)
6362
if err != nil {
64-
log.Error(logTag, ": ", err)
63+
log.Printf("%s: %v\n", logTag, err)
6564
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
6665
return
6766
}
@@ -71,14 +70,14 @@ func (rl *Ratelimiter) rateLimit(h http.HandlerFunc) http.HandlerFunc {
7170
errMsg := "An error occurred while validating rate limit"
7271
reqPermission, err := permission.FromContext(ctx)
7372
if err != nil {
74-
log.Error(logTag, ": ", err)
73+
log.Printf("%s: %v", logTag, err)
7574
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
7675
return
7776
}
7877

7978
reqCategory, err := category.FromContext(ctx)
8079
if err != nil {
81-
log.Error(logTag, ": ", err)
80+
log.Printf("%s: %v", logTag, err)
8281
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
8382
return
8483
}

middleware/validate/acl.go

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

8-
log "github.com/sirupsen/logrus"
9-
109
"github.com/appbaseio/arc/middleware"
1110
"github.com/appbaseio/arc/model/acl"
1211
"github.com/appbaseio/arc/model/credential"
@@ -27,21 +26,21 @@ func validateACL(h http.HandlerFunc) http.HandlerFunc {
2726
errMsg := "an error occurred while validating request acl"
2827
reqACL, err := acl.FromContext(ctx)
2928
if err != nil {
30-
log.Error(logTag, ": ", err)
29+
log.Printf("%s: %v", logTag, err)
3130
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
3231
return
3332
}
3433

3534
reqCredential, err := credential.FromContext(ctx)
3635
if err != nil {
37-
log.Error(logTag, ": ", err)
36+
log.Printf("%s: %v", logTag, err)
3837
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
3938
return
4039
}
4140

4241
ok, err := hasACL(ctx, reqCredential, reqACL)
4342
if err != nil {
44-
log.Error(logTag, ": ", err)
43+
log.Printf("%s: %v", logTag, err)
4544
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
4645
return
4746
}

middleware/validate/category.go

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

8-
log "github.com/sirupsen/logrus"
9-
109
"github.com/appbaseio/arc/middleware"
1110
"github.com/appbaseio/arc/model/category"
1211
"github.com/appbaseio/arc/model/credential"
@@ -27,21 +26,21 @@ func validateCategory(h http.HandlerFunc) http.HandlerFunc {
2726
errMsg := "an error occurred while validating request category"
2827
reqCategory, err := category.FromContext(ctx)
2928
if err != nil {
30-
log.Error(logTag, ": ", err)
29+
log.Printf("%s: %v", logTag, err)
3130
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
3231
return
3332
}
3433

3534
reqCredential, err := credential.FromContext(ctx)
3635
if err != nil {
37-
log.Error(logTag, ": ", err)
36+
log.Printf("%s: %v", logTag, err)
3837
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
3938
return
4039
}
4140

4241
ok, err := hasCategory(ctx, reqCredential, reqCategory)
4342
if err != nil {
44-
log.Error(logTag, ": ", err)
43+
log.Printf("%s: %v", logTag, err)
4544
util.WriteBackError(w, errMsg, http.StatusInternalServerError)
4645
return
4746
}

middleware/validate/expiry.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package validate
22

33
import (
44
"fmt"
5+
"log"
56
"net/http"
67

7-
log "github.com/sirupsen/logrus"
8-
98
"github.com/appbaseio/arc/middleware"
109
"github.com/appbaseio/arc/model/credential"
1110
"github.com/appbaseio/arc/model/permission"
@@ -23,22 +22,22 @@ func validateExpiry(h http.HandlerFunc) http.HandlerFunc {
2322

2423
reqCredential, err := credential.FromContext(ctx)
2524
if err != nil {
26-
log.Error(logTag, ": ", err)
25+
log.Printf("%s: %v", logTag, err)
2726
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
2827
return
2928
}
3029

3130
if reqCredential == credential.Permission {
3231
reqPermission, err := permission.FromContext(ctx)
3332
if err != nil {
34-
log.Error(logTag, ": ", err)
33+
log.Printf("%s: %v", logTag, err)
3534
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
3635
return
3736
}
3837

3938
expired, err := reqPermission.IsExpired()
4039
if err != nil {
41-
log.Error(logTag, ": ", err)
40+
log.Printf("%s: %v", logTag, err)
4241
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
4342
return
4443
}

0 commit comments

Comments
 (0)