Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support notice level log #19232

Merged
merged 2 commits into from
Aug 13, 2024
Merged
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
11 changes: 9 additions & 2 deletions src/sonic-py-common/sonic_py_common/syslogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
import socket
import sys

# customize python logging to support notice logger
logging.NOTICE = logging.INFO + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Junchao-Mellanox how did you arrive at the formula?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python logging package support customizing log level. I read the source code of it and find that:

#---------------------------------------------------------------------------
#
# Default levels and level names, these can be replaced with any positive set
# of values having corresponding names. There is a pseudo-level, NOTSET, which
# is only really there as a lower limit for user-defined levels. Handlers and
# loggers are initialized with NOTSET so that they will log all messages, even
# at user-defined levels.
#

CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

As you can see, there are 10 spare numbers between INFO and WARN, so I take INFO+1 as the NOTICE level.

logging.addLevelName(logging.NOTICE, "NOTICE")
SysLogHandler.priority_map['NOTICE'] = 'notice'


class SysLogger:
"""
SysLogger class for Python applications using SysLogHandler
"""

DEFAULT_LOG_FACILITY = SysLogHandler.LOG_USER
DEFAULT_LOG_LEVEL = SysLogHandler.LOG_NOTICE
DEFAULT_LOG_LEVEL = logging.NOTICE

def __init__(self, log_identifier=None, log_facility=DEFAULT_LOG_FACILITY, log_level=DEFAULT_LOG_LEVEL):
if log_identifier is None:
Expand Down Expand Up @@ -53,10 +59,11 @@ 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)
self.log(logging.NOTICE, 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)

Loading