This guide will teach you how to subscribe to LogManager qimessaging services, configure your listener, receive logs from NAOqi and send logs to it.
NAOqi has a unique logs system service named LogManager. You can:
The first step is to get a LogManager service using a qi.Session
import qi
import qi.logging
def main()
app = qi.Application()
app.start()
logmanager = app.session.service("LogManager")
app.run()
if __name__ == "__main__":
main()
Once you have your LogManager service, you need to get a Listener and connect the onLogMessage signal to your own callback.
Warning
You must never use qiLog or call a methods that use qiLog inside your callback. Otherwise you will have an infinite loop.
You can change some settings of your local Listener (verbosity level, filters see below).
import qi
import qi.logging
def onMessage(mess):
print mess # mess is a dictionary with all known LogMessage information.
def main():
app = qi.Application()
app.start()
logmanager = app.session.service("LogManager")
listener = logmanager.getListener()
listener.onLogMessage.connect(onMessage)
app.run()
if __name__ == "__main__":
main()
Once you have your listener you can change some verbosity settings:
listener = logmanager.getListener()
# init listener
listener.clearFilters();
# All logs with severity lower or equal to DEBUG will be received
listener.setLevel(qi.logging.DEBUG)
# receive logs with 'foo.bar' category and severity lower or equal than SILENT
# ie: "foo.bar" logs are never received.
listener.addFilter("foo.bar", qi.logging.SILENT)
# receive logs with 'foo.baz.*' category and severity lower or equal than ERROR
# globbing works fine
listener.addFilter("foo.baz.*", qi.logging.ERROR)
A log publisher is an entity you register to LogManager that sends logs to it. Then the LogManager dispatches logs coming from all publishers to all listeners having subscribed to it.
Be careful, it’s recommended to have one publisher per process. Otherwise you will send the same log as many times as you have publishers in your process.
import qi
import qi.path
import qi.logging
def main():
app = qi.Application()
app.start()
# import qicore
mod = qi.module("qicore")
# Get LogManager service
logmanager = app.session.service("LogManager")
# Create a provider
provider = mod.createObject("LogProvider", logmanager)
# Register LogProvider to LogManager
providerId = logmanager.addProvider(provider)
# Logs!!!
mylogger = qi.Logger("myfoo.bar")
mylogger.fatal("my fatal log")
mylogger.error("my error log")
mylogger.warning("my warning log")
mylogger.info("my info log")
mylogger.verbose("my verbose log")
# Remove explicitly provider from LogManager.
# It will flush remaining logs
logmanager.removeProvider(id);
if __name__ == "__main__":
main()
The provider settings are set by default to:
However, you can add/reset filters or change the verbosity with 3 methods:
# Create Logger Provider
provider = qi.createObject("LogProvider", logger);
# All logs with severity lower or equal to DEBUG will be sent
provider.setLevel(qi.logging.DEBUG)
# Send logs with 'foo.bar' category and severity lower or equal than SILENT
# ei: "foo.bar" logs are never sent.
provider.addFilter("foo.bar", qi.logging.SILENT)
# Send logs with 'foo.baz.*' category and severity lower or equal than ERROR
# globbing works fine
provider.addFilter("foo.baz.*", qi.logging.ERROR)
# The following code has the same behavior than two previous examples
# except that it resets filters.
filters = [{"foo.bar": qi.logging.SILENT}, {"foo.bar.*": qi.logging.ERROR}]
provider.setFilters(filters);