projectmesa / mesa

Mesa is an open-source Python library for agent-based modeling, ideal for simulating complex systems and exploring emergent behaviors.
https://mesa.readthedocs.io
Apache License 2.0
2.51k stars 877 forks source link

Add logging to MESA #2461

Open quaquel opened 1 week ago

quaquel commented 1 week ago

What's the problem this feature will solve? Mesa's code base is expanding, and more functionality is being added all the time. This makes understanding how certain errors came about more complicated. Moreover, errors in reactive code, as used in the solara visualization, can be particullary tricky to diagnose. Having built-in logging in MESA will make diagnosing errors much easier.

Describe the solution you'd like Add dedicated logging functionality to MESA. This entails:

  1. adding a logging.py that creates the root MESA logger and has some convenience functions (e.g., decorators to make logging of specific methods and functions easy and keep it out of the normal code; function for creating module loggers). See e.g., the workbench logging file for an example.
  2. Add a dedicated logger instance to each module. This is a recommended practice, and, given the hierarchical structure of logger names, isolating log messages becomes easy. So, you would have loggers like mesa.model, mesa.visualization.solara_viz, etc.
  3. Agree on what needs logging inside mesa and assess the performance implications. I favor debug-level logging on all methods and functions but would be curious to see what that does to model runtime.
  4. Agree on how to expose the logging functionality to users. For example, should we always log into a file, but where do we locate this file?
Corvince commented 1 day ago

I haven't looked into python logging yet, but there is extensive official documentation, for example at https://docs.python.org/3/howto/logging.html

I only skimmed it now, but one thing that stood out to me was the possibility to use configuration files. This might make a nice entry point for 4), exposing this to users. They might just provide their own config file to determine how and what to log (debug level, file output, etc.)

/edit removed wrong link

quaquel commented 1 day ago

I haven't used configuration files before. While reading the docs, it seems that it has been superseded by DictConfig, which allows you to use json files: See the logging cookbook.

Corvince commented 1 day ago

Oops, yes, I wanted to refer to that but provided the wrong link I thought fileConfig can load DictConfig, but you are right, its just for the old format. The dictConfig method loads a DictConfig in a dictionary format and its agnostic of where that dict originated (e.g. json or yaml)