[Pkg-mpd-commits] [python-mpd] 152/262: logging: log MPD commands called

Simon McVittie smcv at debian.org
Sun May 22 18:16:40 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch upstream
in repository python-mpd.

commit 668225a001bdd7347433b82f781adf6bc7c74409
Author: Jonathan Ballet <jon at multani.info>
Date:   Sun Nov 25 10:44:12 2012 +0100

    logging: log MPD commands called
---
 README.md | 18 ++++++++++++++++++
 mpd.py    | 20 ++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/README.md b/README.md
index db0e1b1..4162fee 100644
--- a/README.md
+++ b/README.md
@@ -20,6 +20,7 @@ The following features was added:
  - documented API to add new commands (see Future Compatible)
  - use unicode strings in all commands (optionally in python2, default in python3 - see Unicode Handling)
  - configureable timeout
+ - support for logging
 
 If you like this module, you could try contact the original author <jat at spatialrift.net> or
 join the discussion on the [issue tracker](http://jatreuman.indefero.net/p/python-mpd/issues/7/)
@@ -168,6 +169,23 @@ u'http'
 
 Use this option in python3 doesn't have an effect.
 
+Logging
+-------
+
+By default messages are sent to the logger named `mpd`:
+
+```python
+>>> import logging, mpd
+>>> logging.basicConfig(level=logging.DEBUG)
+>>> client = mpd.MPDClient()
+>>> client.connect("localhost", 6600)
+INFO:mpd:Calling MPD connect('localhost', 6600, timeout=None)
+>>> client.find('any', 'dubstep')
+DEBUG:mpd:Calling MPD find('any', 'dubstep')
+```
+
+For more information about logging configuration, see http://docs.python.org/2/howto/logging.html
+
 Future Compatible
 -----------------
 
diff --git a/mpd.py b/mpd.py
index 9f1f50c..ecd004b 100644
--- a/mpd.py
+++ b/mpd.py
@@ -15,6 +15,7 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with python-mpd2.  If not, see <http://www.gnu.org/licenses/>.
 
+import logging
 import sys
 import socket
 import warnings
@@ -33,6 +34,16 @@ else:
     decode_str = lambda s: s
     encode_str = lambda s: str(s)
 
+try:
+    from logging import NullHandler
+except ImportError: # NullHandler was introduced in python2.7
+    class NullHandler(logging.Handler):
+        def emit(self, record):
+            pass
+
+logger = logging.getLogger(__name__)
+logger.addHandler(NullHandler())
+
 class MPDError(Exception):
     pass
 
@@ -224,6 +235,12 @@ class MPDClient(object):
         parts = [command]
         for arg in args:
             parts.append('"%s"' % escape(encode_str(arg)))
+        # Minimize logging cost if the logging is not activated.
+        if logger.isEnabledFor(logging.DEBUG):
+            if command == "password":
+                logger.debug("Calling MPD password(******)")
+            else:
+                logger.debug("Calling MPD %s%r", command, args)
         self._write_line(" ".join(parts))
 
     def _read_line(self):
@@ -435,6 +452,8 @@ class MPDClient(object):
     idletimeout = None
 
     def connect(self, host, port, timeout=None):
+        logger.info("Calling MPD connect(%r, %r, timeout=%r)", host,
+                     port, timeout)
         if self._sock is not None:
             raise ConnectionError("Already connected")
         if timeout != None:
@@ -455,6 +474,7 @@ class MPDClient(object):
             raise
 
     def disconnect(self):
+        logger.info("Calling MPD disconnect()")
         if not self._rfile is None:
             self._rfile.close()
         if not self._wfile is None:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mpd/python-mpd.git



More information about the Pkg-mpd-commits mailing list