lundi 29 décembre 2014

Not getting a log from an except statement

I'm trying to catch an error in a log so I can see the data being passed. In the code below, the specific area I should be seeing the error come from is in the batch_put method where the placeholder self.logger.exception message is.


The self.logger.debug message in the adjoined try block works fine (as long as I set the init.py's level to DEBUG in my staging environment) and logs to the log as expected. But no matter what I've tried I can't seem to get any logging to happen in that except block.


The bulk of the code follows with config info:


main.py



import logging
[...]
logger = logging.getLogger()

#Flask routes making calls to module


module/__init__.py



import logging
[...]
if module.ENVIRONMENT is "production":
log_config(level=logging.WARNING)
else:
log_config(level=logging.INFO)


module/baselib.py



import logging
import logstash_formatter
[...]
def log_config (level=logging.WARNING):
"""
Logging facility.
"""

logger = logging.getLogger()

log_handler = logging.handlers.RotatingFileHandler("/var/log/module/main/main.log",
mode='a',
maxBytes=104857600,
backupCount=10)
formatter = logstash_formatter.LogstashFormatterV1()

log_handler.setFormatter(formatter)
logger.setLevel(level)
logger.addHandler(log_handler)


module/amazonlib.py



import logging
# Boto import stuff
[...]
class ddb_api(module.base):
def __init__ (self, access_key=None, secret_key=None, region=None):
"""
Initialize connection to DynamoDB v2 layer 1 (low-level API).
"""

# if no region is specified, default to US East 1.
if region is None:
region = "us-east-1"

self.handle = boto.dynamodb2.connect_to_region \
(
region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key
)

self.logger = logging.getLogger()


def batch_put (self, table, *args):
"""
Takes a list of dicts and puts their data into the given table.
Ex: module.ddb.batch_put("test_table", *batch)

@type table: str
@param table: Name of the DDB table.
@type args: list
@param args: A list of dictionaries containing DDB data to be written.
"""

attempts = 0
table = self.get_table(table)

while 1:
try:
with table.batch_write() as batch:
for count, item in enumerate(args):
for i in module.listify(item):
try:
self.logger.debug("AMAZONLIB BATCH_PUT DATA: %s" % str(i))
batch.put_item(data=i)
except:
self.logger.exception("AMAZONLIB BATCH_PUT FAILURE: %s" % str(i))

if module.sentry:
module.sentry.captureException()

return True

except boto.dynamodb2.exceptions.ProvisionedThroughputExceededException:
if attempts <= module.DDB_MAX_ATTEMPTS:
attempts += 1
else:
if module.sentry:
module.sentry.captureException()
return False

except:
if module.sentry:
module.sentry.captureException()
return False




Aucun commentaire:

Enregistrer un commentaire