Skip to content

Commit

Permalink
Do not fail if syslog is not available
Browse files Browse the repository at this point in the history
Some platforms (e.g. Windows) do not have the syslog module and
therefore fails when importing it. Handle that case

Change-Id: I86ca0ff2bb3fbfbcaf4a04692ff7871654add5bc
Closes-Bug: #1459187
  • Loading branch information
jd committed May 27, 2015
1 parent 7730773 commit 1d64c91
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
55 changes: 30 additions & 25 deletions oslo_log/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import logging.config
import logging.handlers
import os
import syslog
try:
import syslog
except ImportError:
syslog = None

from debtcollector import removals

Expand Down Expand Up @@ -68,31 +71,33 @@ def format(self, record):
_AUDIT = logging.INFO + 1


class OSSysLogHandler(logging.Handler):
severity_map = {
"CRITICAL": syslog.LOG_CRIT,
"DEBUG": syslog.LOG_DEBUG,
"ERROR": syslog.LOG_ERR,
"INFO": syslog.LOG_INFO,
"WARNING": syslog.LOG_WARNING,
"WARN": syslog.LOG_WARNING,
}
if syslog is not None:
class OSSysLogHandler(logging.Handler):
"""Syslog based handler. Only available on UNIX-like platforms."""
severity_map = {
"CRITICAL": syslog.LOG_CRIT,
"DEBUG": syslog.LOG_DEBUG,
"ERROR": syslog.LOG_ERR,
"INFO": syslog.LOG_INFO,
"WARNING": syslog.LOG_WARNING,
"WARN": syslog.LOG_WARNING,
}

def __init__(self, facility=syslog.LOG_USER,
use_syslog_rfc_format=True):
# Do not use super() unless type(logging.Handler) is 'type'
# (i.e. >= Python 2.7).
logging.Handler.__init__(self)
if use_syslog_rfc_format:
binary_name = _get_binary_name()
else:
binary_name = ""
syslog.openlog(binary_name, 0, facility)

def __init__(self, facility=syslog.LOG_USER,
use_syslog_rfc_format=True):
# Do not use super() unless type(logging.Handler) is 'type'
# (i.e. >= Python 2.7).
logging.Handler.__init__(self)
if use_syslog_rfc_format:
binary_name = _get_binary_name()
else:
binary_name = ""
syslog.openlog(binary_name, 0, facility)

def emit(self, record):
syslog.syslog(self.severity_map.get(record.levelname,
syslog.LOG_DEBUG),
record.getMessage())
def emit(self, record):
syslog.syslog(self.severity_map.get(record.levelname,
syslog.LOG_DEBUG),
record.getMessage())


class ColorHandler(logging.StreamHandler):
Expand Down
8 changes: 7 additions & 1 deletion oslo_log/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
import logging.handlers
import os
import sys
import syslog
try:
import syslog
except ImportError:
syslog = None
import traceback

from oslo_config import cfg
Expand Down Expand Up @@ -313,6 +316,9 @@ def _setup_logging_from_conf(conf, project, version):
log_root.addHandler(handler)

if conf.use_syslog:
global syslog
if syslog is None:
raise RuntimeError("syslog is not available on this platform")
facility = _find_facility(conf.syslog_log_facility)
# TODO(bogdando) use the format provided by RFCSysLogHandler after
# existing syslog format deprecation in J
Expand Down
7 changes: 6 additions & 1 deletion oslo_log/tests/unit/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import logging
import os
import sys
import syslog
try:
import syslog
except ImportError:
syslog = None
import tempfile

import mock
Expand All @@ -29,6 +32,7 @@
from oslo_serialization import jsonutils
from oslotest import base as test_base
import six
import testtools

from oslo_log import _options
from oslo_log import formatters
Expand Down Expand Up @@ -211,6 +215,7 @@ def test_standard_format(self):
expected.getMessage())


@testtools.skipUnless(syslog, "syslog is not available")
class OSSysLogHandlerTestCase(BaseTestCase):
def tests_handler(self):
handler = handlers.OSSysLogHandler()
Expand Down

0 comments on commit 1d64c91

Please sign in to comment.