Logging - jetstreamc/PySimbot GitHub Wiki

Logging is a module that allow you to control the logging message in a convenient way. Sometimes, you need to show the information of the simulation or robot. The logging module let you manage the logging in a convenient way. the log could be either show in console or save to file. You can also set the logging level and enable/disable log based on that level. Sometimes, you need to print a lot of information to debug your code and show less information when you test it. Moreover, you can control the logging message format, or categorize the group of log.

By running the example2_sensors.py,

python example2_sensors.py

You will see the messages in the command window.

Screenshot of Logging

Log messages will display the information of the PySimbotApp while running.

Create your own message logs

Since PySimbot is made on top of Kivy engine, Logging system is already provided by Kivy's Logger module.

You can integrate the Logger module to the PySimbot by the below code. It is the WASD robot that reports the sensors values to Logger. The code snippet below is a part of example 2

from pysimbotlib.core import PySimbotApp, Robot
from kivy.logger import Logger
from kivy.config import Config

Config.set('kivy', 'log_level', 'info')

class ExplorerRobot(Robot):
    def update(self):
        Logger.info("Smell Angle: {0}".format(self.smell()))
        Logger.info("Distance: {0}".format(self.distance()))

if __name__ == '__main__':
    app = PySimbotApp(robot_cls=ExplorerRobot, enable_wasd_control=True)
    app.run()

In the above code, the Logger.info() command generates the message with the INFO log level. The level of the log provides the ability show or hide message based on the log level. You can control the log level by the command Config.set.

Config.set('kivy', 'log_level', 'info')

The code also generate two message categories which are Smell Angle and Distance as you can see in square brackets of the log. The category is automatically extracted from the message. Note that. The message that is passed to the function contains the comma ( : ) sign to separate the user's category and its message.

Logger.info("Smell Angle: {0}".format(self.smell()))
Logger.info("Distance: {0}".format(self.distance()))

More on Logging

You may configure the Kivy's Config to enable/disable the log, control the minimum log level, set the log file path, or the maximum messages in the log file.

For more information on Logging, see Kivy's Logger module.