fluent-logger-python icon indicating copy to clipboard operation
fluent-logger-python copied to clipboard

Feature Request: Allow relativeCreated to be formatted as seconds instead of milliseconds

Open ecerulm opened this issue 2 years ago • 1 comments

Python LogRecord has the attribute relativeCreated

Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.

It's currently possible to use FluentRecordFormatter to include that in the fluent events

version: 1
formatters:
  fluent_fmt:
    '()': fluent.handler.FluentRecordFormatter
    format:
      "host.hostname": '%(hostname)s'
      "host.id": '%(hostname)s'
      "host.uptime": '%(relativeCreated)d'
      "log.logger": '%(name)s'
      "log.level": '%(levelname)s'
      "log.origin.file.name": '%(pathname)s'
      "log.origin.file.line": '%(lineno)d'
      "log.origin.function": '%(funcName)s'

but relativeCreated is in milliseconds, it would be very convenient to be able to convert that to seconds right in FluentRecordFormatter hence this feature request, please considering allowing something like

  "host.uptime": '%(relativeCreatedInSeconds)d'

were the relativeCreatedInSeconds would be something added by FluentRecordFormatter.

ecerulm avatar Dec 14 '23 15:12 ecerulm

The workaround currently would be to add a uptime attribute to the LogRecords like this

old_factory = logging.getLogRecordFactory()

def record_factory(*args, **kwargs):
    record = old_factory(*args, **kwargs)
    record.uptime = record.relativeCreated/1000
    return record

logging.setLogRecordFactory(record_factory)

and the using that new uptime attribute in the LogRecords like this

version: 1
formatters:
  fluent_fmt:
    '()': fluent.handler.FluentRecordFormatter
    format:
      "host.uptime": '%(uptime)d'

ecerulm avatar Dec 14 '23 15:12 ecerulm