severb / graypy

Python logging handler for Graylog that sends messages in GELF (Graylog Extended Log Format).
https://www.graylog.org/
BSD 3-Clause "New" or "Revised" License
260 stars 91 forks source link

Feature/dynamic version number #54

Closed chrcoe closed 7 years ago

chrcoe commented 8 years ago

This feature allows for users to change the version number at instantiation time. I was unable to override the version number using Filters or Adapters. This solution allows users to override the version number very easily. Before this change, the version is hardcoded and difficult to override unless I am missing something. I made the default value still be "1.0" as you had previously.

severb commented 7 years ago

Hey @chrcoe, I'm not sure I understand where this is used/needed.

chrcoe commented 7 years ago

@severb in a project I was using graypy for, we needed to log the version number that was being run from specific servers. Ideally the code would be the same version on every server but this was not possible in that environment at that time so I needed to be able to set the version number at instantiation time. Without this change, the version number was always coming across as 1.0 regardless of the code version I was trying to push through. I wrestled with filters and adapters for a long time to override the version number and was only able to do it by editing this code accordingly.

tusharmakkar08 commented 7 years ago

Hey @chrcoe

I think this can be done via normal logging filters. You can use threadlocal while setting up the filters.

class ContextFilter(logging.Filter):
    def __init__(self):
        super(ContextFilter, self).__init__()
        self.thread_local = threading.local()

    def set_version(self, version):
        self.thread_local.version = version

    def filter(self, record):
        try:
            record.version = self.thread_local.version
        except AttributeError:
            record.version = "1.0"
        return True

Let me know if this suits your requirement. I am closing this PR for now.

chrcoe commented 7 years ago

@tusharmakkar08 thanks for this info, I will give it a shot the next time I need to do this. The project I was using this for ended up using a customized version of graypy to allow the version number change. I have since moved on so I will not be able to test it in that environment any longer.

tusharmakkar08 commented 7 years ago

Ohh ok.. cool 👍