Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pyeapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@

"""
import os
import sys
import logging
import re

Expand All @@ -104,14 +105,12 @@
from ConfigParser import SafeConfigParser
from ConfigParser import Error as SafeConfigParserError

from pyeapi.utils import load_module, make_iterable, syslog_warning
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

LOGGER = logging.getLogger(__name__)

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

TRANSPORTS = {
Expand Down Expand Up @@ -193,12 +192,14 @@ def read(self, filename):
Args:
filename (str): The full path to the file to load
"""

try:
SafeConfigParser.read(self, filename)
except SafeConfigParserError as exc:
# Ignore file and syslog a message on SafeConfigParser errors
syslog_warning("%s: parsing error in eapi conf file: %s" %
(type(exc).__name__, filename))
msg = ("%s: parsing error in eapi conf file: %s" %
(type(exc).__name__, filename))
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
39 changes: 20 additions & 19 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,26 +150,17 @@ def islocalconnection():
return os.path.exists('/etc/Eos-release')

def debug(text):
"""Prints text to syslog when on a local connection

Args:
text (str): The string object to print to syslog

"""

if islocalconnection():
_LOGGER.debug(text)

def syslog_warning(text):
"""Print text to syslog at warning level
"""Log a message to syslog and stderr

Args:
text (str): The string object to print to syslog
text (str): The string object to print

"""

syslog.openlog("pyeapi")
syslog.syslog(syslog.LOG_WARNING, 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')