|
18 | 18 | import argparse |
19 | 19 | import logging |
20 | 20 | import os |
| 21 | +from sys import stdout |
21 | 22 | from typing import Any, Iterable |
22 | 23 |
|
23 | 24 | import oqs |
24 | 25 | import oqs.serialize |
25 | 26 |
|
26 | 27 | logger = logging.getLogger(__name__) |
27 | | - |
28 | | - |
29 | | -# Configure logging from CLI arg or environment variables when invoked as script |
30 | | -# Supported env vars (in precedence order): KEYGEN_LOG_LEVEL, LOG_LEVEL, PYTHONLOGLEVEL |
31 | | -_SUPPORTED_LOG_LEVEL_ENVS = ("KEYGEN_LOG_LEVEL", "LOG_LEVEL", "PYTHONLOGLEVEL") |
32 | | - |
33 | | - |
34 | | -def _parse_log_level(name: str | None) -> int: |
35 | | - if not name: |
36 | | - return logging.INFO |
37 | | - try: |
38 | | - return getattr(logging, name.upper()) |
39 | | - except AttributeError: |
40 | | - return logging.INFO |
41 | | - |
42 | | - |
43 | | -def _configure_logging(cli_level: str | None = None) -> None: |
44 | | - # Determine level from CLI or environment |
45 | | - level_name = cli_level |
46 | | - if level_name is None: |
47 | | - for env in _SUPPORTED_LOG_LEVEL_ENVS: |
48 | | - val = os.environ.get(env) |
49 | | - if val: |
50 | | - level_name = val |
51 | | - break |
52 | | - level = _parse_log_level(level_name) |
53 | | - |
54 | | - # Ensure basic configuration even if another library configured logging |
55 | | - logging.basicConfig( |
56 | | - level=level, |
57 | | - format="%(asctime)s %(levelname)s %(name)s: %(message)s", |
58 | | - datefmt="%H:%M:%S", |
59 | | - force=True, # Python >=3.8; overwrite existing handlers to ensure our settings apply |
60 | | - ) |
61 | | - logger.debug("Logging configured at %s", logging.getLevelName(level)) |
| 28 | +logger.setLevel(logging.INFO) |
| 29 | +logger.addHandler(logging.StreamHandler(stdout)) |
62 | 30 |
|
63 | 31 |
|
64 | 32 | def _mech_to_filename(name: str) -> str: |
@@ -216,22 +184,8 @@ def main(argv: Iterable[str] | None = None) -> int: |
216 | 184 | "Defaults to $KEY_DIR if set, otherwise data/xmss_xmssmt_keys." |
217 | 185 | ), |
218 | 186 | ) |
219 | | - # Optional logging level control |
220 | | - parser.add_argument( |
221 | | - "-l", |
222 | | - "--log-level", |
223 | | - dest="log_level", |
224 | | - choices=["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"], |
225 | | - help=( |
226 | | - "Logging level. If omitted, will use KEYGEN_LOG_LEVEL/LOG_LEVEL/PYTHONLOGLEVEL env vars," |
227 | | - " defaulting to INFO." |
228 | | - ), |
229 | | - ) |
230 | 187 | args = parser.parse_args(list(argv) if argv is not None else None) |
231 | 188 |
|
232 | | - # Configure logging before doing work |
233 | | - _configure_logging(args.log_level) |
234 | | - |
235 | 189 | out_dir = _resolve_out_dir(args.key_dir) |
236 | 190 | stats = generate_keys(out_dir) |
237 | 191 |
|
|
0 commit comments