logdna / python

A python package for sending logs to LogDNA
MIT License
48 stars 35 forks source link

LogDNAHandler.emit fails with non-keyword arguments #13

Closed drdaeman closed 6 years ago

drdaeman commented 6 years ago

Hi. I've tried to add LogDNAHandler to logging configuration of some existing project and noticed that it started raise a lot of exceptions. If the record was logged with non-keyword arguments, like

logger.debug("User %s is now %s", username, status)

Then logger would fail with TypeError: tuple indices must be integers or slices, not str. Still, that's a valid and reasonably common way to use standard library logging.

This happens because:

opts = {}
if 'args' in record:
    opts = record['args']
...
if 'level' in opts:
    message['level'] = opts['level']

And record["args"] is a list, e.g.:

{
    "args": ["drdaeman", "active"],
    "message: "User drdaeman is now active",
    "msg": "User %s is now %s",
    "levelname": "DEBUG",
    "levelno": 10,
    ....
}

I know, the structured logging and keyword arguments are the way to go, but legacy logging code and quick hacks (to print some debug data quick) are reality.

Also, I'm not sure it is a good idea to use record.args at all - in standard library's LogRecord, its sole purpose is to convert from msg to message. Maybe this logic should be put behind some options["use_args"] flag.

respectus commented 6 years ago

Hi @drdaeman, thanks for reporting this issue. I wholeheartedly agree with you that your example is a valid and reasonable way to use a standard logging library. I am able to send the above debug statement properly formatted using the LogDNAHandler. Could you try my configuration? I believe there may be an issue with setting the default level to a low enough priority (DEBUG). Please do let me know if the following code does not work for you and we can further diagnose what the problem may be.

import logging
from logdna import LogDNAHandler

key = '<YOUR API KEY HERE>'
log = logging.getLogger('logdna')
# Should set level to lowest priority to send debug statements
log.setLevel(logging.DEBUG)
test = LogDNAHandler(key, { 'hostname': 'pytest1', 'index_meta': True })

log.addHandler(test)
username = "Me"
status = "working"
log.debug("User %s is now %s", username, status)
respectus commented 6 years ago

Hi @drdaeman,

I hope you got a chance to try out my suggestion above. I'm closing this issue now, since I am not able to reproduce it. I would be glad to reopen it if you continue experiencing issues and we can debug further. Thanks for keeping up with this module!