diff --git a/src/CSET/__init__.py b/src/CSET/__init__.py index c9d386f62..fa8ca6176 100644 --- a/src/CSET/__init__.py +++ b/src/CSET/__init__.py @@ -88,15 +88,23 @@ def main(): default=None, ) parser_cookbook.set_defaults(func=_cookbook_command) - args = parser.parse_args() - # Logging verbosity + # Setup logging. if args.verbose >= 2: - logging.basicConfig(level=logging.DEBUG) - elif args.verbose >= 1: - logging.basicConfig(level=logging.INFO) - + loglevel = logging.DEBUG + elif args.verbose == 1: + loglevel = logging.INFO + else: + loglevel = logging.WARNING + logger = logging.getLogger() + logger.setLevel(min(loglevel, logging.INFO)) + stderr_log = logging.StreamHandler() + stderr_log.addFilter(lambda record: record.levelno >= loglevel) + stderr_log.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s")) + logger.addHandler(stderr_log) + + # Execute the specified subcommand. if args.subparser: args.func(args) else: diff --git a/src/CSET/operators/__init__.py b/src/CSET/operators/__init__.py index 7a905d42c..81eb37509 100644 --- a/src/CSET/operators/__init__.py +++ b/src/CSET/operators/__init__.py @@ -155,9 +155,18 @@ def step_parser(step: dict, step_input: any) -> str: original_working_directory = Path.cwd() os.chdir(output_directory) - # Create metadata file used by some steps. - _write_metadata(recipe) try: + logger = logging.getLogger() + diagnostic_log = logging.FileHandler( + filename="CSET.log", mode="w", encoding="UTF-8" + ) + diagnostic_log.addFilter(lambda record: record.levelno >= logging.INFO) + diagnostic_log.setFormatter( + logging.Formatter("%(asctime)s %(levelname)s %(message)s") + ) + logger.addHandler(diagnostic_log) + # Create metadata file used by some steps. + _write_metadata(recipe) # Execute the recipe. for step in recipe["steps"]: step_input = step_parser(step, step_input)