community icon indicating copy to clipboard operation
community copied to clipboard

Add date/time to logging formatter

Open Cheaterman opened this issue 1 year ago • 2 comments
trafficstars

Is your feature request related to a problem? Please describe. I sometimes want a clear idea of how long it took for a problem to happen in an application, or generally other time-related info for stuff that's in the logs, but Kivy doesn't provide that by default.

Describe the solution you'd like Add date/time to Kivy logs - at the very least in log files (it's OK to omit them for stdout logging IMHO).

Describe alternatives you've considered I think we have some config options for that, but it's a bit cumbersome (and easy to forget) to change it for all apps when this really (again IMHO) should be the default.

Additional context Most frameworks come with a configuration that gives date/time in logs out of the box, it's very convenient when trying to debug issues, particularly in stateful frontend code that might have a timing component (animations, http requests, etc etc).

Thanks in advance, and I hope this isn't a duplicate but it's very hard to check because a lot of issues mention logging (obviously, since we tell people to provide logs hehe).

Cheaterman avatar Jun 10 '24 15:06 Cheaterman

Is there a way to do this until this is implemented?

jporta09 avatar Sep 17 '24 18:09 jporta09

@jporta09 Yes you can add the date and time...

The kivy logger is a configured python logger. The python logger is very flexible. The first way I thought to do it is to add a filter that modified each record and prepends the date. The filter in the logger is designed to use more complex logic that the levels to select the messages that gets logged. It allows the log record to be modified. The logic of the kivy handler requires the date is in the "third" position, as below:

[INFO   ] [Text        ][2024-09-17 19:48:03.983331] Provider: sdl2
[INFO   ] [Base        ][2024-09-17 19:48:03.986391] Start application main loop
[INFO   ] [GL          ][2024-09-17 19:48:03.988387] NPOT texture support is available

Here is an example that adds a filter to the kivy Logger that adds the date and time:

from kivy.app import App
from kivy.lang import Builder
from kivy.logger import Logger
import logging
from datetime import datetime

class AddDateFilter(logging.Filter):
    def filter(self, record):
        colon_pos = record.msg.find(':')
        if colon_pos > 0:
            record.msg = record.msg[:colon_pos + 1] + f' [{datetime.now()}]' + record.msg[colon_pos + 1:]
        else:
            record.msg = 'Application:' + f' [{datetime.now()}] ' + record.msg
        return True

Logger.addFilter(AddDateFilter())


kv = """
AnchorLayout:
    Button:
        size_hint: None, None
        size: dp(200), dp(48)
        text: 'Press me'
        on_release: app.log('Application: Button Pressed')
"""

class DateLogApp(App):
    def build(self):
        return Builder.load_string(kv)

    def log(self, msg):
        Logger.info(msg)


DateLogApp().run()

The formatting of the text with a colon happens after the filtering, so there is some special handing to adjust for this. If the date needs to be first you could vendor in some of the code from Kivy logger.py file, and modify the formatting and update the ConsoleHandler.

ElliotGarbus avatar Sep 18 '24 03:09 ElliotGarbus