Skip to content

Avoid use of root logger and fix close callback in python3 - #1415

Merged
peternewman merged 6 commits into
OpenLightingProject:masterfrom
shenghaoyang:logging
Jun 9, 2018
Merged

Avoid use of root logger and fix close callback in python3#1415
peternewman merged 6 commits into
OpenLightingProject:masterfrom
shenghaoyang:logging

Conversation

@shenghaoyang

@shenghaoyang shenghaoyang commented May 5, 2018

Copy link
Copy Markdown
Contributor
  • Using the root logger makes it non-obvious for users of the library
    to control the log messages from the library.

  • By setting up a logger for ola and using it for all the logging
    calls, users of the library can simply do logging.getLogger('ola')
    and control the output of the logger.

  • Note that any tools / tests depending on log output might need to
    be changed due to the reasons above. Those changes can be done,
    but I want to put this commit out first so we can discuss this approach,
    or we could skip emulating past behavior and simply adopt
    the convention for python libraries.

  • Empty string object is no longer returned by recv() in python3
    on connection close, but an empty bytes object is returned instead.
    Comparing the return value to an empty string is insufficient.
    Since both objects evaluate to false, check for that condition and
    invoke close actions accordingly.

- Using the root logger makes it non-obvious for users of the library
  to control the log messages from the library.

- By setting up a logger for ola and using it for all the logging
  calls, users of the library can simply do logging.getlogger('ola')
  and control the output of the logger.

- However, even though the convention for library loggers is to
  attach a do nothing handler and not propagate messages, it is not
  followed here because the library logged directly to the
  root logger before, and removing all log messages will then
  cause missing output for users that expected log entries.

- Instead, the configuration for the library's logger emulates what
  basicConfig() would do - setup a StreamHandler and a default formatter
  that uses BASIC_FORMAT, and outputs logs in this configuration, to
  emulate past behaviour as closely as possible, for users that
  never configured the root logger.

- There are two issues, though. If the user did not setup the
  root logger and OLA emits log messages, they will now appear as
  before, but without the designation that they were
  from the root logger, but designated as coming from the logger
  named ola.

- If the user of the library knew that OLA was emitting messages from
  the root logger and altered the behaviour of the root logger to
  alter log emission, they will find that it is useless, as OLA
  now by default logs straight to standard error through the
  StreamHandler. To change OLA's logging behaviour, they will have to
  remove the default StreamHandler added and alter propagation settings.
@shenghaoyang

Copy link
Copy Markdown
Contributor Author

@maintainers
Found another little bug in the python library - could I consolidate everything into this request?

@peternewman

Copy link
Copy Markdown
Member

Yeah go for it @shenghaoyang . Just retitle the PR to suit.

peternewman and others added 2 commits May 7, 2018 17:01
- In python3, recv() no longer returns a string object, so comparing
  against an empty string will always fail and the close callback
  is never called - program never gets notified that the socket is
  closed.

- Both b'' and '' evaluate to false, so just check if the recv() call
  returns an object that is implicitly false and invoke the
  callback accordingly.
@shenghaoyang shenghaoyang changed the title Change logging configuration to avoid usage of the root logger. Avoid use of root logger and fix close callback in python3 May 8, 2018
Comment thread python/ola/__init__.py Outdated

import logging

"""Initialization code for the ola python library"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy doesn't like this:
"String statement has no effect"
https://app.codacy.com/app/peternewman/ola/pullRequest?prid=1616358

I think it may need to be above the import, looking at some other real world examples.

@shenghaoyang

shenghaoyang commented May 11, 2018 via email

Copy link
Copy Markdown
Contributor Author

@peternewman peternewman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @shenghaoyang !

@peternewman
peternewman merged commit dd13514 into OpenLightingProject:master Jun 9, 2018
@peternewman peternewman added this to the 0.11.0 milestone Jun 12, 2018
@shenghaoyang
shenghaoyang deleted the logging branch July 8, 2018 15:10
@peternewman peternewman mentioned this pull request Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants