This repository has been archived by the owner on Jan 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Implement LoggerAdapter to provide extended paths in the output. Improve #571 #572
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,8 +71,15 @@ def _make_unicode(self, input): | |
return input | ||
|
||
|
||
class Logging: | ||
class AtomicappLoggingAdapter(logging.LoggerAdapter): | ||
""" | ||
A class to pass contextual information to logs. | ||
""" | ||
def process(self, msg, kwargs): | ||
return('{} : {}'.format(self.extra['atomicapp_extra'], msg), kwargs) | ||
|
||
|
||
class Logging: | ||
@staticmethod | ||
def setup_logging(verbose=None, quiet=None, logtype=None): | ||
""" | ||
|
@@ -101,20 +108,20 @@ def setup_logging(verbose=None, quiet=None, logtype=None): | |
|
||
# Get the loggers and clear out the handlers (allows this function | ||
# to be ran more than once) | ||
logger = logging.getLogger(LOGGER_DEFAULT) | ||
logger.handlers = [] | ||
logger_raw = logging.getLogger(LOGGER_DEFAULT) | ||
logger_raw.handlers = [] | ||
cockpit_logger = logging.getLogger(LOGGER_COCKPIT) | ||
cockpit_logger.handlers = [] | ||
|
||
if logtype == 'none': | ||
# blank out both loggers | ||
logger.addHandler(logging.NullHandler()) | ||
logger_raw.addHandler(logging.NullHandler()) | ||
cockpit_logger.addHandler(logging.NullHandler()) | ||
return | ||
|
||
if logtype == 'cockpit': | ||
# blank out normal log messages | ||
logger.addHandler(logging.NullHandler()) | ||
logger_raw.addHandler(logging.NullHandler()) | ||
|
||
# configure cockpit logger | ||
handler = logging.StreamHandler(stream=sys.stdout) | ||
|
@@ -130,10 +137,10 @@ def setup_logging(verbose=None, quiet=None, logtype=None): | |
|
||
# configure logger for basic no color printing to stdout | ||
handler = logging.StreamHandler(stream=sys.stdout) | ||
formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(filename)s - %(message)s') | ||
formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(message)s') | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
logger.setLevel(logging_level) | ||
logger_raw.addHandler(handler) | ||
logger_raw.setLevel(logging_level) | ||
return | ||
|
||
if logtype == 'color': | ||
|
@@ -142,11 +149,26 @@ def setup_logging(verbose=None, quiet=None, logtype=None): | |
|
||
# configure logger for color printing to stdout | ||
handler = logging.StreamHandler(stream=sys.stdout) | ||
formatter = colorizeOutputFormatter('%(asctime)s - [%(levelname)s] - %(filename)s - %(message)s') | ||
formatter = colorizeOutputFormatter('%(asctime)s - [%(levelname)s] - %(message)s') | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
logger.setLevel(logging_level) | ||
logger_raw.addHandler(handler) | ||
logger_raw.setLevel(logging_level) | ||
return | ||
|
||
# If we made it here then there is an error | ||
raise Exception("Invalid logging output type: {}".format(logtype)) | ||
|
||
@staticmethod | ||
def global_logger(filename): | ||
""" | ||
This function returns a logging instance which will output logging event information | ||
along with what the LoggerAdapter tells it to output | ||
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. paraphrase, "along with LoggerAdapter output" instead of "along with what the LoggerAdapter tells it to output" |
||
:param filename: path of the file calling this function | ||
:return the function returns the logger instance which is being used by all the files | ||
""" | ||
|
||
# creating a logging instance | ||
logger_raw = logging.getLogger(LOGGER_DEFAULT) | ||
# the logging adapter handles the filename received from the file importing this | ||
logger = AtomicappLoggingAdapter(logger_raw, {'atomicapp_extra': '/'.join(filename.split('/')[-2:])}) | ||
return logger |
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
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead of "A class". Maybe "A logging adapter?" Bit more descriptive :)