Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging #1936

Merged
merged 32 commits into from
Jun 19, 2024
Merged

Logging #1936

merged 32 commits into from
Jun 19, 2024

Conversation

JimMadge
Copy link
Member

@JimMadge JimMadge commented Jun 14, 2024

✅ Checklist

  • You have given your pull request a meaningful title (e.g. Enable foobar integration rather than 515 foobar).
  • You are targeting the appropriate branch. If you're not certain which one this is, it should be develop.
  • Your branch is up-to-date with the target branch (it probably was when you started, but it may have changed since then).

🚦 Depends on

⤴️ Summary

  • Logging changes
    • Simplify handler/formatter construction
    • Remove log level, file, line number from console handler
  • Move unrelated methods from logging class
  • Remove top level try/except
  • More Pythonic use of logging module (makes configuration neater, particularly in unit tests)

The approach here is

  • ConsoleHandler: Logging to console, information for the user, INFO and above
  • PlainFileHandler: Logging to a file with rich markup removed, information for auditing and debugging, all levels

with logging levels

  • rich.print: When output is only of interest to the user. Used for commands which send their output to stdout.
  • logger.info: Used for notifications of actions taken and completed successfully. stdout which is also useful to log.
  • logger.warning: Used for unexpected events or potential problems that won't cause the program to stop. User should take notice even if the program continues.
  • logger.error: Used for unexpected events or problems that may cause the program to stop.
  • logger.fatal: Used for problems that will stop the program, immediately before an exit call.

🌂 Related issues

Closes #1891
Closes #1657

🔬 Tests

@JimMadge JimMadge requested a review from a team as a code owner June 14, 2024 08:03
Copy link

github-actions bot commented Jun 14, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  data_safe_haven
  __init__.py
  directories.py
  data_safe_haven/administration/users
  entra_users.py 29
  user_handler.py 29, 129, 155, 161
  data_safe_haven/commands
  cli.py 57, 60, 100
  config.py
  context.py
  pulumi.py
  sre.py 32
  users.py 128, 230
  data_safe_haven/config
  config.py
  data_safe_haven/context
  context_settings.py
  data_safe_haven/exceptions
  __init__.py
  data_safe_haven/external/api
  azure_api.py 753
  azure_cli.py 75
  graph_api.py 87, 1086, 1112-1119
  data_safe_haven/external/interface
  azure_postgresql_database.py 54, 151, 159
  data_safe_haven/infrastructure
  project_manager.py 176, 251-254, 262, 272, 306
  data_safe_haven/infrastructure/components/dynamic
  entra_application.py 43, 69, 125
  ssl_certificate.py 51, 81, 156
  data_safe_haven/logging
  __init__.py
  logger.py
  data_safe_haven/provisioning
  sre_provisioning_manager.py 32, 117
  data_safe_haven/utility
  console.py 17-25
  prompts.py
Project Total  

The report is truncated to 25 files out of 31. To see the full report, please visit the workflow summary page.

This report was generated by python-coverage-comment-action

@jemrobinson
Copy link
Member

Something to consider (no problem if you decide not to) - should we log all runs to $appdirs / $date-$user-something.log and potentially also upload these to Azure for future reference?

JimMadge added 14 commits June 14, 2024 15:13
The singleton structure was causing problems in unit tests.
This commit uses a more Pythonic style of logging (I wonder if the
reason Python logging works as it does it because they tried something
like we did an also found it introduced unexpected problems).
@JimMadge JimMadge changed the title WIP: Logging Logging Jun 18, 2024
Copy link
Member

@jemrobinson jemrobinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, LGTM. Can you post some examples of what the output looks like before and after for places where it's changed?

data_safe_haven/commands/cli.py Outdated Show resolved Hide resolved
data_safe_haven/exceptions/__init__.py Outdated Show resolved Hide resolved
data_safe_haven/logging/logger.py Outdated Show resolved Hide resolved
data_safe_haven/utility/console.py Show resolved Hide resolved
data_safe_haven/utility/prompts.py Show resolved Hide resolved
data_safe_haven/commands/cli.py Show resolved Hide resolved
Co-authored-by: James Robinson <james.em.robinson@gmail.com>
@JimMadge JimMadge mentioned this pull request Jun 19, 2024
@JimMadge JimMadge merged commit a5bbe3a into develop Jun 19, 2024
10 of 11 checks passed
@JimMadge JimMadge deleted the logging branch June 19, 2024 08:32
@JimMadge
Copy link
Member Author

Merged through docs link check failing as that wasn't modified here. Does look like a genuine failure though.

@JimMadge
Copy link
Member Author

JimMadge commented Jun 20, 2024

Screenshots,

  • New with --verbose --show-level
  • New (default)
  • Old
Screenshot 2024-06-20 at 11 15 26

@JimMadge
Copy link
Member Author

And the log file,

2024-06-20 11:13:47,048 - DEBUG - Reading project settings from '/Users/jmadge/Library/Application Support/data_safe_haven/contexts.yaml'.
2024-06-20 11:13:47,067 - INFO - Switched context to 'elements'.
2024-06-20 11:13:47,073 - DEBUG - Saved context settings to '/Users/jmadge/Library/Application Support/data_safe_haven/contexts.yaml'.
2024-06-20 11:14:06,576 - DEBUG - Reading project settings from '/Users/jmadge/Library/Application Support/data_safe_haven/contexts.yaml'.
2024-06-20 11:14:06,586 - INFO - Switched context to 'elements'.
2024-06-20 11:14:06,595 - DEBUG - Saved context settings to '/Users/jmadge/Library/Application Support/data_safe_haven/contexts.yaml'.

@jemrobinson
Copy link
Member

Looks great. I like the idea of keeping timestamps in the log file but not in the console output (by default). Do you think there's any benefit in adding an option to include timestamps in the console output?

@jemrobinson jemrobinson mentioned this pull request Jun 20, 2024
5 tasks
@JimMadge
Copy link
Member Author

Looks great. I like the idea of keeping timestamps in the log file but not in the console output (by default). Do you think there's any benefit in adding an option to include timestamps in the console output?

That did occur to me when I posted the pictures. Could definitely do that too, should be an easy addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stdout, stderr and log usage Restructure entrypoint and exception/log interaction
2 participants