@@ -60,24 +60,47 @@ You may notice CmdStanPy can produce a lot of output when it is running:
6060
6161 fit = model.sample(data = data_file, show_progress = False )
6262
63- This output is managed through the built-in :mod: `logging ` module. For example, it can be disabled entirely:
63+ This output is managed through the built-in :mod: `logging ` module. For
64+ convenience, CmdStanPy provides the module-level :meth: `cmdstanpy.enable_logging() `
65+ and :meth: `cmdstanpy.disable_logging() ` functions to simplify logging management.
66+
67+ For example, it can be disabled entirely:
6468
6569.. ipython :: python
6670
67- import logging
68- cmdstanpy_logger = logging.getLogger(" cmdstanpy" )
69- cmdstanpy_logger.disabled = True
71+ import cmdstanpy
72+ cmdstanpy.disable_logging()
7073 # look, no output!
7174 fit = model.sample(data = data_file, show_progress = False )
7275
73- Or one can remove the logging handler that CmdStanPy installs by default and install their own for more
74- fine-grained control. For example, the following code sends all logs (including the ``DEBUG `` logs, which are hidden by default),
75- to a file.
76+ We can re-enable this by calling ``enable_logging() ``:
77+
78+ .. ipython :: python
79+
80+ cmdstanpy.enable_logging()
81+ fit = model.sample(data = data_file, show_progress = False )
82+
83+
84+ These functions also work as context managers for more local control:
85+
86+ .. ipython :: python
87+
88+ with cmdstanpy.disable_logging():
89+ fit = model.sample(data = data_file, show_progress = False )
90+
91+
92+ For more fine-grained control, one can interact with the underlying ``logging ``
93+ library directly. For example, the following code installs a custom handler
94+ that sends all logs (including the ``DEBUG `` logs, which are hidden by
95+ default), to a file.
7696
7797DEBUG logging is useful primarily to developers or when trying to hunt down an issue.
7898
7999.. ipython :: python
80100
101+ import logging
102+ cmdstanpy_logger = logging.getLogger(" cmdstanpy" )
103+
81104 cmdstanpy_logger.disabled = False
82105 # remove all existing handlers
83106 cmdstanpy_logger.handlers = []
@@ -93,6 +116,7 @@ DEBUG logging is useful primarily to developers or when trying to hunt down an i
93116 )
94117 cmdstanpy_logger.addHandler(handler)
95118
119+
96120 Now, if we run the model and check the contents of the file, we will see all the possible logging.
97121
98122.. ipython :: python
0 commit comments