Skip to content

Commit

Permalink
Do not use root logger
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrnr committed Dec 13, 2024
1 parent c8baf91 commit 56861b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
18 changes: 10 additions & 8 deletions unisens/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from xml.etree.ElementTree import Element
from copy import deepcopy

logger = logging.getLogger("unisens")


def get_module(name):
try:
Expand Down Expand Up @@ -215,7 +217,7 @@ def _set_channels(self, ch_names: List[str], n_data: int):
# this means new channel names are indicated and will overwrite.
assert len(ch_names) == n_data, f'len {ch_names}!={n_data}'
if hasattr(self, 'channel'):
logging.warning('Channels present will be overwritten')
logger.warning('Channels present will be overwritten')
self.remove_entry('channel')
for name in ch_names:
channel = MiscEntry('channel', key='name', value=name)
Expand All @@ -226,7 +228,7 @@ def _set_channels(self, ch_names: List[str], n_data: int):
'Please provide a list of channel names with set_data().',
category=DeprecationWarning, stacklevel=2)
# we create new generic names for the channels
logging.info('No channel names indicated, will use generic names')
logger.info('No channel names indicated, will use generic names')
for i in range(n_data):
channel = MiscEntry('channel', key='name', value=f'ch_{i}')
self.add_entry(channel)
Expand Down Expand Up @@ -373,7 +375,7 @@ def remove_attr(self, name: str):
del self.attrib[name]
del self.__dict__[name]
else:
logging.error('{} not in attrib'.format(name))
logger.error('{} not in attrib'.format(name))
self._autosave()
return self

Expand Down Expand Up @@ -438,12 +440,12 @@ def __init__(self, id, attrib=None, parent='.', **kwargs):
valid_filename(self.id)
self._filename = os.path.join(self._folder, self.id)
if not os.access(self._filename, os.F_OK):
logging.error('File {} does not exist'.format(self.id))
logger.error('File {} does not exist'.format(self.id))
elif id:
# writing entry
valid_filename(id)
if os.path.splitext(str(id))[-1] == '':
logging.warning('id should be a filename with extension ie. .bin')
logger.warning('id should be a filename with extension ie. .bin')
self._filename = os.path.join(self._folder, id)
self.set_attrib('id', id)
# ensure subdirectories exist to write data
Expand Down Expand Up @@ -623,7 +625,7 @@ def __init__(self, id=None, attrib=None, parent='.',
assert decimalSeparator and separator, 'Must supply separators'

if not self.id.endswith('csv'):
logging.warning(f'id "{id}" does not end in .csv')
logger.warning(f'id "{id}" does not end in .csv')

csvFileFormat = MiscEntry('csvFileFormat', parent=self)
csvFileFormat.set_attrib('decimalSeparator', decimalSeparator)
Expand Down Expand Up @@ -652,8 +654,8 @@ def set_data(self, data: list, **kwargs):
sep = self.csvFileFormat.separator
dec = self.csvFileFormat.decimalSeparator

if len(data) == 0 or len(data[0]) < 2: logging.warning('Should supply at least two columns: ' \
'time and data')
if len(data) == 0 or len(data[0]) < 2:
logger.warning('Should supply at least two columns: time and data')

write_csv(self._filename, data, sep=sep, decimal_sep=dec)

Expand Down
8 changes: 5 additions & 3 deletions unisens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from .utils import AttrDict, strip, validkey, lowercase, make_key, indent
from .utils import str2num

logger = logging.getLogger("unisens")


class Unisens(Entry):
"""
Expand Down Expand Up @@ -78,12 +80,12 @@ def __init__(self, folder: str, makenew=False, autosave=False, readonly=False,
self._convert_nums = convert_nums

if os.path.isfile(self._file) and not makenew:
logging.debug('loading unisens.xml from {}'.format(self._file))
logger.debug('loading unisens.xml from {}'.format(self._file))
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.read_unisens()
else:
logging.debug('New unisens.xml will be created at {}'.format(self._file))
logger.debug('New unisens.xml will be created at {}'.format(self._file))
if not timestampStart:
now = datetime.datetime.now()
timestampStart = now.strftime('%Y-%m-%dT%H:%M:%S')
Expand Down Expand Up @@ -188,7 +190,7 @@ def unpack_element(self, element: (Element, ET)) -> Entry:
entry = MiscEntry(name=name, attrib=attrib, parent=self._folder)
else:
if not 'Entry' in element.tag:
logging.warning('Unknown entry type: {}'.format(entryType))
logger.warning('Unknown entry type: {}'.format(entryType))
name = element.tag
entry = MiscEntry(name=name, attrib=attrib, parent=self._folder)

Expand Down

0 comments on commit 56861b9

Please sign in to comment.