<commit_before>import argparse
import logging.config

# parse args and setup logging before anything else

arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('token')
arg_parser.add_argument('--log', help='Log level', default='WARNING')
arg_parser.add_argument('--state', help='Bot state file', default='bot.json')
args = arg_parser.parse_args()

# attempt to use colorlog, if available
try:
    import colorlog
    import sys

    formatter = colorlog.ColoredFormatter(
        fmt='%(log_color)s%(asctime)s [%(name)s/%(levelname)s] %(message)s%(reset)s',
        log_colors={
            'DEBUG': 'blue',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'white,bg_red',
        }
    )

    handler = colorlog.StreamHandler(sys.stdout)
    handler.setFormatter(formatter)
    logging.root.addHandler(handler)
    logging.root.setLevel(args.log)

# otherwise just stick with basic logging
except:
    logging.basicConfig(
        level=args.log,
        format='%(asctime)s [%(name)s/%(levelname)s] %(message)s')

log = logging.getLogger(__name__)

import asyncio
import time

from cogbot.cog_bot import CogBot
from cogbot.cog_bot_state import CogBotState


# TODO Consider switching to the library rewrite: https://github.com/Rapptz/discord.py/tree/rewrite
# The rewrite has auto-reconnect behaviour: https://github.com/Rapptz/discord.py/blob/rewrite/discord/client.py#L405
# This would make our hacky crash workarounds trivial.


def _attempt_logout(loop, bot):
    try:
        log.warning('Attempting clean logout...')
        loop.run_until_complete(bot.logout())

        log.info('Gathering leftover tasks...')
        pending = asyncio.Task.all_tasks(loop=loop)
        gathered = asyncio.gather(*pending, loop=loop)

        log.info('Cancelling leftover tasks...')<commit_msg>Consolidate log format string<commit_after>import argparse
import logging.config

# parse args and setup logging before anything else

arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('token')
arg_parser.add_argument('--log', help='Log level', default='WARNING')
arg_parser.add_argument('--state', help='Bot state file', default='bot.json')
args = arg_parser.parse_args()

LOG_FMT = '%(asctime)s P#%(process)d T#%(thread)d [%(name)s/%(levelname)s] %(message)s'

# attempt to use colorlog, if available
try:
    import colorlog
    import sys

    formatter = colorlog.ColoredFormatter(
        fmt='%(log_color)s' + LOG_FMT + '%(reset)s',
        log_colors={
            'DEBUG': 'blue',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'white,bg_red',
        }
    )

    handler = colorlog.StreamHandler(sys.stdout)
    handler.setFormatter(formatter)
    logging.root.addHandler(handler)
    logging.root.setLevel(args.log)

# otherwise just stick with basic logging
except:
    logging.basicConfig(
        level=args.log,
        format=LOG_FMT)

log = logging.getLogger(__name__)

import asyncio
import time

from cogbot.cog_bot import CogBot
from cogbot.cog_bot_state import CogBotState


# TODO Consider switching to the library rewrite: https://github.com/Rapptz/discord.py/tree/rewrite
# The rewrite has auto-reconnect behaviour: https://github.com/Rapptz/discord.py/blob/rewrite/discord/client.py#L405
# This would make our hacky crash workarounds trivial.


def _attempt_logout(loop, bot):
    try:
        log.warning('Attempting clean logout...')
        loop.run_until_complete(bot.logout())

        log.info('Gathering leftover tasks...')
        pending = asyncio.Task.all_tasks(loop=loop)
        gathered = asyncio.gather(*pending, loop=loop)

        log.info('Cancelling leftover tasks...')
{'fraction_non_alphanumeric': 0.12776151184455684, 'fraction_numerical': 0.0015970188980569604, 'mean_word_length': 3.555151515151515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2428723', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1171, 'n_words': 256}