Skip to content

Commit

Permalink
Log diagnostic steps to file in output
Browse files Browse the repository at this point in the history
Fixes #114
  • Loading branch information
jfrost-mo committed Dec 19, 2023
1 parent 4c6f534 commit 38aa358
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/CSET/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 11 additions & 2 deletions src/CSET/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 38aa358

Please sign in to comment.