Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusting Field/Filtering Function Naming to be More Concise and Less With-y #78

Merged
merged 6 commits into from Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 111 additions & 0 deletions .changelog/78.txt
@@ -0,0 +1,111 @@
```release-note:breaking-change
tflog: renamed `With()` to `SetField()`
```

```release-note:breaking-change
tflog: renamed `WithOmitLogWithFieldKeys()` to `OmitLogWithFieldKeys()`
```

```release-note:breaking-change
tflog: renamed `WithOmitLogWithMessageRegex()` to `OmitLogWithMessageRegexes()`
```

```release-note:breaking-change
tflog: renamed `WithOmitLogMatchingString()` to `OmitLogWithMessageStrings()`
```

```release-note:breaking-change
tflog: renamed `WithMaskFieldValueWithFieldKeys()` to `MaskFieldValuesWithFieldKeys()`
```

```release-note:breaking-change
tflog: renamed `WithMaskMessageRegex()` to `MaskMessageRegexes()`
```

```release-note:breaking-change
tflog: renamed `WithMaskLogMatchingString()` to `MaskMessageStrings()`
```

```release-note:breaking-change
tflog: renamed `SubsystemWith()` to `SubsystemSetField()`
```

```release-note:breaking-change
tflog: renamed `SubsystemWithOmitLogWithFieldKeys()` to `SubsystemOmitLogWithFieldKeys()`
```

```release-note:breaking-change
tflog: renamed `SubsystemWithOmitLogWithMessageRegex()` to `SubsystemOmitLogWithMessageRegexes()`
```

```release-note:breaking-change
tflog: renamed `SubsystemWithOmitLogMatchingString()` to `SubsystemOmitLogWithMessageStrings()`
```

```release-note:breaking-change
tflog: renamed `SubsystemWithMaskFieldValueWithFieldKeys()` to `SubsystemMaskFieldValuesWithFieldKeys()`
```

```release-note:breaking-change
tflog: renamed `SubsystemWithMaskMessageRegex()` to `SubsystemMaskMessageRegexes()`
```

```release-note:breaking-change
tflog: renamed `SubsystemWithMaskLogMatchingString()` to `SubsystemMaskMessageStrings()`
```

```release-note:breaking-change
tfsdklog: renamed `With()` to `SetField()`
```

```release-note:breaking-change
tfsdklog: renamed `WithOmitLogWithFieldKeys()` to `OmitLogWithFieldKeys()`
```

```release-note:breaking-change
tfsdklog: renamed `WithOmitLogWithMessageRegex()` to `OmitLogWithMessageRegexes()`
```

```release-note:breaking-change
tfsdklog: renamed `WithOmitLogMatchingString()` to `OmitLogWithMessageStrings()`
```

```release-note:breaking-change
tfsdklog: renamed `WithMaskFieldValueWithFieldKeys()` to `MaskFieldValuesWithFieldKeys()`
```

```release-note:breaking-change
tfsdklog: renamed `WithMaskMessageRegex()` to `MaskMessageRegexes()`
```

```release-note:breaking-change
tfsdklog: renamed `WithMaskLogMatchingString()` to `MaskMessageStrings()`
```

```release-note:breaking-change
tfsdklog: renamed `SubsystemWith()` to `SubsystemSetField()`
```

```release-note:breaking-change
tfsdklog: renamed `SubsystemWithOmitLogWithFieldKeys()` to `SubsystemOmitLogWithFieldKeys()`
```

```release-note:breaking-change
tfsdklog: renamed `SubsystemWithOmitLogWithMessageRegex()` to `SubsystemOmitLogWithMessageRegexes()`
```

```release-note:breaking-change
tfsdklog: renamed `SubsystemWithOmitLogMatchingString()` to `SubsystemOmitLogWithMessageStrings()`
```

```release-note:breaking-change
tfsdklog: renamed `SubsystemWithMaskFieldValueWithFieldKeys()` to `SubsystemMaskFieldValuesWithFieldKeys()`
```

```release-note:breaking-change
tfsdklog: renamed `SubsystemWithMaskMessageRegex()` to `SubsystemMaskMessageRegexes()`
```

