Skip to content

Commit ba6f012

Browse files
authored
[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 <[email protected]>
1 parent 67408c8 commit ba6f012

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/sonic-py-common/sonic_py_common/daemon_base.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#
99
# Constants ====================================================================
1010
#
11+
1112
REDIS_TIMEOUT_MSECS = 0
1213

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

3132
class DaemonBase(Logger):
3233
def __init__(self, log_identifier):
33-
super(DaemonBase, self).__init__(log_identifier, Logger.LOG_FACILITY_DAEMON)
34+
super(DaemonBase, self).__init__(
35+
log_identifier=log_identifier,
36+
log_facility=Logger.LOG_FACILITY_DAEMON,
37+
log_option=(Logger.LOG_OPTION_NDELAY | Logger.LOG_OPTION_PID)
38+
)
3439

3540
# Register our default signal handlers, unless the signal already has a
3641
# handler registered, most likely from a subclass implementation

src/sonic-py-common/sonic_py_common/logger.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,35 @@ class Logger(object):
1111
"""
1212
Logger class for SONiC Python applications
1313
"""
14-
LOG_FACILITY_USER = syslog.LOG_USER
1514
LOG_FACILITY_DAEMON = syslog.LOG_DAEMON
15+
LOG_FACILITY_USER = syslog.LOG_USER
16+
17+
LOG_OPTION_NDELAY = syslog.LOG_NDELAY
18+
LOG_OPTION_PID = syslog.LOG_PID
1619

1720
LOG_PRIORITY_ERROR = syslog.LOG_ERR
1821
LOG_PRIORITY_WARNING = syslog.LOG_WARNING
1922
LOG_PRIORITY_NOTICE = syslog.LOG_NOTICE
2023
LOG_PRIORITY_INFO = syslog.LOG_INFO
2124
LOG_PRIORITY_DEBUG = syslog.LOG_DEBUG
2225

23-
def __init__(self, log_identifier=None, log_facility=LOG_FACILITY_USER):
24-
self.syslog = syslog
26+
DEFAULT_LOG_FACILITY = LOG_FACILITY_USER
27+
DEFAULT_LOG_OPTION = LOG_OPTION_NDELAY
28+
29+
def __init__(self, log_identifier=None, log_facility=DEFAULT_LOG_FACILITY, log_option=DEFAULT_LOG_OPTION):
30+
self._syslog = syslog
2531

26-
if not log_identifier:
32+
if log_identifier is None:
2733
log_identifier = os.path.basename(sys.argv[0])
2834

29-
self.syslog.openlog(ident=log_identifier,
30-
logoption=(syslog.LOG_PID | syslog.LOG_NDELAY),
31-
facility=log_facility)
35+
# Initialize syslog
36+
self._syslog.openlog(ident=log_identifier, logoption=log_option, facility=log_facility)
3237

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

3641
def __del__(self):
37-
self.syslog.closelog()
42+
self._syslog.closelog()
3843

3944
#
4045
# Methods for setting minimum log priority
@@ -48,7 +53,7 @@ def set_min_log_priority(self, priority):
4853
Args:
4954
priority: The minimum priority at which to log messages
5055
"""
51-
self.syslog.setlogmask(self.syslog.LOG_UPTO(priority))
56+
self._min_log_priority = priority
5257

5358
def set_min_log_priority_error(self):
5459
"""
@@ -85,10 +90,13 @@ def set_min_log_priority_debug(self):
8590
#
8691

8792
def log(self, priority, msg, also_print_to_console=False):
88-
self.syslog.syslog(priority, msg)
93+
if self._min_log_priority >= priority:
94+
# Send message to syslog
95+
self._syslog.syslog(priority, msg)
8996

90-
if also_print_to_console:
91-
print(msg)
97+
# Send message to console
98+
if also_print_to_console:
99+
print(msg)
92100

93101
def log_error(self, msg, also_print_to_console=False):
94102
self.log(self.LOG_PRIORITY_ERROR, msg, also_print_to_console)

0 commit comments

Comments
 (0)