Skip to content

Commit b4606ff

Browse files
committed
Cleanup log handling
1 parent 1352182 commit b4606ff

14 files changed

+110
-51
lines changed

api/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ func waitForTermination(log logrus.FieldLogger, done <-chan struct{}) {
7373
}
7474

7575
// NewAPI instantiates a new REST API using the default version.
76-
func NewAPI(globalConfig *conf.GlobalConfiguration, db *gorm.DB) *API {
77-
return NewAPIWithVersion(context.Background(), globalConfig, db, defaultVersion)
76+
func NewAPI(globalConfig *conf.GlobalConfiguration, log logrus.FieldLogger, db *gorm.DB) *API {
77+
return NewAPIWithVersion(context.Background(), globalConfig, log, db, defaultVersion)
7878
}
7979

8080
// NewAPIWithVersion instantiates a new REST API.
81-
func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfiguration, db *gorm.DB, version string) *API {
81+
func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfiguration, log logrus.FieldLogger, db *gorm.DB, version string) *API {
8282
api := &API{
8383
config: globalConfig,
8484
db: db,
@@ -87,7 +87,7 @@ func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfigurati
8787
}
8888

8989
xffmw, _ := xff.Default()
90-
logger := newStructuredLogger(logrus.StandardLogger())
90+
logger := newStructuredLogger(log)
9191

9292
r := newRouter()
9393
r.UseBypass(xffmw.Handler)

api/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http/httptest"
77
"testing"
88

9+
"github.com/sirupsen/logrus"
910
"github.com/sirupsen/logrus/hooks/test"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
@@ -25,7 +26,7 @@ func TestTraceWrapper(t *testing.T) {
2526

2627
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, config, "")
2728
require.NoError(t, err)
28-
api := NewAPIWithVersion(ctx, globalConfig, nil, "")
29+
api := NewAPIWithVersion(ctx, globalConfig, logrus.StandardLogger(), nil, "")
2930

3031
server := httptest.NewServer(api.handler)
3132
defer server.Close()

api/instance_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ type InstanceTestSuite struct {
2525
}
2626

2727
func (ts *InstanceTestSuite) SetupTest() {
28-
globalConfig, err := conf.LoadGlobal("test.env")
28+
globalConfig, log, err := conf.LoadGlobal("test.env")
2929
require.NoError(ts.T(), err)
3030
globalConfig.OperatorToken = operatorToken
3131
globalConfig.MultiInstanceMode = true
32-
db, err := models.Connect(globalConfig)
32+
db, err := models.Connect(globalConfig, log)
3333
require.NoError(ts.T(), err)
3434

35-
api := NewAPI(globalConfig, db)
35+
api := NewAPI(globalConfig, log, db)
3636
ts.API = api
3737

3838
// Cleanup existing instance

api/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010
"github.com/sirupsen/logrus"
1111
)
1212

13-
func newStructuredLogger(logger *logrus.Logger) func(next http.Handler) http.Handler {
13+
func newStructuredLogger(logger logrus.FieldLogger) func(next http.Handler) http.Handler {
1414
return chimiddleware.RequestLogger(&structuredLogger{logger})
1515
}
1616

1717
type structuredLogger struct {
18-
Logger *logrus.Logger
18+
Logger logrus.FieldLogger
1919
}
2020

2121
func (l *structuredLogger) NewLogEntry(r *http.Request) chimiddleware.LogEntry {
22-
entry := &structuredLoggerEntry{Logger: logrus.NewEntry(l.Logger)}
22+
entry := &structuredLoggerEntry{Logger: l.Logger}
2323
logFields := logrus.Fields{
2424
"component": "api",
2525
"method": r.Method,

api/middleware_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ type MiddlewareTestSuite struct {
2121
}
2222

2323
func (ts *MiddlewareTestSuite) SetupTest() {
24-
globalConfig, err := conf.LoadGlobal("test.env")
24+
globalConfig, log, err := conf.LoadGlobal("test.env")
2525
require.NoError(ts.T(), err)
2626
globalConfig.MultiInstanceMode = true
27-
db, err := models.Connect(globalConfig)
27+
db, err := models.Connect(globalConfig, log)
2828
require.NoError(ts.T(), err)
2929

30-
api := NewAPI(globalConfig, db)
30+
api := NewAPI(globalConfig, log, db)
3131
ts.API = api
3232
}
3333

api/payments_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func TestPaymentsRefund(t *testing.T) {
253253
err = signHTTPRequest(r, testAdminToken("magical-unicorn", ""), test.Config.JWT.Secret)
254254
require.NoError(t, err)
255255

256-
NewAPIWithVersion(ctx, test.GlobalConfig, test.DB, defaultVersion).handler.ServeHTTP(w, r)
256+
NewAPIWithVersion(ctx, test.GlobalConfig, logrus.StandardLogger(), test.DB, defaultVersion).handler.ServeHTTP(w, r)
257257

258258
rsp := new(models.Transaction)
259259
extractPayload(t, http.StatusOK, w, rsp)
@@ -674,7 +674,7 @@ func TestPaymentPreauthorize(t *testing.T) {
674674
globalConfig := new(conf.GlobalConfiguration)
675675
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "")
676676
require.NoError(t, err)
677-
NewAPIWithVersion(ctx, test.GlobalConfig, test.DB, "").handler.ServeHTTP(recorder, req)
677+
NewAPIWithVersion(ctx, test.GlobalConfig, logrus.StandardLogger(), test.DB, "").handler.ServeHTTP(recorder, req)
678678

679679
rsp := payments.PreauthorizationResult{}
680680
extractPayload(t, http.StatusOK, recorder, &rsp)
@@ -715,7 +715,7 @@ func TestPaymentPreauthorize(t *testing.T) {
715715
globalConfig := new(conf.GlobalConfiguration)
716716
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "")
717717
require.NoError(t, err)
718-
NewAPIWithVersion(ctx, test.GlobalConfig, test.DB, "").handler.ServeHTTP(recorder, req)
718+
NewAPIWithVersion(ctx, test.GlobalConfig, logrus.StandardLogger(), test.DB, "").handler.ServeHTTP(recorder, req)
719719

720720
rsp := payments.PreauthorizationResult{}
721721
extractPayload(t, http.StatusOK, recorder, &rsp)

api/utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func db(t *testing.T) (*gorm.DB, *conf.GlobalConfiguration, *conf.Configuration,
5555
globalConfig.DB.Driver = "sqlite3"
5656
globalConfig.DB.URL = f.Name()
5757

58-
db, err := models.Connect(globalConfig)
58+
db, err := models.Connect(globalConfig, logrus.StandardLogger())
5959
if err != nil {
6060
assert.FailNow(t, "failed to connect to db: "+err.Error())
6161
}
@@ -386,7 +386,7 @@ func (r *RouteTest) TestEndpoint(method string, url string, body io.Reader, toke
386386
globalConfig := new(conf.GlobalConfiguration)
387387
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, r.Config, "")
388388
require.NoError(r.T, err)
389-
NewAPIWithVersion(ctx, r.GlobalConfig, r.DB, "").handler.ServeHTTP(recorder, req)
389+
NewAPIWithVersion(ctx, r.GlobalConfig, logrus.StandardLogger(), r.DB, "").handler.ServeHTTP(recorder, req)
390390

391391
return recorder
392392
}

cmd/migrate_cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ var migrateCmd = cobra.Command{
1515
},
1616
}
1717

18-
func migrate(globalConfig *conf.GlobalConfiguration, config *conf.Configuration) {
19-
db, err := models.Connect(globalConfig)
18+
func migrate(globalConfig *conf.GlobalConfiguration, log logrus.FieldLogger, config *conf.Configuration) {
19+
db, err := models.Connect(globalConfig, log)
2020
if err != nil {
2121
logrus.Fatalf("Error opening database: %+v", err)
2222
}

cmd/multi_cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ var multiCmd = cobra.Command{
1818
}
1919

2020
func multi(cmd *cobra.Command, args []string) {
21-
globalConfig, err := conf.LoadGlobal(configFile)
21+
globalConfig, log, err := conf.LoadGlobal(configFile)
2222
if err != nil {
2323
logrus.Fatalf("Failed to load configuration: %+v", err)
2424
}
2525
if globalConfig.OperatorToken == "" {
2626
logrus.Fatal("Operator token secret is required")
2727
}
2828

29-
db, err := models.Connect(globalConfig)
29+
db, err := models.Connect(globalConfig, log.WithField("component", "db"))
3030
if err != nil {
3131
logrus.Fatalf("Error opening database: %+v", err)
3232
}
3333
defer db.Close()
3434

35-
bgDB, err := models.Connect(globalConfig)
35+
bgDB, err := models.Connect(globalConfig, log.WithField("component", "db").WithField("bgdb", true))
3636
if err != nil {
3737
logrus.Fatalf("Error opening database: %+v", err)
3838
}
3939
defer bgDB.Close()
4040

4141
globalConfig.MultiInstanceMode = true
42-
api := api.NewAPIWithVersion(context.Background(), globalConfig, db.Debug(), Version)
42+
api := api.NewAPIWithVersion(context.Background(), globalConfig, log, db.Debug(), Version)
4343

4444
l := fmt.Sprintf("%v:%v", globalConfig.API.Host, globalConfig.API.Port)
4545
logrus.Infof("GoCommerce API started on: %s", l)

cmd/root_cmd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func RootCmd() *cobra.Command {
2525
return &rootCmd
2626
}
2727

28-
func execWithConfig(cmd *cobra.Command, fn func(globalConfig *conf.GlobalConfiguration, config *conf.Configuration)) {
29-
globalConfig, err := conf.LoadGlobal(configFile)
28+
func execWithConfig(cmd *cobra.Command, fn func(globalConfig *conf.GlobalConfiguration, log logrus.FieldLogger, config *conf.Configuration)) {
29+
globalConfig, log, err := conf.LoadGlobal(configFile)
3030
if err != nil {
3131
logrus.Fatalf("Failed to load configuration: %+v", err)
3232
}
@@ -35,5 +35,5 @@ func execWithConfig(cmd *cobra.Command, fn func(globalConfig *conf.GlobalConfigu
3535
logrus.Fatalf("Failed to load configuration: %+v", err)
3636
}
3737

38-
fn(globalConfig, config)
38+
fn(globalConfig, log, config)
3939
}

0 commit comments

Comments
 (0)