Skip to content

Setting logging.basicConfig causes exception on save() #37

Description

@zEdS15B3GCwq

Hi,

Windows 11
Python 3.11.4
snirf 0.7.4

I'm learning to use the module to create snirf files from scratch. I've run into a problem, where my code also uses the logging module and sets logging.basicConfig, and it results in a TypeError exception being thrown on save(). Minimal example follows:

import logging
from snirf import Snirf

logging.basicConfig(level=logging.DEBUG)

with Snirf("test.snirf", "w") as f:
    f.nirs.appendGroup()
    f.save()

Running this code results in:

INFO:root:Loading from file test.snirf
INFO:root:IndexedGroup Nirs at / in test.snirf initalized with 0 instances of <class 'snirf.pysnirf2.NirsElement'>
INFO:root:IndexedGroup Data at //nirs1 in None initalized with 0 instances of <class 'snirf.pysnirf2.DataElement'>
INFO:root:IndexedGroup Stim at //nirs1 in None initalized with 0 instances of <class 'snirf.pysnirf2.StimElement'>
INFO:root:IndexedGroup Aux at //nirs1 in None initalized with 0 instances of <class 'snirf.pysnirf2.AuxElement'>
INFO:root:1 th <class 'snirf.pysnirf2.NirsElement'> appended to IndexedGroup Nirs at / in test.snirf
--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\dev\.pyenv\pyenv-win\versions\3.11.4\Lib\logging\__init__.py", line 1110, in emit
    msg = self.format(record)
          ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dev\.pyenv\pyenv-win\versions\3.11.4\Lib\logging\__init__.py", line 953, in format
    return fmt.format(record)
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\dev\.pyenv\pyenv-win\versions\3.11.4\Lib\logging\__init__.py", line 687, in format
    record.message = record.getMessage()
                     ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dev\.pyenv\pyenv-win\versions\3.11.4\Lib\logging\__init__.py", line 377, in getMessage
    msg = msg % self.args
          ~~~~^~~~~~~~~~~
TypeError: not all arguments converted during string formatting
Call stack:
  File "C:\Code\snirf_logging_bug.py", line 9, in <module>
    f.save()
  File "C:\Users\dev\Virtualenvs\snirfbug\Lib\site-packages\snirf\pysnirf2.py", line 6117, in save
    self._save(self._h.file)
  File "C:\Users\dev\Virtualenvs\snirfbug\Lib\site-packages\snirf\pysnirf2.py", line 5879, in _save
    self.nirs._save(*args)
  File "C:\Users\dev\Virtualenvs\snirfbug\Lib\site-packages\snirf\pysnirf2.py", line 1361, in _save
    self._order_names(h=h)  # Enforce order in the group names
  File "C:\Users\dev\Virtualenvs\snirfbug\Lib\site-packages\snirf\pysnirf2.py", line 1280, in _order_names
    self._cfg.logger.info(
Message: '//nirs1'
Arguments: ('--->', '//nirs')
INFO:root:Closing Snirf file test.snirf

No exception is thrown if the code only changes a property of the snirf root (e.g. formatVersion), but doesn't append a nirs group. Removing the logging.basicConfig line also removes the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions