Skip to content

Commit f0ffcba

Browse files
authored
_cli: refactor logging behavior (#372)
* _cli: refactor logging behavior Closes #371. Signed-off-by: William Woodruff <william@trailofbits.com> * CHANGELOG: record changes Signed-off-by: William Woodruff <william@trailofbits.com> * README: update `sigstore --help` Signed-off-by: William Woodruff <william@trailofbits.com> * _cli: fold `level` variable Signed-off-by: William Woodruff <william@trailofbits.com> Signed-off-by: William Woodruff <william@trailofbits.com>
1 parent e79273d commit f0ffcba

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ All versions prior to 0.9.0 are untracked.
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
* `sigstore` now supports the `-v`/`--verbose` flag as an alternative to
14+
`SIGSTORE_LOGLEVEL` for debug logging
15+
([#372](https://github.com/sigstore/sigstore-python/pull/372))
16+
17+
### Changed
18+
19+
* The default behavior of `SIGSTORE_LOGLEVEL` has changed; the logger
20+
configured is now the `sigstore.*` hierarchy logger, rather than the "root"
21+
logger ([#372](https://github.com/sigstore/sigstore-python/pull/372))
22+
1123
## [0.9.0]
1224

1325
### Added

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Top-level:
7171

7272
<!-- @begin-sigstore-help@ -->
7373
```
74-
usage: sigstore [-h] [-V] {sign,verify,get-identity-token} ...
74+
usage: sigstore [-h] [-V] [-v] {sign,verify,get-identity-token} ...
7575

7676
a tool for signing and verifying Python package distributions
7777

@@ -81,6 +81,8 @@ positional arguments:
8181
options:
8282
-h, --help show this help message and exit
8383
-V, --version show program's version number and exit
84+
-v, --verbose run with additional debug logging; supply multiple
85+
times to increase verbosity (default: 0)
8486
```
8587
<!-- @end-sigstore-help@ -->
8688

sigstore/_cli.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@
5252
policy,
5353
)
5454

55+
logging.basicConfig()
5556
logger = logging.getLogger(__name__)
56-
level = os.environ.get("SIGSTORE_LOGLEVEL", "INFO").upper()
57-
logging.basicConfig(level=level)
5857

59-
# workaround to make tuf less verbose https://github.com/theupdateframework/python-tuf/pull/2243
60-
if level == "INFO":
61-
logging.getLogger("tuf").setLevel("WARNING")
58+
# NOTE: We configure the top package logger, rather than the root logger,
59+
# to avoid overly verbose logging in third-party code by default.
60+
package_logger = logging.getLogger("sigstore")
61+
package_logger.setLevel(os.environ.get("SIGSTORE_LOGLEVEL", "INFO").upper())
6262

6363

6464
def _boolify_env(envvar: str) -> bool:
@@ -146,6 +146,13 @@ def _parser() -> argparse.ArgumentParser:
146146
parser.add_argument(
147147
"-V", "--version", action="version", version=f"%(prog)s {__version__}"
148148
)
149+
parser.add_argument(
150+
"-v",
151+
"--verbose",
152+
action="count",
153+
default=0,
154+
help="run with additional debug logging; supply multiple times to increase verbosity",
155+
)
149156
subcommands = parser.add_subparsers(required=True, dest="subcommand")
150157

151158
# `sigstore sign`
@@ -324,6 +331,12 @@ def main() -> None:
324331
parser = _parser()
325332
args = parser.parse_args()
326333

334+
# Configure logging upfront, so that we don't miss anything.
335+
if args.verbose >= 1:
336+
package_logger.setLevel("DEBUG")
337+
if args.verbose >= 2:
338+
logging.getLogger().setLevel("DEBUG")
339+
327340
logger.debug(f"parsed arguments {args}")
328341

329342
# Stuff the parser back into our namespace, so that we can use it for

0 commit comments

Comments
 (0)