Skip to content

Logging

crugas edited this page Dec 17, 2024 · 1 revision

Logging

This Wiki page is used to describe our best practices for logging scripts.

General Principles

  • Logging should be included in every script.
  • Logging should capture errors handled by the script and and output messages.

Best Practices

  • Log files should be named like the following: <name_of_script>_YYYY_MM_DD.log
  • Log files should be written to the logs directory, as set up by a user in their local repository.
  • The logs directory and log files should be excluded from any commits to the repository. Instead, log files from scripts run against production should be stored in OneDrive - Smithsonian Institution\Documents - OCIO-Digital Platforms-CAAS\Data Cleanup Projects\ArchivesSpace.
  • Log files should clearly indicate where errors were raised using the logging library's ERROR status when available.
  • Log files can include any additional info the programmer thinks is useful for debugging, general info (such as DEBUG, INFO, WARNING)

Logging Libraries

Code example (using loguru)

import loguru

logger.remove()
log_path = Path('../logs', 'delete_dometadata_{time:YYYY-MM-DD}.log')
logger.add(str(log_path), format="{time}-{level}: {message}")

Example log file

2024-11-21T16:35:26.253085-0500-INFO: Updated /repositories/32/digital_objects/222717
2024-11-21T16:35:26.316023-0500-INFO: Updated /repositories/32/digital_objects/222718
2024-11-21T16:35:26.360491-0500-ERROR: get_object() - Unable to retrieve object with provided URI: {'error': 'DigitalObject not found'}
Clone this wiki locally