Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/sonic-py-common/sonic_py_common/daemon_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#
# Constants ====================================================================
#

REDIS_TIMEOUT_MSECS = 0

EEPROM_MODULE_NAME = 'eeprom'
Expand All @@ -30,7 +31,11 @@ def db_connect(db_name, namespace=EMPTY_NAMESPACE):

class DaemonBase(Logger):
def __init__(self, log_identifier):
super(DaemonBase, self).__init__(log_identifier, Logger.LOG_FACILITY_DAEMON)
super(DaemonBase, self).__init__(
log_identifier=log_identifier,
log_facility=Logger.LOG_FACILITY_DAEMON,
log_option=(Logger.LOG_OPTION_NDELAY | Logger.LOG_OPTION_PID)
)

# Register our default signal handlers, unless the signal already has a
# handler registered, most likely from a subclass implementation
Expand Down
32 changes: 20 additions & 12 deletions src/sonic-py-common/sonic_py_common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@ class Logger(object):
"""
Logger class for SONiC Python applications
"""
LOG_FACILITY_USER = syslog.LOG_USER
LOG_FACILITY_DAEMON = syslog.LOG_DAEMON
LOG_FACILITY_USER = syslog.LOG_USER

LOG_OPTION_NDELAY = syslog.LOG_NDELAY
LOG_OPTION_PID = syslog.LOG_PID

LOG_PRIORITY_ERROR = syslog.LOG_ERR
LOG_PRIORITY_WARNING = syslog.LOG_WARNING
LOG_PRIORITY_NOTICE = syslog.LOG_NOTICE
LOG_PRIORITY_INFO = syslog.LOG_INFO
LOG_PRIORITY_DEBUG = syslog.LOG_DEBUG

def __init__(self, log_identifier=None, log_facility=LOG_FACILITY_USER):
self.syslog = syslog
DEFAULT_LOG_FACILITY = syslog.LOG_USER
DEFAULT_LOG_OPTION = syslog.LOG_NDELAY

def __init__(self, log_identifier=None, log_facility=DEFAULT_LOG_FACILITY, log_option=DEFAULT_LOG_OPTION):
self._syslog = syslog

if not log_identifier:
if log_identifier is None:
log_identifier = os.path.basename(sys.argv[0])

self.syslog.openlog(ident=log_identifier,
logoption=(syslog.LOG_PID | syslog.LOG_NDELAY),
facility=log_facility)
# Initialize syslog
self._syslog.openlog(ident=log_identifier, logoption=log_option, facility=log_facility)

# Set the default minimum log priority to LOG_PRIORITY_NOTICE
self.set_min_log_priority(self.LOG_PRIORITY_NOTICE)

def __del__(self):
self.syslog.closelog()
self._syslog.closelog()

#
# Methods for setting minimum log priority
Expand All @@ -48,7 +53,7 @@ def set_min_log_priority(self, priority):
Args:
priority: The minimum priority at which to log messages
"""
self.syslog.setlogmask(self.syslog.LOG_UPTO(priority))
self._min_log_priority = priority

def set_min_log_priority_error(self):
"""
Expand Down Expand Up @@ -85,10 +90,13 @@ def set_min_log_priority_debug(self):
#

def log(self, priority, msg, also_print_to_console=False):
self.syslog.syslog(priority, msg)
if self._min_log_priority >= priority:
# Send message to syslog
self._syslog.syslog(priority, msg)

if also_print_to_console:
print(msg)
# Send message to console
if also_print_to_console:
print(msg)

def log_error(self, msg, also_print_to_console=False):
self.log(self.LOG_PRIORITY_ERROR, msg, also_print_to_console)
Expand Down