diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 2d374c536..d68ec0efa 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -16,7 +16,7 @@ Please review the [Community Note](https://github.com/trufflesecurity/trufflehog ### Expected Behavior diff --git a/Makefile b/Makefile index 35f161ee5..c2dd41869 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/README.md b/README.md index 5fe0dd350..8bb67c8ff 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.go b/main.go index 31db0e3b7..7e8a92153 100644 --- a/main.go +++ b/main.go @@ -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) + } } }