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
Show file tree
Hide file tree
Changes from all commits
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)

34 changes: 34 additions & 0 deletions src/sonic-py-common/tests/test_syslogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import logging
import os
import sys
from io import StringIO
from contextlib import redirect_stdout

if sys.version_info.major == 3:
from unittest import mock
else:
import mock

modules_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(modules_path, 'sonic_py_common'))
from sonic_py_common import syslogger


class TestSysLogger:
def test_notice_log(self, capsys):
"""NOTICE is a customize log level, added a test case here to make sure it works in future
"""
log = syslogger.SysLogger()
# remove syslog handler, unit test environment has no rsyslogd
for handler in log.logger.handlers[:]:
log.logger.removeHandler(handler)

# put a stdout log handler
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(name)s[%(process)d] - %(levelname)s : %(message)s')
handler.setFormatter(formatter)
log.logger.addHandler(handler)

log.log_notice('this is a message')
captured = capsys.readouterr()
assert 'NOTICE' in captured.out
Loading