Kivy Logger with Timestamp?

1,320 views
Skip to first unread message

Carsten Thielepape

unread,
May 24, 2014, 3:16:22 PM5/24/14
to kivy-...@googlegroups.com
Hallo,

any advice how to add a timestamp to the kivy logger?

Bruce Cropley

unread,
May 25, 2014, 5:54:07 AM5/25/14
to kivy-...@googlegroups.com
I've just spent a couple of hours investigating this myself, and was about to post here anyway :)

#Before importing kivy:

import logging
import sys

formatter
= logging.Formatter("[%(asctime)s.%(msecs)03d][%(levelname)s][%(message)s]", "%H:%M:%S")
console
= logging.StreamHandler()
console
.setFormatter(formatter)
sys
._kivy_logging_handler = console

This works for me, but it doesn't do the colour formatting of the default logger, or separate the kivy sections. I've included milliseconds, but not the date.
Have a look at the kivy logger source code and the python logging module if you want more detail.

Enjoy,
Bruce

Carsten Thielepape

unread,
May 25, 2014, 11:39:08 AM5/25/14
to kivy-...@googlegroups.com
Hallo,

great advice: Thanks, working fine for me!

Carsten Thielepape

unread,
May 25, 2014, 12:29:42 PM5/25/14
to kivy-...@googlegroups.com
Update: Is not adding the timestamp to the logfile..

Bruce Cropley

unread,
May 25, 2014, 7:57:28 PM5/25/14
to kivy-...@googlegroups.com
That's because it is just using a StreamHandler.
You will need to do some mixing and matching.
Here's a sample of code that uses a FileHandler:
(from https://docs.python.org/2/howto/logging-cookbook.html#logging-cookbook)

import logging
import auxiliary_module

# create logger with 'spam_application'
logger = logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

Carsten Thielepape

unread,
May 28, 2014, 3:42:53 AM5/28/14
to kivy-...@googlegroups.com
Hallo,
I like to post the solution I am using.

This patches the kivy.logger, so a timestamp is added to all logger (console & Filehandler). I added this into a separate modul, and you just need to import the module, and the timestamp is added to all log messages.
I would preferred to use the formatter module, but it crashes with bad formatted message strings, and it seems , that no exception  can't be cathed.

from kivy.logger  import Logger
import logging
import time


class LoggerPatch():
   
   
def __init__(self):
       
self.emit_org = None
       
       
# we create a formatter object once to avoid
       
# inialisation on every log line
       
self.oFormatter=logging.Formatter(None)

       
# we just need to patch the first Handler
       
# as we change the message itself
        oHandler
= Logger.handlers[0]
       
self.emit_org=oHandler.emit
        oHandler
.emit=self.emit


   
def emit(self,record):
       
# we do not use the formatter by purpose as it runs on failure
       
# if the message string contains format characters

        ct
= self.oFormatter.converter(record.created)
        t
= time.strftime("%Y-%m-%d %H|%M|%S", ct)
        s
= "%s.%03d: " % (t, record.msecs)
   
        record
.msg= s +record.msg
       
self.emit_org(record)


oLoggerPatch
=LoggerPatch()




John Buckley

unread,
Jan 31, 2019, 8:13:10 AM1/31/19
to Kivy users support
I know this is quite old. I just want to add that this was been very helpful for me.

Thanks

alessandro bonvicini

unread,
Apr 19, 2019, 4:52:35 PM4/19/19
to Kivy users support
for me tooo
thanks

David Matthews

unread,
Aug 18, 2022, 9:24:04 PM8/18/22
to Kivy users support
Coming to this nearly a decade later, I wanted to share what might be a simpler solution that preserves console colors and such while putting the timestamp in both the log file and the console.

import logging
from kivy.logger import Logger, ColoredFormatter

# Set millisecond format to use a decimal because I'm in the US
logging.Formatter.default_msec_format = '%s.%03d'
# Add timestamp to log file
Logger.handlers[1].setFormatter(
    logging.Formatter('%(asctime)s %(message)s'))
# Add timestampt to console output
Logger.handlers[2].setFormatter(
    ColoredFormatter('[%(levelname)-18s] %(asctime)s %(message)s'))


Reply all
Reply to author
Forward
0 new messages