-
Notifications
You must be signed in to change notification settings - Fork 664
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
[generic_config_updater] Logging #1864
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
import jsonpatch | ||
from jsonpointer import JsonPointer | ||
import sonic_yang | ||
import syslog | ||
import subprocess | ||
import yang as ly | ||
import copy | ||
import re | ||
from enum import Enum | ||
|
||
YANG_DIR = "/usr/local/yang-models" | ||
SYSLOG_IDENTIFIER = "GenericConfigUpdater" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is how it look like:
I think it is OK, this way I can grep all syslogs associated with I think we will add more logs to other parts such config replace, rollback checkpoint and the applier. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I don't think we should use |
||
|
||
class GenericConfigUpdaterError(Exception): | ||
pass | ||
|
@@ -691,3 +693,44 @@ def _get_model(self, model, name): | |
return submodel | ||
|
||
return None | ||
|
||
class GenericUpdaterLogger: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class seems have general value, like https://github.com/Azure/sonic-buildimage/blob/master/src/sonic-py-common/sonic_py_common/logger.py. Is it better to enhance existing Logger class? If something is impossible, we can inherit the class. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. used sonic-py-common logger |
||
""" | ||
Logs are printed to console and added to syslog. | ||
Console settings: log levels [error, warning, info] always printed, [debug] only printed if verbose logging | ||
Syslog settings: log all levels [error, warning, info, debug] | ||
""" | ||
def init_verbose_logging(self, verbose): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. used loggingSettings instead and the logger is created from |
||
# Usually logs have levels such as: error, warning, info, debug. | ||
# By default all log levels should show up to the user, except debug. | ||
# By allowing verbose logging, debug msgs will also be shown to the user. | ||
self.enable_verbose_logging = verbose | ||
|
||
def log_error(self, title, msg): | ||
self.log(title, msg, syslog.LOG_ERR) | ||
|
||
def log_warning(self, title, msg): | ||
self.log(title, msg, syslog.LOG_WARNING) | ||
|
||
def log_info(self, title, msg): | ||
self.log(title, msg, syslog.LOG_INFO) | ||
|
||
def log_debug(self, title, msg): | ||
self.log(title, msg, syslog.LOG_DEBUG) | ||
|
||
def log(self, title, msg, logLevel): | ||
combined_msg = f"{title}: {msg}" | ||
self._log_to_console(combined_msg, logLevel) | ||
self._log_to_syslog(combined_msg, logLevel) | ||
|
||
def _log_to_console(self, msg, logLevel): | ||
# Always log [warning, error, info] i.e. not debug, but if verbose logging print debug as well | ||
if logLevel < syslog.LOG_DEBUG or self.enable_verbose_logging: | ||
print(msg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. used common logger logic |
||
|
||
def _log_to_syslog(self, msg, logLevel): | ||
syslog.openlog(SYSLOG_IDENTIFIER) | ||
syslog.syslog(logLevel, msg) | ||
syslog.closelog() | ||
|
||
logger = GenericUpdaterLogger() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is to have a single global Another idea is to create an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. global loggingSettings and then each logger instance is created per class, there is no global logger There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @qiluo-msft I have used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You add the same title everywhere. Is it better to define an instance in this class, and init with the title. #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way makes it easier to add new log msgs, I don't have to create a new instance of logger for each class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm, created a logger instance for the PatchApplier class