Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ecerulm opened this issue Dec 14, 2023 · 1 comment

Comments

@ecerulm
Copy link

ecerulm commented Dec 14, 2023

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
Copy link
Author

ecerulm commented Dec 14, 2023

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant