/
test_program.py
65 lines (51 loc) · 2.07 KB
/
test_program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import logging
import Queue as queue
import atexit
import sys
import SlackQueue
#Set up the root logger
logging.basicConfig(stream=sys.stdout,
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
root = logging.getLogger()
#Instantiate a queue for the handler/listener
slack_queue = queue.Queue(-1)
#Instantiate a queue handler using the queue above
SlackHandler = SlackQueue.sqHandler(slack_queue)
SlackHandler.setLevel("DEBUG")
#Set a formatter
simple = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
SlackHandler.setFormatter(simple)
#Add the queue handler to the root logger
root.addHandler(SlackHandler)
#Instantiate the queue listener with the same queue above, and also set
#the settings for communicating with Slack.
#You will need to get your own Incoming Webhook URL from Slack's Custom
#Integration setup.
SlackListener = SlackQueue.sqListener(
slack_queue,
logging_url="https://hooks.slack.com/services/<your_webhook_url>",
channel="#your_channel",
username="Penguin",
icon_emoji = ":penguin:"
)
#Start the listener
SlackListener.start()
#Register the listener stop method to run upon exit in order to allow the
#queue to be emptied before exiting.
#If you miss this step, the queue might not be emptied before the program
#exits and you won't receive all the messages on Slack.
atexit.register(SlackListener.stop)
#Begin logging
logging.info("Test info message")
#You can temporarily change the Slack settings via the "extra" parameter.
#The settings in extra will only apply to this one log message.
logging.debug("Another test message",extra= {
"channel":"@someone",
"icon_emoji":":coffee:",
"username":"Latte"
})
#You can put the <!channel> tag in the message to send an announcement.
msg="Some sort of warning!"
logging.warning("<!channel>: %s"%msg)
logging.error("Gasp!")