-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add log filtering functionality #71
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1196825
New utility function in `internal/hclogutils`: `ArgsToKeys`
49e7709
Implementing the business logic of filtering in `internal/logging` pa…
15e3dad
Defining utilities to create `loggerKey`s for storing log filtering c…
48a8bdb
Exposing log filtering on all offerings of this library
a1e51ea
Missed an import
d23e161
Adding CHANGELOG entries for log filtering and preparing for v0.5.0 r…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-log/internal/hclogutils" | ||
) | ||
|
||
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 { | ||
// 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 { | ||
argKeys := hclogutils.ArgsToKeys(args) | ||
if argKeysContain(argKeys, lo.OmitLogWithFieldKeys) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
// Omit log if any of the configured regexp matches the log message | ||
if len(lo.OmitLogWithMessageRegex) > 0 { | ||
for _, r := range lo.OmitLogWithMessageRegex { | ||
if r.MatchString(*msg) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
// Omit log if any of the configured strings is contained in the log message | ||
if len(lo.OmitLogWithMessageStrings) > 0 { | ||
for _, s := range lo.OmitLogWithMessageStrings { | ||
if strings.Contains(*msg, s) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// ApplyMask takes a log's *string message and slices of arguments, | ||
// and applies masking of keys' values and/or message, | ||
// 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 { | ||
// 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 | ||
// an even number of elements, we simply skip the last k/v pair. | ||
for i := 1; i < len(args); i += 2 { | ||
detro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch argK := args[i-1].(type) { | ||
case string: | ||
if k == argK { | ||
args[i] = logMaskingReplacementString | ||
} | ||
default: | ||
if k == fmt.Sprintf("%s", argK) { | ||
args[i] = logMaskingReplacementString | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 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 { | ||
*msg = r.ReplaceAllString(*msg, logMaskingReplacementString) | ||
} | ||
} | ||
|
||
// Replace any part of the log message equal to any of the configured strings, | ||
// with a masking replacement string | ||
if len(lo.MaskMessageStrings) > 0 { | ||
for _, s := range lo.MaskMessageStrings { | ||
*msg = strings.ReplaceAll(*msg, s, logMaskingReplacementString) | ||
} | ||
} | ||
} | ||
|
||
func argKeysContain(haystack []string, needles []string) bool { | ||
for _, h := range haystack { | ||
for _, n := range needles { | ||
if n == h { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although it looks like this should never happen, if
len(args)
is an odd/uneven number, then a key with no corresponding value could be appended tokeys
.Perhaps
for i := 0; i + 1 < len(args); i += 2 {
could be used to avoid such a scenario?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added tests for this as well