[sonic-py-common]: Fix syslog implicit min priority override (#5707)

Current implementation of logger class is based on standard python syslog library.  
Thus, logger class can be instantiated in different places and share the same context across the entire process.  
This means that reducing log severity level will affect other modules which use logging facility.

**- Why I did it**
* To fix syslog implicit min priority override

**- How I did it**
* Added per instance log severity check

**- How to verify it**
1. Run code snippet
```
from sonic_py_common import logger

log1 = logger.Logger(log_identifier='myApp1')
log1.set_min_log_priority_debug()
log1.log_error("=> this is error")
log1.log_warning("=> this is warning")
log1.log_notice("=> this is notice")
log1.log_info("=> this is info")
log1.log_debug("=> this is debug")

log2 = logger.Logger(
    log_identifier='myApp2',
    log_facility=logger.Logger.LOG_FACILITY_DAEMON,
    log_option=(logger.Logger.LOG_OPTION_NDELAY | logger.Logger.LOG_OPTION_PID)
)
log2.log_error("=> this is error")
log2.log_warning("=> this is warning")
log2.log_notice("=> this is notice")
log2.log_info("=> this is info")
log2.log_debug("=> this is debug")
```
2. Sample output:
```
Oct 23 15:08:30.447301 sonic ERR myApp1: => this is error
Oct 23 15:08:30.447908 sonic WARNING myApp1: => this is warning
Oct 23 15:08:30.448305 sonic NOTICE myApp1: => this is notice
Oct 23 15:08:30.448696 sonic INFO myApp1: => this is info
Oct 23 15:08:30.449063 sonic DEBUG myApp1: => this is debug

Oct 23 15:08:30.449442 sonic ERR myApp2[19178]: => this is error
Oct 23 15:08:30.449819 sonic WARNING myApp2[19178]: => this is warning
Oct 23 15:08:30.450183 sonic NOTICE myApp2[19178]: => this is notice
```

Signed-off-by: Nazarii Hnydyn <nazariig@nvidia.com>
This commit is contained in:
Nazarii Hnydyn 2020-10-24 22:36:22 +03:00 committed by GitHub
parent 67408c85aa
commit ba6f012cc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 13 deletions

View File

@ -8,6 +8,7 @@ from .logger import Logger
# #
# Constants ==================================================================== # Constants ====================================================================
# #
REDIS_TIMEOUT_MSECS = 0 REDIS_TIMEOUT_MSECS = 0
EEPROM_MODULE_NAME = 'eeprom' EEPROM_MODULE_NAME = 'eeprom'
@ -30,7 +31,11 @@ def db_connect(db_name, namespace=EMPTY_NAMESPACE):
class DaemonBase(Logger): class DaemonBase(Logger):
def __init__(self, log_identifier): 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 # Register our default signal handlers, unless the signal already has a
# handler registered, most likely from a subclass implementation # handler registered, most likely from a subclass implementation

View File

@ -11,8 +11,11 @@ class Logger(object):
""" """
Logger class for SONiC Python applications Logger class for SONiC Python applications
""" """
LOG_FACILITY_USER = syslog.LOG_USER
LOG_FACILITY_DAEMON = syslog.LOG_DAEMON 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_ERROR = syslog.LOG_ERR
LOG_PRIORITY_WARNING = syslog.LOG_WARNING LOG_PRIORITY_WARNING = syslog.LOG_WARNING
@ -20,21 +23,23 @@ class Logger(object):
LOG_PRIORITY_INFO = syslog.LOG_INFO LOG_PRIORITY_INFO = syslog.LOG_INFO
LOG_PRIORITY_DEBUG = syslog.LOG_DEBUG LOG_PRIORITY_DEBUG = syslog.LOG_DEBUG
def __init__(self, log_identifier=None, log_facility=LOG_FACILITY_USER): DEFAULT_LOG_FACILITY = LOG_FACILITY_USER
self.syslog = syslog DEFAULT_LOG_OPTION = LOG_OPTION_NDELAY
if not log_identifier: def __init__(self, log_identifier=None, log_facility=DEFAULT_LOG_FACILITY, log_option=DEFAULT_LOG_OPTION):
self._syslog = syslog
if log_identifier is None:
log_identifier = os.path.basename(sys.argv[0]) log_identifier = os.path.basename(sys.argv[0])
self.syslog.openlog(ident=log_identifier, # Initialize syslog
logoption=(syslog.LOG_PID | syslog.LOG_NDELAY), self._syslog.openlog(ident=log_identifier, logoption=log_option, facility=log_facility)
facility=log_facility)
# Set the default minimum log priority to LOG_PRIORITY_NOTICE # Set the default minimum log priority to LOG_PRIORITY_NOTICE
self.set_min_log_priority(self.LOG_PRIORITY_NOTICE) self.set_min_log_priority(self.LOG_PRIORITY_NOTICE)
def __del__(self): def __del__(self):
self.syslog.closelog() self._syslog.closelog()
# #
# Methods for setting minimum log priority # Methods for setting minimum log priority
@ -48,7 +53,7 @@ class Logger(object):
Args: Args:
priority: The minimum priority at which to log messages 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): def set_min_log_priority_error(self):
""" """
@ -85,8 +90,11 @@ class Logger(object):
# #
def log(self, priority, msg, also_print_to_console=False): 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)
# Send message to console
if also_print_to_console: if also_print_to_console:
print(msg) print(msg)