diff --git a/src/CSET/__init__.py b/src/CSET/__init__.py index 0df82db82..dda73b5d8 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 5a5637410..7f6e11d9a 100644 --- a/src/CSET/operators/__init__.py +++ b/src/CSET/operators/__init__.py @@ -154,9 +154,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)