Skip to content

Commit

Permalink
Merge pull request googleapis#91 from joh06937/logging_handler
Browse files Browse the repository at this point in the history
Sorry for the delay on merging this. I thought I had posted a comment but it turns out I hadn't.
  • Loading branch information
Narcolapser authored May 2, 2018
2 parents fa28547 + 04d3d9a commit 06eff41
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
75 changes: 75 additions & 0 deletions O365/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import logging
import json
import copy
import O365

log = logging.getLogger(__name__)

class O365Handler(logging.Handler):
'''
Logging handler for sending O365 emails
'''

def __init__(self, *args, **kwargs):
'''
Creates a new logging handler for sending O365 messages
The parameters for initialization are the same for O365.Message.
If the handler is initialized with a JSON parameter, that will be used
as a default message structure.
If a new record has a message that can be deserialized by JSON.loads(),
its message will be used to create a new O365 message. If the message
can't be deserialized, the message will be appended to the body of the
handler's default O365 message.
If a new record is received and the handler was initialized with a
default JSON format, the record's message will be appended to the end
of the default messsage's body prior to being sent.
'''

super(O365Handler, self).__init__()

self._defaultMessage = O365.Message(*args, **kwargs)

def emit(self, record):
'''
Handles emitting a record
Arguments
record -- the record to handle
'''

# We'll need to try to use our default message to send this, so start
# by making a copy
message = copy.deepcopy(self._defaultMessage)

# If we can deserialize this record's message, use that to try to send
# the message
try:
message.json = json.loads(record.getMessage())
message.sendMessage()
return
except Exception as e:
pass

# There could be a few things missing from the various dictionaries,
# and the dictionary layout might change, so just be cheap and wrap
# this in a dictionary try/catch.
try:
defaultMessageBody = self._defaultMessage.getBody()

# We already have a body, so append to the end
message.setBody("{}{}".format(defaultMessageBody, record.getMessage()))

except KeyError:
# We don't have a body yet, so just set the record's message as the
# body
message.setBody(record.getMessage())

# Send!
try:
message.sendMessage()
except Exception as e:
log.info('Could not send message: {}'.format(str(e)))
4 changes: 2 additions & 2 deletions O365/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Message(object):
Management of the process of sending, receiving, reading, and editing emails.
Note: the get and set methods are technically superflous. You can get more through control over
a message you are trying to craft throught he use of editing the message.json, but these
a message you are trying to craft through the use of editing the message.json, but these
methods provide an easy way if you don't need all the power and would like the ease.
Methods:
Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(self, json=None, auth=None, verify=True):
self.hasAttachments = json['HasAttachments']

else:
self.json = {'Message': {'Body': {}},
self.json = {'Body': {},
'ToRecipients': [], 'CcRecipients': [], 'BccRecipients': []}
self.hasAttachments = False

Expand Down

0 comments on commit 06eff41

Please sign in to comment.