# SoftBank Robotics documentation What's new in NAOqi 2.8?

# Python - How to receive and send logs

## Introduction

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:

- use a listener to Subscribe to LogManager to obtain logs,
- Add a publisher to send logs to dispatch logs to all clients listeners subscribed to LogManager.

## Prerequisites

- An installed python NAOqi SDK for your operating system.
- Read guide python: Writing a client

## Subscribe to LogManager to obtain logs

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:

- clearFilters() will reset/initialize to default all changes made to the listener,
- addFilter(category, level) changes the verbosity for the specific category,
- setLevel(level) changes the verbosity for all categories (setLevel(level) == addFilter(“*”, level))

    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)

Note that if you connect your listener with onLogMessageWithBacklog, your filter settings will only apply to the messages that are received after you set the filter, and not to the messages already in the backlog.

## Add a publisher to send logs

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.

## Example

    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:

- verbosity: qi.logging.INFO
- filters: “qitype.*” to qi.logging.SILENT, “qimessaging.*” to qi.logging.SILENT, “qi.*” to qi.logging.SILENT. Those filters are NEEDED to avoid infinite recursion. You cannot change those filters.

However, you can add/reset filters or change the verbosity with 3 methods:

- addFilter(category, level) changes the verbosity for the specific category,
- setLevel(level) changes the verbosity for all categories (setLevel(level) == addFilter(“*”, level))
- setFilters(filters) will set categories’ filters to filters. Filters is a list of pair containing filtering rules (ie: pair(“filter”, level))

## Example

    # 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);

Glossary Site map Index Support Contact Legal Notices