Skip to content

Instantly share code, notes, and snippets.

@lukpazera
Last active August 29, 2015 13:58
Show Gist options
  • Save lukpazera/10017005 to your computer and use it in GitHub Desktop.
Save lukpazera/10017005 to your computer and use it in GitHub Desktop.
An example of a simple class that allows for writing messages into desired log.
# python
""" Log allows for writing messages into choosen log subsystem.
Messages can have simple hierarchical format:
- Head Message,
- Child Message 1,
- Child Message 2,
- Child Message 3,
- etc.
"""
import lx
class Log:
""" Provide an easy way for logging messages into choosen log subsystem.
"""
def __init__ (self):
""" To log messages we need 2 things:
- Log service which will provide us with desired log subsystem object,
- lx.object.Log() interface that will give us access to a choosen log subsystem,
"""
self.log_service = lx.service.Log()
self.log = lx.object.Log()
def set (self, log_name):
""" This is a method equivalent to set() method that you can find on most (if not all)
Nexus interfaces and it sets our Log class instance to a desired log subsystem based on subsystem name.
This method has to be called before we can use the Log class instance.
NOTE: I use set() method in my classes to keep consistency with the way Nexus SDK works
but obviously you don't need set() and can initialize the log subsystem in other way
(directly in __init__() for example).
"""
try:
# To add entires to log we need lx.object.Log() interface that points to choosen subsystem.
# This can be obtained with SubSystemLookup() method of the Log service.
# The log subsystem is looked up by its name (other Log service methods allow for accessing
# log subsystems by index).
# If log subsystem is not found LookupError exception is raised in which case we return False.
self.log = lx.object.Log(self.log_service.SubSystemLookup(log_name))
except LookupError:
return False
else:
return True
def Out (self, message_type, head_message, child_messages=None):
""" Output a message comprising of:
- one head message,
- a set of child messages passed as a list.
Arguments:
- message_type --- one of message type symbols such as lx.symbol.e_INFO, e_WARNING or e_FAILED.
- head_message --- main message string.
- child_messages --- list of string messages that will be printed under the head one.
"""
if not self.log.test():
return False
# Messages are added to Event Log in the form of LogEntry objects.
# So first we need to create LogEntry object that will contain the head message.
# To do that we use CreateEntryMessage() method from the Log service. It takes message type
# symbol (as described above) and message string.
# It returns LogEntry object that is ready to be added to the log.
head_entry = lx.object.LogEntry(self.log_service.CreateEntryMessage(message_type, head_message))
# However, if we want to add some child entries we need to do it first,
# before posting head entry. Child messages won't be shown if you add them
# to head entry after head entry was already posted.
# Basically you need to have a main entry and all its children entries set up before
# adding main entry to log.
if not child_messages:
child_messages = []
if not isinstance(child_messages, list):
child_messages = [child_messages]
# To add child entries we walk the child_messages list and for each child message:
# - we create new entry using CreateEntryMessage() method.
# - we add child entry to the head entry using AddEntry() method of the lx.object.LogEntry() interface.
for child_message in child_messages:
child_entry = lx.object.LogEntry(self.log_service.CreateEntryMessage(message_type, child_message))
head_entry.AddEntry(child_entry)
# Such set up head entry is ready to be posted in the log.
# This is done by calling AddEntry() method of our log interface.
self.log.AddEntry(head_entry)
# To use the class and print to the 'script' log we need to do following:
log = Log()
if log.set('script'):
log.Out(lx.symbol.e_INFO, 'Head Message', ['Child Message 1', 'Child Message 2'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment