Using Logstash to record logs and also record when a user does something. Unfortunately, it seems to send multiple entries per 'action' or error instead of one per action or error. For example, if a user submits a form, I have it set to log that they've submitted. Sometimes it will record 1 log entry for that, sometimes it will record 5, or 3, or 2. It's incredibly inconsistent. Other tools and applications that log to the same server don't do this. This is where I set up logstash:
class LogStashExp(object):
@staticmethod
def log(exc, request, servfrom, erlevel, email):
if str(request.url).startswith('http://localhost'):
host = 'localhost'
else:
host = '*******'
prlogger = logging.getLogger(str(servfrom))
prlogger.setLevel(logging.INFO)
prlogger.addHandler(logstash.LogstashHandler(host, 6123, version=1))
extra = {
'email': email
}
if erlevel == "warn":
prlogger.warning(exc, extra=extra)
elif erlevel == "error":
prlogger.error(exc, extra=extra)
elif erlevel == "info":
prlogger.info(exc, extra=extra)
else:
prlogger.error(exc)
And I use this where I want to log something.
LogStashExp.log("message", request, servfrom='application', erlevel='info', email=email)
Ideas?
Python logging
has a global state.
prlogger = logging.getLogger(str(servfrom))
prlogger.setLevel(logging.INFO)
prlogger.addHandler(logstash.LogstashHandler(host, 6123, version=1))
First time this code is executed, it creates a logger. The second time this code is executed, it returns the same logger back. And each time the code is executed a new logging handler is added to the existing logger.
I think if you want to create your logger dynamically, you should check if it has any handlers before proceeding to add new ones.