Change to use getlogger - #36
Conversation
Fix logging
nitobuendia
left a comment
There was a problem hiding this comment.
Hi @johro897 - thanks a lot for your contribution and taking the time to do this. This is definitely a helpful change and I am happy to merge it. However, in this state, these changes introduce several changes in code style (e.g. f-strings, 80 chars line limit), messaging (e.g. removing self._name, using Couldnt instead of Couldn't) and flow logic with impact in readability (some if-statement changes).
I've added comments where I saw these points. However, maybe an easier approach is that, given your description, there should only be exactly 2 types of changes:
- Add
_LOGGER = logging.getLogger(__name__)where the module constants are defined. - Replace all instances of
logging.xxxwith_LOGGER.xxx
There should be no other changes regarding messaging or logic.
Would you be open to making these changes so we can merge your pull request?
| ) | ||
| ) | ||
| if date_value: | ||
| message = 'No Oura data found for '+ date_name_title +' ('+ original_date +'). Fetching '+ date_value + 'instead.' |
There was a problem hiding this comment.
This has an issue because there's no space before instead, so the date would merge. Also space before and after spaces are desirable for readability.
To solve for all this, let's change to f-strings here and 258.
| ) | ||
| ) | ||
| if date_value: | ||
| message = 'No Oura data found for '+ date_name_title +' ('+ original_date +'). Fetching '+ date_value + 'instead.' |
There was a problem hiding this comment.
This comment is missing self._name which might be helpful for debugging.
| if not oura_data or data_param not in oura_data: | ||
| logging.error( | ||
| f'Oura ({self._name}): Couldn\'t fetch data for Oura ring sensor.') | ||
| _LOGGER.error('Couldnt fetch data for Oura ring sensor.') |
There was a problem hiding this comment.
Couldn't - to avoid probelsm with the single quotes, you can escape it 'Couldn't ...'
Aso, this is missing self._name. It might be worth just leaving Oura ({self._name}) on all of them even if this is now done by _LOGGER which will include the module name.
| else: | ||
| message = 'Unable to find suitable backfill date. No data available.' | ||
| _LOGGER.warning(message) | ||
|
|
There was a problem hiding this comment.
Ditto from before: change to f-strings and keep Oura ({self._name}.
| message = 'No Oura data found for '+ date_name_title +' ('+ original_date +'). Fetching '+ date_value + 'instead.' | ||
| else: | ||
| message = 'Unable to find suitable backfill date. No data available.' | ||
| _LOGGER.warning(message) |
There was a problem hiding this comment.
nit: to keep consistent with the other file (e.g.sensor_base_dated lines 258-260) add a break line before _LOGGER.warning()
| from homeassistant.helpers import entity | ||
| from . import api | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) |
There was a problem hiding this comment.
Actually, _LOGGER is not used in this file. I think a better change would be removing line 9 and import logging on line 3 altogether. My bad.
| logging.info( | ||
| f'Oura ({self._name}): ' + | ||
| 'Unknown day name `{date_name}`, using yesterday.') | ||
| _LOGGER.info('Unknown day name ' + date_name + 'using yesterday.') |
There was a problem hiding this comment.
Change to f-strings and keep self._name. The first change would ensure higher consistency and readability. The second would help in troubleshooting when multiple Oura sensors are added.
| ) | ||
| ) | ||
| if date_value: | ||
| message = 'No Oura data found for '+ date_name_title +' ('+ original_date +'). Fetching '+ date_value + 'instead.' |
There was a problem hiding this comment.
This exceeds 80 chars. Break it into lines of max 80 chars. That's why the original is broken down into concatenated strings.
| f'Unable to find suitable backfill date. No data available.' | ||
| ) | ||
| ) | ||
| if date_value: |
There was a problem hiding this comment.
Can we keep the original if logic structure?
| ) | ||
| ) | ||
| if date_value: | ||
| message = 'No Oura data found for '+ date_name_title +' ('+ original_date +'). Fetching '+ date_value + 'instead.' |
There was a problem hiding this comment.
Ditto: change to f-strings, keep lines below 80 chars and keep Oura ({self._name})
update to use getlogger in order to remove the [root] message in logs