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

Add logging subsystem #285

Merged
merged 12 commits into from
Feb 28, 2023
Merged

Conversation

collijk
Copy link
Member

@collijk collijk commented Feb 27, 2023

Add logging subsystem

Description

Category: feature
Issues: #282 and #283

The primary goal of this PR is to add a logging subsystem into vivarium and to make it have as small an impact as
possible on user facing code.

User-facing changes

  • add_logging_sink, configure_logging_to_terminal, and configure_logging_to_file have been moved from vivarium.interface.utilities to vivarium.framework.logging.utilities. These functions are not used in vivarium_cluster_tools but may be alive in user code (Abie pointed out he uses it in Logging in interactive settings is verbose and not configurable #283 to suppress noisy logging, but part of the goal here is to give a more sensible way to do that).
  • Output log format has changed. Our old format is {time} | {level} | {module}:{function}:{line} - {message}. There are two new formats. The default (long) format is {time} | {level} | {simulation_name}-{component_name}:{line} - {message} if logging from a component that has requested a named logger and {time} | {level} | {simulation_name}-{module}:{line} - {message} if logging from a component that has requested an anonymous logger or if logging from the simulation context itself. The idea here is that the component is going to be a more intuitive (and fine-grained) logging detail than the module for researchers and straightforward to translate to modules for engineers. If that turns out not to be true, user space code can just request anonymous loggers. Additionally, there is a short format log that does {time} | {module}:{line} - {message} that I think will be appropriate for things like running sims in parallel. Only the long format is currently exposed to users.
  • The default logging level for both interactive and command line settings has been changed from ERROR at the command line and DEBUG in an interactive setting to INFO in both settings (who thought that was a good idea originally 🙃 ).
  • The logging level for the CLI is configurable from command line flags (-v logs at DEBUG level, -q logs at WARNING level)
  • The logging level for the interactive sim is configurable from a logging_verbosity argument in the initializer (2 logs at DEBUG level, 1 logs at INFO level [default], 0 logs at WARNING level). This argument is ignored if an external tool (like the command line interface) has configured logging. External tools may want to log broader information to the same sinks and must have the ability to preempt.
  • The following message levels have been changed:
    • Artifact manager messages are logged at INFO (formerly DEBUG).
    • The context message about unused configuration keys is now logged at WARNING (formerly DEBUG)
    • Results and performance metrics are now logged at INFO (formerly DEBUG)
  • API docs have been setup in sphinx
  • A concept stub for logging has been added to the concept section of the docs.
  • User components can now request a logger from a new system of the builder builder.logging.get_logger and can, if desired, pass in the component name from which they want to log.

Testing

CI and manual testing.

@collijk collijk changed the base branch from collijk/feature/add-unique-simulation-name to develop February 27, 2023 22:21
@@ -220,13 +228,13 @@ def report(self, print_results=True):
self._population.get_population(True).index
)
if print_results:
logger.debug("\n" + pformat(metrics))
self._logger.info("\n" + pformat(metrics))
Copy link
Contributor

Choose a reason for hiding this comment

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

This potentially spits out a very large dictionary. Since these outputs are being written to output.hdf anyway, are you sure this needs to be logged at INFO? I think DEBUG might still make more sense.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is a good point, but one to address in a different PR focused on the results system.

performance_metrics = self.get_performance_metrics()
performance_metrics = performance_metrics.to_string(
index=False,
float_format=lambda x: f"{x:.2f}",
)
logger.debug("\n" + performance_metrics)
self._logger.info("\n" + performance_metrics)
Copy link
Contributor

Choose a reason for hiding this comment

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

The same argument could be made here, but since performance metrics are much smaller, I think logging at INFO still could make sense.

log_file = output_directory / "simulation.log"
_add_logging_sink(
log_file,
verbosity=2,
Copy link
Contributor

Choose a reason for hiding this comment

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

I wish logging levels were Enums rather than ints. This would be more legible if it read verbosity=DEBUG, but there's nothing we can do about that I guess.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will address in a separate PR

fmt += self.simulation_and_component + " - "
else:
fmt += self.simulation + " - "
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this else supposed to be for this if or the one above?

Copy link
Member Author

Choose a reason for hiding this comment

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

For this if.

@@ -2,6 +2,8 @@

import pytest
import tables
from _pytest.logging import LogCaptureFixture
Copy link
Contributor

Choose a reason for hiding this comment

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

What is _pytest?

Copy link
Member Author

Choose a reason for hiding this comment

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

Cargo culting. This is a pattern from the loguru docs for making pytest work correctly with loguru.

collijk and others added 4 commits February 27, 2023 16:20
Co-authored-by: Rajan Mudambi <11376379+rmudambi@users.noreply.github.com>
Write a more extensive comment about checking for terminal logging handlers.
@collijk collijk merged commit 7e9788f into develop Feb 28, 2023
@collijk collijk deleted the collijk/feature/add-logging-subsystem branch February 28, 2023 01:05
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.

None yet

3 participants