feat(cli): fine-grained log level (#3703)

This commit is contained in:
Richard Gomez
2024-12-02 12:57:36 -06:00
committed by GitHub
parent 1d0bd0ae5f
commit 8f1ba8bac9
4 changed files with 26 additions and 10 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ Please review the [Community Note](https://github.com/trufflesecurity/trufflehog
<!---
Please provide a link to a GitHub Gist containing the complete debug output. Please do NOT paste the debug output in the issue; just paste a link to the Gist.
To obtain the trace output, run trufflehog with the --trace flag.
To obtain the trace output, run trufflehog with the --log-level=5 flag.
--->
### Expected Behavior
+2 -2
View File
@@ -12,7 +12,7 @@ PROTOS_IMAGE ?= trufflesecurity/protos:1.22
.PHONY: dogfood
dogfood:
CGO_ENABLED=0 go run . git file://. --json --debug
CGO_ENABLED=0 go run . git file://. --json --log-level=2
install:
CGO_ENABLED=0 go install .
@@ -49,7 +49,7 @@ run:
CGO_ENABLED=0 go run . git file://. --json
run-debug:
CGO_ENABLED=0 go run . git file://. --json --debug
CGO_ENABLED=0 go run . git file://. --json --log-level=2
protos:
docker run --rm -u "$(shell id -u)" -v "$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))":/pwd "${PROTOS_IMAGE}" bash -c "cd /pwd; /pwd/scripts/gen_proto.sh"
+2 -3
View File
@@ -410,8 +410,7 @@ Find credentials in git repositories.
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--debug Run in debug mode.
--trace Run in trace mode.
--log-level=0 Logging verbosity on a scale of 0 (info) to 5 (trace). Can be disabled with "-1".
--profile Enables profiling and sets a pprof and fgprof server on :18066.
-j, --json Output in JSON format.
--json-legacy Use the pre-v3.0 JSON format. Only works with git, gitlab, and github sources.
@@ -567,7 +566,7 @@ TruffleHog statically detects [https://canarytokens.org/](https://canarytokens.o
# Scan commits until here (usually dev branch).
head: # optional
# Extra args to be passed to the trufflehog cli.
extra_args: --debug --only-verified
extra_args: --log-level=2 --only-verified
```
If you'd like to specify specific `base` and `head` refs, you can use the `base` argument (`--since-commit` flag in TruffleHog CLI) and the `head` argument (`--branch` flag in the TruffleHog CLI). We only recommend using these arguments for very specific use cases, where the default behavior does not work.
+21 -4
View File
@@ -40,10 +40,12 @@ import (
)
var (
cli = kingpin.New("TruffleHog", "TruffleHog is a tool for finding credentials.")
cmd string
debug = cli.Flag("debug", "Run in debug mode.").Bool()
trace = cli.Flag("trace", "Run in trace mode.").Bool()
cli = kingpin.New("TruffleHog", "TruffleHog is a tool for finding credentials.")
cmd string
// https://github.com/trufflesecurity/trufflehog/blob/main/CONTRIBUTING.md#logging-in-trufflehog
logLevel = cli.Flag("log-level", `Logging verbosity on a scale of 0 (info) to 5 (trace). Can be disabled with "-1".`).Default("0").Int()
debug = cli.Flag("debug", "Run in debug mode.").Hidden().Bool()
trace = cli.Flag("trace", "Run in trace mode.").Hidden().Bool()
profile = cli.Flag("profile", "Enables profiling and sets a pprof and fgprof server on :18066.").Bool()
localDev = cli.Flag("local-dev", "Hidden feature to disable overseer for local dev.").Hidden().Bool()
jsonOut = cli.Flag("json", "Output in JSON format.").Short('j').Bool()
@@ -275,11 +277,26 @@ func init() {
cmd = kingpin.MustParse(cli.Parse(os.Args[1:]))
// Configure logging.
switch {
case *trace:
log.SetLevel(5)
case *debug:
log.SetLevel(2)
default:
l := int8(*logLevel)
if l < -1 || l > 5 {
fmt.Fprintf(os.Stderr, "invalid log level: %d\n", *logLevel)
os.Exit(1)
}
if l == -1 {
// Zap uses "5" as the value for fatal.
// We need to pass in "-5" because `SetLevel` passes the negation.
log.SetLevel(-5)
} else {
log.SetLevel(l)
}
}
}