Skip to content

EQWorks/slack-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slack-handler

Slack ™️ handler for the standard Python logging facility.

Installation

% pipenv install git+https://github.com/EQWorks/slack-handler.git@master#egg=slack-handler
% python -m pip install git+https://github.com/EQWorks/slack-handler.git@master#egg=slack-handler

Depending on the environment setup, one may need to perform python3 -m pip for the pip installation option.

Usage

from logging import getLogger

from slack_handler import SlackHandler

logger = getLogger(__name__)
sh = SlackHandler(
    # required identifier
    title='my awesome app',
    # default to environment variable
    # os.getenv('SLACK_WEBHOOK')
    hook='SLACK_WEBHOOK',
    # optional channel ID to override webhook's default channel
    channel='SLACK_CHANNEL_ID',
    # optional color function, see Customizations section
    get_color='color_function',
)
logger.addHandler(sh)

try:
    1 / 0
except Exception as e:
    logger.exception(e)

try:
    1 + {}
except Exception as e:
    logger.warning(e)

ss

Customizations

logging level dependent colors.

import logging

def custom_colors(levelno: int):
    if levelno > logging.WARNING:
        return '#f00'  # Slack message attachments supported color format

    # any number of different colors based on logging levels

    return '#ccc'  # default color


sh = SlackHandler(
    title='my awesome app with custom colors',
    get_color=custom_colors,
)

About

Slack ™️ handler for the standard Python logging facility.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages