Skip to content

Commit

Permalink
fixes to send debug to syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
grybak-arista committed Apr 1, 2016
1 parent 0da4378 commit e0e8097
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
23 changes: 2 additions & 21 deletions pyeapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,12 @@
from ConfigParser import SafeConfigParser
from ConfigParser import Error as SafeConfigParserError

from pyeapi.utils import load_module, make_iterable
from pyeapi.utils import load_module, make_iterable, debug

from pyeapi.eapilib import HttpEapiConnection, HttpsEapiConnection
from pyeapi.eapilib import SocketEapiConnection, HttpLocalEapiConnection
from pyeapi.eapilib import CommandError

if sys.platform == "darwin":
# Mac OS syslog
address = '/var/run/syslog'
elif sys.platform == "win32":
# Windows write to localhost, port 514
# This is the default SysLogHandler address, but needs to be
# specified since we are using a variable.
address = ('localhost', 514)
else:
# Most *nix syslog
address = '/var/log/syslog'

LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler()
formatter = logging.Formatter('pyeapi.%(module)s.%(funcName)s: %(message)s')
handler.setFormatter(formatter)
LOGGER.addHandler(handler)

CONFIG_SEARCH_PATH = ['~/.eapi.conf', '/mnt/flash/eapi.conf']

TRANSPORTS = {
Expand Down Expand Up @@ -218,7 +199,7 @@ def read(self, filename):
# Ignore file and syslog a message on SafeConfigParser errors
msg = ("%s: parsing error in eapi conf file: %s" %
(type(exc).__name__, filename))
LOGGER.debug(msg)
debug(msg)

self._add_default_connection()

Expand Down
2 changes: 1 addition & 1 deletion pyeapi/eapilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def send(self, data):
# For Python 3.x - decode bytes into string
response_content = response_content.decode()
decoded = json.loads(response_content)
debug('eapi_response: %s' % decoded)
_LOGGER.debug('eapi_response: %s' % decoded)

if 'error' in decoded:
(code, msg, err, out) = self._parse_error_message(decoded)
Expand Down
28 changes: 20 additions & 8 deletions pyeapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import os
import sys
import imp
import inspect
import logging
import logging.handlers
import syslog
import collections

from itertools import tee
Expand All @@ -47,10 +47,20 @@
from itertools import izip_longest as zip_longest

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)

_syslog_handler = logging.handlers.SysLogHandler()
# Create a handler to log messages to syslog
if sys.platform == "darwin":
_syslog_handler = logging.handlers.SysLogHandler(address='/var/run/syslog')
else:
_syslog_handler = logging.handlers.SysLogHandler()
_LOGGER.addHandler(_syslog_handler)
_LOGGER.setLevel(logging.INFO)

# Create a handler to log messages to stderr
_stderr_formatter = logging.Formatter('\n\n******** LOG NOTE ********\n%(message)s\n')
_stderr_handler = logging.StreamHandler()
_stderr_handler.setFormatter(_stderr_formatter)
_LOGGER.addHandler(_stderr_handler)

def import_module(name):
""" Imports a module into the current runtime environment
Expand Down Expand Up @@ -140,15 +150,17 @@ def islocalconnection():
return os.path.exists('/etc/Eos-release')

def debug(text):
"""Prints text to syslog when on a local connection
"""Log a message to syslog and stderr
Args:
text (str): The string object to print to syslog
text (str): The string object to print
"""

if islocalconnection():
_LOGGER.debug(text)
frame = inspect.currentframe().f_back
module = frame.f_globals['__name__']
func = frame.f_code.co_name
msg = "%s.%s: %s" % (module, func, text)
_LOGGER.debug(msg)

def make_iterable(value):
"""Converts the supplied value to a list object
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ def test_collapse_mixed(self):
def test_debug(self, mock_logger):
pyeapi.utils.islocalconnection = Mock(return_value=True)
pyeapi.utils.debug('test')
mock_logger.debug.assert_called_with('test')
mock_logger.debug.assert_called_with('test_utils.test_debug: test')

0 comments on commit e0e8097

Please sign in to comment.