Last active
July 17, 2024 17:40
-
-
Save duckythescientist/a3ea6a084a68280f94c373ac20cdfd4e to your computer and use it in GitHub Desktop.
Is your Python logging not UwU enough for you? Well, do I have a solution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Notices your logging, OwO what's this? | |
""" | |
import logging | |
def configure_ducklogging(): | |
rootlog = logging.getLogger() | |
log_format = "%(asctime)s | %(name)s %(levelname)s %(module)s:%(funcName)s:%(lineno)d [%(process)d] | %(message)s" | |
log_dateformat = "%Y-%m-%dT%H:%M:%S" # ISO 8601 | |
import os | |
try: | |
import coloredlogs | |
if os.name == "nt": | |
# Windows need colorama initialized to properly use coloredlogs | |
import colorama | |
colorama.init(convert=True) | |
except ImportError: | |
logging.basicConfig( | |
level=logging.DEBUG, format=log_format, datefmt=log_dateformat | |
) | |
else: | |
coloredlogs.install( | |
level=logging.DEBUG, | |
datefmt=log_dateformat, | |
field_styles={ | |
"asctime": {"color": "green"}, | |
"levelname": {"color": "blue", "bold": True}, | |
"module": {"color": "yellow"}, | |
"process": {"color": "magenta"}, | |
}, | |
fmt=log_format, | |
) | |
# rootlog.setLevel(logging.DEBUG) | |
rootlog.setLevel(0) | |
# I think Parso is being loud because of my iPython | |
noisy_log = logging.getLogger("parso") | |
noisy_log.setLevel(logging.WARNING) | |
noisy_log = logging.getLogger("matplotlib") | |
noisy_log.setLevel(logging.WARNING) | |
noisy_log = logging.getLogger("asyncio") | |
noisy_log.setLevel(logging.WARNING) | |
def uwu(shortlevelnames=False): | |
import os | |
import re | |
import logging | |
# https://stackoverflow.com/a/24894475 | |
def replace_keep_case(word, replacement, text): | |
def func(match): | |
g = match.group() | |
if g.islower(): | |
return replacement.lower() | |
if g.istitle(): | |
return replacement.title() | |
if g.isupper(): | |
return replacement.upper() | |
return replacement | |
return re.sub(word, func, text, flags=re.I) | |
def owo_filter(record): | |
pairs = ( | |
("small", "smol"), | |
("cute", "kawaii"), | |
("fluff", "floof"), | |
("love", "luv"), | |
("stupid", "baka"), | |
("what", "nani"), | |
("meow", "nya"), | |
("your", "ur"), | |
("you're", "ur"), | |
("fuck", "fwickk"), | |
("hell", "hekk"), | |
("my", "mwy"), | |
("to", "tuwu"), | |
("had", "hawd"), | |
("you", "yuw"), | |
("go", "gow"), | |
("and", "awnd"), | |
("have", "haw"), | |
("debug", "debuwug"), | |
("info", "infowo"), | |
) | |
text = record.msg | |
for a, b in pairs: | |
text = replace_keep_case(a, b, text) | |
text = re.sub(r"l[el](s?)\b", r"w\1", text) | |
text = re.sub(r"[LR]", "W", text) | |
text = re.sub(r"[lr]", "w", text) | |
text = re.sub(r"([MmNn])([Oo])", r"\1y\2", text) | |
record.msg = text | |
return True | |
rootlog = logging.getLogger() | |
if shortlevelnames: | |
logging.addLevelName(logging.CRITICAL, "ΘOΘ") | |
logging.addLevelName(logging.ERROR, "X.X") | |
logging.addLevelName(logging.WARNING, "ÒwÓ") | |
logging.addLevelName(logging.INFO, "o.O") | |
logging.addLevelName(logging.DEBUG, "UwU") | |
else: | |
logging.addLevelName(logging.CRITICAL, "ΘOΘ CWITICAL") | |
logging.addLevelName(logging.ERROR, "X.X EWWOR") | |
logging.addLevelName(logging.WARNING, "ÒwÓ WAWNING") | |
logging.addLevelName(logging.INFO, "o.O INFOwO") | |
logging.addLevelName(logging.DEBUG, "UwU DEBUwUG") | |
for handler in rootlog.handlers: | |
handler.addFilter(owo_filter) | |
if __name__ == "__main__": | |
# print(text) | |
configure_ducklogging() | |
uwu() | |
logger = logging.getLogger(__name__) | |
# logging_tree.printout() | |
logger.debug("This text is debug") | |
logger.info("This text is info") | |
logger.warning("This text is a warning") | |
logger.error("Something went really wrong") | |
logger.critical("Catastrophic failure, quitting") | |
logger.info("Sparkle sparkles sparkling") | |
logger.warning("Ths program is the most important piece of software that I have ever written") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.