-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SysLogger for new implementation. (#17171)
This PR introduces the SysLogger to address an issue where the original Logger.py could override the SYSLOG_IDENTIFIER if a new Logger instance is created. - Why I did it? This change was made because the SysLog was displaying NOTICE pmon#CCmisApi: instead of NOTICE pmon#ycable:. This discrepancy led to confusion during debugging and incorrect component identification in log analysis. - How I did it I implemented an instance-level syslog using the logging.handlers module to prevent the SYSLOG_IDENTIFIER from being overridden.
- Loading branch information
1 parent
a480f8d
commit 166a9fb
Showing
3 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import logging | ||
from logging.handlers import SysLogHandler | ||
import os | ||
import socket | ||
import sys | ||
|
||
class SysLogger: | ||
""" | ||
SysLogger class for Python applications using SysLogHandler | ||
""" | ||
|
||
DEFAULT_LOG_FACILITY = SysLogHandler.LOG_USER | ||
DEFAULT_LOG_LEVEL = SysLogHandler.LOG_NOTICE | ||
|
||
def __init__(self, log_identifier=None, log_facility=DEFAULT_LOG_FACILITY, log_level=DEFAULT_LOG_LEVEL): | ||
if log_identifier is None: | ||
log_identifier = os.path.basename(sys.argv[0]) | ||
|
||
# Initialize SysLogger | ||
self.logger = logging.getLogger(log_identifier) | ||
|
||
# Reset all existing handlers | ||
for handler in self.logger.handlers[:]: | ||
self.logger.removeHandler(handler) | ||
|
||
handler = SysLogHandler(address="/dev/log", facility=log_facility, socktype=socket.SOCK_DGRAM) | ||
formatter = logging.Formatter('%(name)s[%(process)d]: %(message)s') | ||
handler.setFormatter(formatter) | ||
self.logger.addHandler(handler) | ||
|
||
self.set_min_log_priority(log_level) | ||
|
||
def set_min_log_priority(self, priority): | ||
""" | ||
Sets the minimum log priority level. All log messages | ||
with a priority lower than this will not be logged | ||
""" | ||
self._min_log_level = priority | ||
self.logger.setLevel(priority) | ||
|
||
# Methods for logging messages | ||
def log(self, priority, msg, also_print_to_console=False): | ||
self.logger.log(priority, msg) | ||
|
||
if also_print_to_console: | ||
print(msg) | ||
|
||
# Convenience methods | ||
def log_error(self, msg, also_print_to_console=False): | ||
self.log(logging.ERROR, msg, also_print_to_console) | ||
|
||
def log_warning(self, msg, also_print_to_console=False): | ||
self.log(logging.WARNING, msg, also_print_to_console) | ||
|
||
def log_notice(self, msg, also_print_to_console=False): | ||
self.log(logging.INFO, msg, also_print_to_console) | ||
|
||
def log_info(self, msg, also_print_to_console=False): | ||
self.log(logging.INFO, msg, also_print_to_console) | ||
|
||
def log_debug(self, msg, also_print_to_console=False): | ||
self.log(logging.DEBUG, msg, also_print_to_console) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters