Skip to main content

🗃️ Custom Logger

The App will provide a default logger, but you can also provide your own. The default Logger instance will be set to Python's standard logging module (configured with ConsoleFormatter from the SDK). from the microsoft-teams-common package.

The Python SDK writes to the standard logging module. Configure a handler and formatter at startup:

import asyncio
import logging

from microsoft_teams.api import MessageActivity
from microsoft_teams.apps import ActivityContext, App
from microsoft_teams.common import ConsoleFormatter

# Setup logging
logging.getLogger().setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(ConsoleFormatter())
logging.getLogger().addHandler(stream_handler)
logger = logging.getLogger(__name__)

app = App()


@app.on_message
async def handle_message(ctx: ActivityContext[MessageActivity]):
logger.debug(ctx.activity)
await ctx.send(f"You said '{ctx.activity.text}'")


if __name__ == "__main__":
asyncio.run(app.start())

Log Levels

Python's standard logging levels apply: DEBUG, INFO, WARNING, ERROR, CRITICAL.

Filtering by Logger Name

Each logger is created with a name. You can filter which loggers emit output by providing a name pattern, using * as a wildcard.

Use ConsoleFilter to limit which loggers emit, matched by name with * wildcards:

from microsoft_teams.common import ConsoleFilter, ConsoleFormatter

handler = logging.StreamHandler()
handler.setFormatter(ConsoleFormatter())
handler.addFilter(ConsoleFilter("microsoft_teams*")) # only SDK loggers
logging.getLogger().addHandler(handler)

Environment Variables

The Python SDK does not read logging environment variables on its own. If you want LOG_LEVEL to control verbosity, read it yourself at startup:

import os
logging.getLogger().setLevel(os.getenv("LOG_LEVEL", "INFO").upper())

Child Loggers

[Dev] Section "child-logger" not found in Python documentation. Either mark the section explicitly as N/A for intentionally ignored, or fill in documentation.