More logging improvements

This commit is contained in:
Jacob Henry 2019-03-10 03:10:51 -04:00
parent 63e4f5af57
commit 173bb8ab7b
4 changed files with 58 additions and 4 deletions

View File

@ -13,6 +13,7 @@ from aiohttp import web
from slackclient import SlackClient
import hooks
import settings
import slack_util
"""
@ -172,7 +173,7 @@ class ClientWrapper(object):
# If we get a coro back, then task it up and set consumption appropriately
if coro is not None:
log.debug("Spawned task. Now {} running total.".format(len(asyncio.all_tasks())))
logging.debug("Spawned task. Now {} running total.".format(len(asyncio.all_tasks())))
yield asyncio.create_task(_exception_printing_task(coro))
if hook.consumes:
break
@ -253,7 +254,7 @@ class ClientWrapper(object):
text = "_Block message. Open slack client to view_"
# Begin constructing kwargs with fields that _must_ exist
kwargs = {"channel": channel_id, "text": text, "as_user": True}
kwargs = {"channel": channel_id, "text": text, "as_user": True, "parse": True}
# Deduce thread stuff
if thread:
@ -405,7 +406,9 @@ def get_slack() -> ClientWrapper:
Miscellania
"""
A, B, C = TypeVar("A"), TypeVar("B"), TypeVar("C")
A = TypeVar("A")
B = TypeVar("B")
C = TypeVar("C")
# Prints exceptions instead of silently dropping them in async tasks

View File

@ -11,7 +11,7 @@ import slack_util
import logging
logging.basicConfig(filename="run.log", filemode="w", level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.basicConfig(filename=settings.LOGFILE, filemode="w", level=logging.DEBUG, format='#!# %(levelname)s - %(asctime)s \n%(message)s \n', datefmt='%m/%d/%Y %I:%M:%S %p')
def main() -> None:
wrap = client.get_slack()
@ -27,6 +27,7 @@ def main() -> None:
# Add kill switch
wrap.add_hook(management_commands.reboot_hook)
wrap.add_hook(management_commands.log_hook)
# Add towel rolling
wrap.add_hook(slavestothemachine.count_work_hook)

View File

@ -2,6 +2,7 @@ from typing import Match
import hooks
import client
import settings
import slack_util
@ -13,7 +14,54 @@ async def reboot_callback(event: slack_util.Event, match: Match) -> None:
exit(0)
async def post_log_callback(event: slack_util.Event, match: Match) -> None:
# Get the last 500 lines of log of the specified severity or higher
count = 500
lines = []
# numerically rank the debug severity
severity_codex = {
"CRITICAL": 50,
"ERROR": 40,
"WARNING": 30,
"INFO": 20,
"DEBUG": 10,
"NOTSET": 0
}
curr_rating = 0
# Get the min rating if one exists
min_rating = 0
rating_str = match.group(1).upper().strip()
for severity_name, severity_value in severity_codex.items():
if severity_name in rating_str:
min_rating = severity_value
break
with open(settings.LOGFILE, 'r') as f:
for line in f:
# Update the current rating if necessary
if line[:3] == "#!#":
for k, v in severity_codex.items():
if k in line:
curr_rating = v
break
# Add the line if its severity is at or above the required minimum
if curr_rating >= min_rating:
lines.append(line)
if len(lines) > count:
del lines[0]
# Spew them out
client.get_slack().reply(event, "```" + ''.join(lines) + "```")
# Make hooks
reboot_hook = hooks.ChannelHook(reboot_callback,
patterns=r"reboot",
channel_whitelist=["#command-center"])
log_hook = hooks.ChannelHook(post_log_callback,
patterns=["post logs(.*)", "logs(.*)", "post_logs(.*)"],
channel_whitelist=["#botzone"])

View File

@ -9,3 +9,5 @@ SINGLE_THREAD_TASKS = False
# Note that this occasionally will give us warnings if SINGLE_THREAD_TAKS is False (which it usually is)
# howver, these warnings are harmless, as regardless of if aa task is awaited it still does its job
USE_ASYNC_DEBUG_MODE = False
LOGFILE = "run.log"