Skip to content

Commit cd16ad8

Browse files
Move logger to separate directory and misc logging fixes (#70)
1 parent b947d67 commit cd16ad8

16 files changed

+65
-115
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ issues:
180180
- path: pkg/protostore/protostore_test.go
181181
linters:
182182
- maintidx
183-
- path: pkg/datastore/logger.go
183+
- path: pkg/logutils/logger.go
184184
linters:
185185
- gochecknoglobals
186186
- path: pkg/datastore/sql_struct.go

docs/DOCUMENTATION.md

+33-93
Large diffs are not rendered by default.

pkg/authorizer/metadata_authorizer.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
2828
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/errors"
29+
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
2930
)
3031

3132
const (
@@ -106,11 +107,11 @@ func (s *MetadataBasedAuthorizer) GetMatchingDbRole(ctx context.Context, tableNa
106107

107108
func (s *MetadataBasedAuthorizer) Configure(tableName string, roleMapping map[string]dbrole.DbRole) {
108109
if s.roleMapping == nil {
109-
// TRACE("RoleMapping: setting to NEWMAP")
110+
TRACE("RoleMapping: setting to NEWMAP")
110111
s.roleMapping = make(map[string]map[string]dbrole.DbRole)
111112
}
112113
s.roleMapping[tableName] = roleMapping
113-
// TRACE("RoleMapping: configured for table %s: %+v", tableName, s.roleMapping)
114+
TRACE("RoleMapping: configured for table %s: %+v", tableName, s.roleMapping)
114115
}
115116

116117
func (s *MetadataBasedAuthorizer) GetAuthContext(orgId string, roles ...string) context.Context {

pkg/datastore/database.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/authorizer"
5050
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
5151
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/errors"
52+
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
5253
)
5354

5455
const (

pkg/datastore/datastore_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/datastore"
3838
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
3939
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/errors"
40+
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
4041
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/test"
4142
"github.com/vmware-labs/multi-tenant-persistence-for-saas/test/pb"
4243
)
@@ -179,17 +180,17 @@ func testCrud(t *testing.T, ds datastore.DataStore, ctx context.Context, myCokeA
179180
}
180181

181182
func TestMain(m *testing.M) {
182-
LOG = datastore.GetCompLogger()
183+
LOG = logutils.GetCompLogger()
183184

184185
code := m.Run()
185186
os.Exit(code)
186187
}
187188

188189
func BenchmarkCrud(b *testing.B) {
189-
logger := datastore.GetLogger()
190+
logger := logutils.GetLogger()
190191
logger.SetLevel(logrus.FatalLevel)
191192
logger.SetOutput(io.Discard)
192-
LOG = logger.WithField(datastore.COMP, datastore.SAAS_PERSISTENCE)
193+
LOG = logger.WithField(logutils.COMP, logutils.SAAS_PERSISTENCE)
193194

194195
var t testing.T
195196
ds, _ := SetupDataStore("BenchmarkCrud")

pkg/datastore/datastore_with_txcontext_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/authorizer"
3232
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/datastore"
3333
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/errors"
34+
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
3435
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/test"
3536
)
3637

@@ -123,10 +124,10 @@ func testTxCrud(t *testing.T, ds datastore.DataStore, ctx context.Context, user1
123124
}
124125

125126
func BenchmarkTxCrud(b *testing.B) {
126-
logger := datastore.GetLogger()
127+
logger := logutils.GetLogger()
127128
logger.SetLevel(logrus.FatalLevel)
128129
logger.SetOutput(io.Discard)
129-
LOG = logger.WithField(datastore.COMP, datastore.SAAS_PERSISTENCE)
130+
LOG = logger.WithField(logutils.COMP, logutils.SAAS_PERSISTENCE)
130131

131132
var t testing.T
132133
ds, _ := SetupDataStore("BenchmarkTxCrud")

pkg/datastore/db_user.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strconv"
2626

2727
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
28+
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
2829
)
2930

3031
// Specifications for database user.
@@ -139,7 +140,7 @@ func getDbUsers(tableName string, tenantRole, instancerRole bool, isTableTenante
139140
dbUsers[i].password = getPassword(string(dbUsers[i].username))
140141
dbUsers[i].policyName = getRlsPolicyName(string(dbUsers[i].username), tableName)
141142
}
142-
TRACE("Returning DB user specs for table %q:\n\t[roleT=%s, roleI=%s, tableT=%s, tableI=%s]\n\tdbUsers - %+v\n",
143+
TRACE("Returning DB user specs for table %q:\n\t[tenantRole=%t, instancerRole=%t, isTableTenanted=%t, isTableInstanced=%t]\n\n\tdbUsers - %+v\n",
143144
tableName, tenantRole, instancerRole, isTableTenanted, isTableInstanced, dbUsers)
144145
return dbUsers
145146
}

pkg/datastore/example_multiinstance_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/authorizer"
99
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/datastore"
1010
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
11+
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
1112
)
1213

1314
type Person struct {
@@ -39,7 +40,7 @@ func ExampleDataStore_multiInstance() {
3940
ProdInstanceCtx := instancer.WithInstanceId(ServiceAdminCtx, "Prod")
4041

4142
// Initializes the Datastore using the metadata authorizer and connection details obtained from the ENV variables.
42-
ds, err := datastore.FromEnvWithDB(datastore.GetCompLogger(), mdAuthorizer, instancer, "ExampleDataStore_multiInstance")
43+
ds, err := datastore.FromEnvWithDB(logutils.GetCompLogger(), mdAuthorizer, instancer, "ExampleDataStore_multiInstance")
4344
defer ds.Reset()
4445
if err != nil {
4546
log.Fatalf("datastore initialization from env errored: %s", err)

pkg/datastore/example_multitenancy_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/authorizer"
99
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/datastore"
1010
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
11+
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
1112
)
1213

1314
type User struct {
@@ -36,7 +37,7 @@ func ExampleDataStore_multiTenancy() {
3637
PepsiOrgCtx := mdAuthorizer.GetAuthContext("Pepsi", TENANT_ADMIN)
3738

3839
// Initializes the Datastore using the metadata authorizer and connection details obtained from the ENV variables.
39-
ds, err := datastore.FromEnvWithDB(datastore.GetCompLogger(), mdAuthorizer, nil, "ExampleDataStore_multiTenancy")
40+
ds, err := datastore.FromEnvWithDB(logutils.GetCompLogger(), mdAuthorizer, nil, "ExampleDataStore_multiTenancy")
4041
defer ds.Reset()
4142
if err != nil {
4243
log.Fatalf("datastore initialization from env errored: %s", err)

pkg/datastore/helper.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/authorizer"
3434
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
3535
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/errors"
36+
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
3637
)
3738

3839
const (
@@ -113,7 +114,7 @@ func FromEnvWithDB(l *logrus.Entry, a authorizer.Authorizer, instancer authorize
113114
}
114115

115116
func FromConfig(l *logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer, cfg DBConfig) (d DataStore, err error) {
116-
gl := GetGormLogger(l)
117+
gl := logutils.GetGormLogger(l)
117118
dbConnInitializer := func(db *relationalDb, dbRole dbrole.DbRole) error {
118119
db.Lock()
119120
defer db.Unlock()

pkg/datastore/noinstancer_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/authorizer"
1414
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/datastore"
1515
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
16+
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
1617
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/test"
1718
)
1819

@@ -41,7 +42,7 @@ func TestDataStoreWithoutInstancer(t *testing.T) {
4142
DevInstanceCtx := instancer.WithInstanceId(ServiceAdminCtx, "Dev")
4243
ProdInstanceCtx := instancer.WithInstanceId(ServiceAdminCtx, "Prod")
4344

44-
ds, _ := datastore.FromEnv(datastore.GetCompLogger(), mdAuthorizer, nil)
45+
ds, _ := datastore.FromEnvWithDB(logutils.GetCompLogger(), mdAuthorizer, nil, "TestDataStoreWithoutInstancer")
4546
defer ds.Reset()
4647
roleMapping := map[string]dbrole.DbRole{
4748
SERVICE_AUDITOR: dbrole.READER,

pkg/datastore/sql_struct.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"sync"
2525

2626
"gorm.io/gorm/schema"
27+
28+
. "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
2729
)
2830

2931
// Library processing all the golang tags in the struct into SQL domain

pkg/datastore/logger.go pkg/logutils/logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// specific language governing permissions and limitations
1717
// under the License.
1818

19-
package datastore
19+
package logutils
2020

2121
import (
2222
"context"

pkg/protostore/example_protostore_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/authorizer"
88
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/datastore"
99
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
10+
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
1011
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/protostore"
1112
"github.com/vmware-labs/multi-tenant-persistence-for-saas/test/pb"
1213
)
1314

1415
func ExampleProtoStore() {
1516
// Initialize protostore with proper logger, authorizer and datastore
16-
myLogger := datastore.GetCompLogger()
17+
myLogger := logutils.GetCompLogger()
1718
mdAuthorizer := &authorizer.MetadataBasedAuthorizer{}
1819
myDatastore, _ := datastore.FromEnvWithDB(myLogger, mdAuthorizer, nil, "ExampleProtoStore")
1920
defer myDatastore.Reset()

pkg/protostore/protostore_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ The test case will first initialize "producer's" ProtoStore instance and later "
304304
*/
305305
func TestProtoStoreInDbFindAll_DifferentProducerConsumer_InitProducerFirst(t *testing.T) {
306306
const dbName = "TestProtoStoreInDbFindAll_DifferentProducerConsumer"
307-
t.Log("Dropping all DB tables...")
308307
t.Log("Creating producer's ProtoStore instance (instancer ENABLED)")
309308
pNoInstancer := setupDbContextNoInstancer(t, dbName, true /* dropTables */)
310309

@@ -318,7 +317,6 @@ func TestProtoStoreInDbFindAll_DifferentProducerConsumer_InitProducerFirst(t *te
318317
// is initialized first.
319318
func TestProtoStoreInDbFindAll_DifferentProducerConsumer_InitConsumerFirst(t *testing.T) {
320319
const dbName = "TestProtoStoreInDbFindAll_DifferentProducerConsumer"
321-
t.Log("Dropping all DB tables...")
322320
t.Log("Creating consumer's ProtoStore instance (instancer DISABLED)")
323321
pWithInstancer := setupDbContextWithInstancer(t, dbName, true /* dropTables */)
324322

@@ -337,7 +335,6 @@ func testProtoStoreInDbFindAll_DifferentProducerConsumer(t *testing.T, pNoInstan
337335
t.Helper()
338336
assert := assert.New(t)
339337

340-
t.Log("====================START OF UNIT TEST====================")
341338
memMsg1, memMsg2 := pb.Memory{}, pb.Memory{}
342339
for _, protoMsg := range []proto.Message{&memMsg1, &memMsg2} {
343340
_ = faker.FakeData(protoMsg)

test/helper.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/datastore"
2525
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
26+
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/logutils"
2627
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/protostore"
2728
)
2829

@@ -32,11 +33,11 @@ func SetupDataStore(dbName string) (datastore.DataStore, protostore.ProtoStore)
3233
if err != nil {
3334
log.Fatalf("Failed to create database from cfg %+v", cfg)
3435
}
35-
ds, err := datastore.FromConfig(datastore.GetCompLogger(), TestMetadataAuthorizer, TestInstancer, cfg)
36+
ds, err := datastore.FromConfig(logutils.GetCompLogger(), TestMetadataAuthorizer, TestInstancer, cfg)
3637
if err != nil {
3738
log.Fatalf("Failed to initialize datastore from cfg %+v", cfg)
3839
}
39-
ps := protostore.GetProtoStore(datastore.GetCompLogger(), ds)
40+
ps := protostore.GetProtoStore(logutils.GetCompLogger(), ds)
4041
return ds, ps
4142
}
4243

@@ -46,11 +47,11 @@ func SetupDataStoreNoInstancer(dbName string) (datastore.DataStore, protostore.P
4647
if err != nil {
4748
log.Fatalf("Failed to create database from cfg %+v", cfg)
4849
}
49-
ds, err := datastore.FromConfig(datastore.GetCompLogger(), TestMetadataAuthorizer, nil /* instancer */, cfg)
50+
ds, err := datastore.FromConfig(logutils.GetCompLogger(), TestMetadataAuthorizer, nil /* instancer */, cfg)
5051
if err != nil {
5152
log.Fatalf("Failed to initialize datastore from cfg %+v", cfg)
5253
}
53-
ps := protostore.GetProtoStore(datastore.GetCompLogger(), ds)
54+
ps := protostore.GetProtoStore(logutils.GetCompLogger(), ds)
5455
return ds, ps
5556
}
5657

0 commit comments

Comments
 (0)