3
0

Ability to disable action logging

Introduce new policy type (NewPolicyNone, MakeDisabledPolicy),
add ACTIONLOG_ENABLED configuration (true by default)
This commit is contained in:
Denis Arh
2020-07-09 16:29:01 +02:00
parent f2b2279547
commit 08e1fb7a14
7 changed files with 92 additions and 14 deletions

View File

@@ -83,11 +83,14 @@ func Initialize(ctx context.Context, log *zap.Logger, c Config) (err error) {
DefaultLogger = log.Named("service")
{
tee := log
tee := zap.NewNop()
policy := actionlog.MakeProductionPolicy()
if c.ActionLog.Debug {
tee = zap.NewNop()
if !c.ActionLog.Enabled {
policy = actionlog.MakeDisabledPolicy()
} else if c.ActionLog.Debug {
policy = actionlog.MakeDebugPolicy()
tee = log
}
DefaultActionlog = actionlog.NewService(

View File

@@ -60,11 +60,14 @@ func Initialize(ctx context.Context, log *zap.Logger, c Config) (err error) {
DefaultLogger = log.Named("service")
{
tee := log
tee := zap.NewNop()
policy := actionlog.MakeProductionPolicy()
if c.ActionLog.Debug {
tee = zap.NewNop()
if !c.ActionLog.Enabled {
policy = actionlog.MakeDisabledPolicy()
} else if c.ActionLog.Debug {
policy = actionlog.MakeDebugPolicy()
tee = log
}
DefaultActionlog = actionlog.NewService(

View File

@@ -10,3 +10,7 @@ func MakeProductionPolicy() policyMatcher {
NewPolicyNegate(NewPolicyMatchSeverity(Debug)),
)
}
func MakeDisabledPolicy() policyMatcher {
return NewPolicyNone()
}

View File

@@ -0,0 +1,50 @@
package actionlog
import (
"testing"
)
func TestCannedPolies(t *testing.T) {
tests := []struct {
name string
actn *Action
mtch policyMatcher
want bool
}{
{
"debug policy should pass on anything",
&Action{},
MakeDebugPolicy(),
true,
},
{
"production policy should record info",
&Action{Severity: Info},
MakeProductionPolicy(),
true,
},
{
"production policy should not record debug",
&Action{Severity: Debug},
MakeProductionPolicy(),
false,
},
{
"disabled policy should not record anything",
&Action{Severity: Alert},
MakeDisabledPolicy(),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.want != tt.mtch.Match(tt.actn) {
if tt.want {
t.Errorf("expecting to match")
} else {
t.Errorf("expecting not to match ")
}
}
})
}
}

View File

@@ -34,9 +34,18 @@ type (
logPolicyMatchSeverity struct {
severities map[Severity]bool
}
logPolicyNoop struct {
v bool
}
)
// NewPolicyAny returns policy where at least one of the subpolicies should match
// NewPolicyNone ignores all action logs
func NewPolicyNone() policyMatcher {
return &logPolicyNoop{v: false}
}
// NewPolicyAny returns policy where at least one of the sub-policies should match
func NewPolicyAny(mm ...policyMatcher) policyMatcher {
return &logPolicyAny{mm: mm}
}
@@ -51,7 +60,7 @@ func (p logPolicyAny) Match(a *Action) bool {
return false
}
// NewPolicyAll returns policy where all subpolicies should match
// NewPolicyAll returns policy where all sub-policies should match
func NewPolicyAll(mm ...policyMatcher) policyMatcher {
return &logPolicyAll{mm: mm}
}
@@ -116,3 +125,8 @@ func NewPolicyMatchRequestOrigin(rr ...string) policyMatcher {
func (p logPolicyMatchRequestOrigin) Match(a *Action) bool {
return p.origin[a.RequestOrigin]
}
// Match Internal policy
func (p logPolicyNoop) Match(*Action) bool {
return p.v
}

View File

@@ -2,13 +2,15 @@ package options
type (
ActionLogOpt struct {
Debug bool `env:"ACTIONLOG_DEBUG"`
Enabled bool `env:"ACTIONLOG_ENABLED"`
Debug bool `env:"ACTIONLOG_DEBUG"`
}
)
func ActionLog() (o *ActionLogOpt) {
o = &ActionLogOpt{
Debug: false,
Enabled: true,
Debug: false,
}
fill(o, "")

View File

@@ -2,7 +2,6 @@ package service
import (
"context"
"go.uber.org/zap"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
@@ -96,11 +95,14 @@ func Initialize(ctx context.Context, log *zap.Logger, c Config) (err error) {
DefaultLogger = log.Named("service")
{
tee := log
tee := zap.NewNop()
policy := actionlog.MakeProductionPolicy()
if c.ActionLog.Debug {
tee = zap.NewNop()
if !c.ActionLog.Enabled {
policy = actionlog.MakeDisabledPolicy()
} else if c.ActionLog.Debug {
policy = actionlog.MakeDebugPolicy()
tee = log
}
DefaultActionlog = actionlog.NewService(