```release-note:breaking-change
tfsdklog: renamed `SubsystemWithMaskLogMatchingString()` to `SubsystemMaskMessageStrings()`
```
20 changes: 10 additions & 10 deletions internal/logging/filtering.go
Expand Up @@ -12,12 +12,12 @@ const logMaskingReplacementString = "***"
// ShouldOmit takes a log's *string message and slices of arguments,
// and determines, based on the LoggerOpts configuration, if the
// log should be omitted (i.e. prevent it to be printed on the final writer).
func (lo LoggerOpts) ShouldOmit(msg *string, argSlices ...[]interface{}) bool {
func (lo LoggerOpts) ShouldOmit(msg *string, hclogArgSlices ...[]interface{}) bool {
// Omit log if any of the configured keys is found
// either in the logger implied arguments,
// or in the additional arguments
if len(lo.OmitLogWithFieldKeys) > 0 {
for _, args := range argSlices {
for _, args := range hclogArgSlices {
argKeys := hclogutils.ArgsToKeys(args)
if argKeysContain(argKeys, lo.OmitLogWithFieldKeys) {
return true
Expand All @@ -26,8 +26,8 @@ func (lo LoggerOpts) ShouldOmit(msg *string, argSlices ...[]interface{}) bool {
}

// Omit log if any of the configured regexp matches the log message
if len(lo.OmitLogWithMessageRegex) > 0 {
for _, r := range lo.OmitLogWithMessageRegex {
if len(lo.OmitLogWithMessageRegexes) > 0 {
for _, r := range lo.OmitLogWithMessageRegexes {
if r.MatchString(*msg) {
return true
}
Expand All @@ -51,10 +51,10 @@ func (lo LoggerOpts) ShouldOmit(msg *string, argSlices ...[]interface{}) bool {
// based on the LoggerOpts configuration.
//
// Note that the given input is changed-in-place by this method.
func (lo LoggerOpts) ApplyMask(msg *string, argSlices ...[]interface{}) {
if len(lo.MaskFieldValueWithFieldKeys) > 0 {
for _, k := range lo.MaskFieldValueWithFieldKeys {
for _, args := range argSlices {
func (lo LoggerOpts) ApplyMask(msg *string, hclogArgSlices ...[]interface{}) {
if len(lo.MaskFieldValuesWithFieldKeys) > 0 {
for _, k := range lo.MaskFieldValuesWithFieldKeys {
for _, args := range hclogArgSlices {
// Here we loop `i` with steps of 2, starting from position 1 (i.e. `1, 3, 5, 7...`).
// We then look up the key for each argument, by looking at `i-1`.
// This ensures that in case of malformed arg slices that don't have
Expand All @@ -77,8 +77,8 @@ func (lo LoggerOpts) ApplyMask(msg *string, argSlices ...[]interface{}) {

// Replace any part of the log message matching any of the configured regexp,
// with a masking replacement string
if len(lo.MaskMessageRegex) > 0 {
for _, r := range lo.MaskMessageRegex {
if len(lo.MaskMessageRegexes) > 0 {
for _, r := range lo.MaskMessageRegexes {
*msg = r.ReplaceAllString(*msg, logMaskingReplacementString)
}
}
Expand Down
58 changes: 29 additions & 29 deletions internal/logging/filtering_test.go
Expand Up @@ -15,13 +15,13 @@ func TestShouldOmit(t *testing.T) {
testCases := map[string]struct {
lOpts LoggerOpts
msg string
argSlices [][]interface{}
hclogArgSlices [][]interface{}
expectedToOmit bool
}{
"empty-opts": {
lOpts: LoggerOpts{},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -34,7 +34,7 @@ func TestShouldOmit(t *testing.T) {
OmitLogWithFieldKeys: []string{"k2"},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -47,7 +47,7 @@ func TestShouldOmit(t *testing.T) {
OmitLogWithFieldKeys: []string{"K2"},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -60,7 +60,7 @@ func TestShouldOmit(t *testing.T) {
OmitLogWithFieldKeys: []string{"k3"},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -70,10 +70,10 @@ func TestShouldOmit(t *testing.T) {
},
"omit-log-matching-regexp-case-insensitive": {
lOpts: LoggerOpts{
OmitLogWithMessageRegex: []*regexp.Regexp{regexp.MustCompile("(?i)(foo|bar)")},
OmitLogWithMessageRegexes: []*regexp.Regexp{regexp.MustCompile("(?i)(foo|bar)")},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -83,10 +83,10 @@ func TestShouldOmit(t *testing.T) {
},
"do-not-omit-log-matching-regexp-case-sensitive": {
lOpts: LoggerOpts{
OmitLogWithMessageRegex: []*regexp.Regexp{regexp.MustCompile("(foo|bar)")},
OmitLogWithMessageRegexes: []*regexp.Regexp{regexp.MustCompile("(foo|bar)")},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -102,7 +102,7 @@ func TestShouldOmit(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.lOpts.ShouldOmit(&testCase.msg, testCase.argSlices...)
got := testCase.lOpts.ShouldOmit(&testCase.msg, testCase.hclogArgSlices...)

if got != testCase.expectedToOmit {
t.Errorf("expected ShouldOmit to return %t, got %t", testCase.expectedToOmit, got)
Expand All @@ -117,14 +117,14 @@ func TestApplyMask(t *testing.T) {
testCases := map[string]struct {
lOpts LoggerOpts
msg string
argSlices [][]interface{}
hclogArgSlices [][]interface{}
expectedMsg string
expectedArgSlices [][]interface{}
}{
"empty-opts": {
lOpts: LoggerOpts{},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -140,10 +140,10 @@ func TestApplyMask(t *testing.T) {
},
"mask-log-by-key": {
lOpts: LoggerOpts{
MaskFieldValueWithFieldKeys: []string{"k2"},
MaskFieldValuesWithFieldKeys: []string{"k2"},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -159,10 +159,10 @@ func TestApplyMask(t *testing.T) {
},
"no-mask-log-by-key-if-case-mismatches": {
lOpts: LoggerOpts{
MaskFieldValueWithFieldKeys: []string{"K2"},
MaskFieldValuesWithFieldKeys: []string{"K2"},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -178,10 +178,10 @@ func TestApplyMask(t *testing.T) {
},
"mask-log-by-non-even-args-cannot-mask-missing-value": {
lOpts: LoggerOpts{
MaskFieldValueWithFieldKeys: []string{"k2", "k4"},
MaskFieldValuesWithFieldKeys: []string{"k2", "k4"},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -205,10 +205,10 @@ func TestApplyMask(t *testing.T) {
},
"mask-log-by-non-even-args": {
lOpts: LoggerOpts{
MaskFieldValueWithFieldKeys: []string{"k2"},
MaskFieldValuesWithFieldKeys: []string{"k2"},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -228,10 +228,10 @@ func TestApplyMask(t *testing.T) {
},
"mask-log-matching-regexp-case-insensitive": {
lOpts: LoggerOpts{
MaskMessageRegex: []*regexp.Regexp{regexp.MustCompile("(?i)(foo|bar)")},
MaskMessageRegexes: []*regexp.Regexp{regexp.MustCompile("(?i)(foo|bar)")},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -247,10 +247,10 @@ func TestApplyMask(t *testing.T) {
},
"mask-log-matching-regexp-case-sensitive": {
lOpts: LoggerOpts{
MaskMessageRegex: []*regexp.Regexp{regexp.MustCompile("incorrectly configured BAZ")},
MaskMessageRegexes: []*regexp.Regexp{regexp.MustCompile("incorrectly configured BAZ")},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -266,11 +266,11 @@ func TestApplyMask(t *testing.T) {
},
"mask-log-by-key-and-matching-regexp": {
lOpts: LoggerOpts{
MaskMessageRegex: []*regexp.Regexp{regexp.MustCompile("incorrectly configured BAZ")},
MaskFieldValueWithFieldKeys: []string{"k1", "k2"},
MaskMessageRegexes: []*regexp.Regexp{regexp.MustCompile("incorrectly configured BAZ")},
MaskFieldValuesWithFieldKeys: []string{"k1", "k2"},
},
msg: testLogMsg,
argSlices: [][]interface{}{
hclogArgSlices: [][]interface{}{
{
"k1", "v1",
"k2", "v2",
Expand All @@ -292,13 +292,13 @@ func TestApplyMask(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

testCase.lOpts.ApplyMask(&testCase.msg, testCase.argSlices...)
testCase.lOpts.ApplyMask(&testCase.msg, testCase.hclogArgSlices...)

if diff := cmp.Diff(testCase.msg, testCase.expectedMsg); diff != "" {
t.Errorf("unexpected difference detected in log message: %s", diff)
}

if diff := cmp.Diff(testCase.argSlices, testCase.expectedArgSlices); diff != "" {
if diff := cmp.Diff(testCase.hclogArgSlices, testCase.expectedArgSlices); diff != "" {
t.Errorf("unexpected difference detected in log arguments: %s", diff)
}
})
Expand Down