Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Python logging handler for Microsoft Teams webhook integration with both simple and dictionary configurations.

License

Notifications You must be signed in to change notification settings

AnesFoufa/python-teams-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

teams-logger

Python logging handler for Microsoft Teams webhook integration with both simple and dictionary configurations.

This package requires Python 3.8 or newer.

Installation

pip install teams-logger

Examples

Simple configuration

import logging
from teams_logger import TeamsHandler

th = TeamsHandler(url='YOUR_WEB_HOOK_URL', level=logging.INFO)
logging.basicConfig(handlers=[th])
logging.warning('warn message')

Simple configuration and non blocking handler

import logging
from teams_logger import TeamsQueueHandler
th = TeamsQueueHandler(url='YOUR_WEB_HOOK_URL', level=logging.INFO)
logging.basicConfig(handlers=[th])
logging.info("info message")

Simple configuration and Card Formatter

import logging
from teams_logger import TeamsHandler, Office365CardFormatter

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

th = TeamsHandler(url='YOUR_WEB_HOOK_URL', level=logging.INFO)
th.setLevel(logging.DEBUG)
logger.addHandler(th)

cf = Office365CardFormatter(facts=["name", "levelname", "lineno"])
th.setFormatter(cf)
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')

try:
    2/0
except ZeroDivisionError as e:
    logger.error('Oops !', exc_info=True)

Dictionary configuration and Card Formatter

import logging
import logging.config
from teams_logger import TeamsHandler, Office365CardFormatter

url = 'YOUR_WEB_HOOK_URL'
logging_dict = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'teamscard': {
            '()': Office365CardFormatter,
            'facts': ["name", "levelname", "lineno"],
        },
    },
    'handlers': {
        'msteams': {
            'level': logging.INFO,
            'class': 'teams_logger.TeamsHandler',
            'url': url,
            'formatter': 'teamscard',
        },
    },
    'loggers': {
        __name__: {
            'handlers': ['msteams'],
            'level': logging.DEBUG,
        }
    },
}
logging.config.dictConfig(logging_dict)
logger = logging.getLogger(__name__)
logger.info('Info message')

Running tests

To run the test suite, install the package with uv and execute the test file:

uv pip install -e .
uv run python tests/test_teams_logger.py

About

Python logging handler for Microsoft Teams webhook integration with both simple and dictionary configurations.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages