Kivy - Logger



Python's standard library includes logging module that helps in implementing a robust event logging system in Python applications. The logging mechanism of Kivy builds upon it with certain additional features such as color-coded output on supported terminals, message categorization etc.

Each time you run a Kivy application, you get to see a log being displayed in the console window. It appears like this −

[INFO  ] [Logger  ] Record log in C:\Users\user\.kivy\logs\kivy_23-07-10_67.txt
[INFO  ] [deps    ] Successfully imported "kivy_deps.gstreamer" 0.3.3
[INFO  ] [deps    ] Successfully imported "kivy_deps.angle" 0.3.3
[INFO  ] [deps    ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO  ] [deps    ] Successfully imported "kivy_deps.sdl2" 0.6.0
[INFO  ] [Kivy    ] v2.2.0
[INFO  ] [Kivy    ] Installed at "c:\kivyenv\Lib\sitepackages\kivy\__init__.py"
[INFO  ] [Python  ] v3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)]
[INFO  ] [Python  ] Interpreter at "c:\kivyenv\Scripts\python.exe"
[INFO  ] [Logger  ] Purge log fired. Processing...
[INFO  ] [Logger  ] Purge finished!
[INFO  ] [Factory ] 190 symbols loaded
[INFO  ] [Image   ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO  ] [Window  ] Provider: sdl2
[INFO  ] [GL      ] Using the "OpenGL" graphics system
[INFO  ] [GL      ] GLEW initialization succeeded
[INFO  ] [GL      ] Backend used <glew>
[INFO  ] [GL      ] OpenGL version <b'4.6.0 - Build 31.0.101.3959'>
[INFO  ] [GL      ] OpenGL vendor <b'Intel'>
[INFO  ] [GL      ] OpenGL renderer <b'Intel(R) Iris(R) Xe Graphics'>
[INFO  ] [GL      ] OpenGL parsed version: 4, 6
[INFO  ] [GL      ] Shading version <b'4.60 - Build 31.0.101.3959'>
[INFO  ] [GL      ] Texture max size <16384>
[INFO  ] [GL      ] Texture max units <32>
[INFO  ] [Window  ] auto add sdl2 input provider
[INFO  ] [Window  ] virtual keyboard not allowed, single mode, not docked
[INFO  ] [Text    ] Provider: sdl2
[INFO  ] [Base    ] Start application main loop
[INFO  ] [GL      ] NPOT texture support is available

These messages tell you which hardware and drivers have been detected and initialized, and which could not be done so.

The Logger class in Kivy library provides a singleton instance, and is defined in kivy.logger module. In addition to the logging levels in Python's logging module (debug, info, warning, error and critical), Kivy has an additional trace level.

The Logger object has the methods corresponding to the above logging levels. Each of these methods accept a string argument - message - split in two parts separated by colon ( : ) symbol. The first part is used as a title and the string after colon is the logging message.

from kivy.logger import Logger

Logger.info('title: This is a info message.')
Logger.debug('title: This is a debug message.')

The default logging level is set to info in Kivy's configuration file. Hence, you get to see such a long logging output. You can set it to any desired logging level by setlevel() method of the Logger object.

from kivy.logger import Logger, LOG_LEVELS

Logger.setLevel(LOG_LEVELS["debug"])

Kivy's logging mechanism is controlled by an environment variable KIVY_LOG_MODE with three possible values: KIVY, PYTHON, MIXED.

The default KIVY_LOG_MODE is KIVY, because of which all log messages in the system are output to the Kivy log files and to the console.

If you set it to PYTHON mode, no handlers are added, and sys.stderr output is not captured. It is left to the client to add appropriate handlers.

In MIXED mode, handlers are added to the Kivy's Logger object directly, and propagation is turned off. sys.stderr is not redirected.

All the logging related configuration parameters are found in the [Kivy] section of the config.ini file. The parameters and their default values are −

log_dir = logs
log_enable = 1
log_level = info
log_name = kivy_%y-%m-%d_%_.txt
log_maxfiles = 100

Note that, you can access to the last 100 LogRecords even if the logger is not enabled.

from kivy.logger import LoggerHistory
print(LoggerHistory.history)
Advertisements