music-kraken-core/src/music_kraken/__init__.py

64 lines
1.7 KiB
Python
Raw Normal View History

2022-11-23 07:24:05 +00:00
import logging
2023-04-15 15:17:33 +00:00
import gc
2023-10-12 17:24:35 +00:00
import sys
2023-04-15 15:17:33 +00:00
2023-09-13 16:55:04 +00:00
from .utils.shared import DEBUG, DEBUG_LOGGIN
2023-09-10 14:27:09 +00:00
from .utils.config import logging_settings, main_settings, read_config
read_config()
from . import cli
2023-04-18 07:02:03 +00:00
2023-10-24 15:41:42 +00:00
if True:
import sys
sys.setrecursionlimit(100)
2023-09-10 14:27:09 +00:00
2024-01-16 09:37:22 +00:00
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = logging_settings['logging_format']
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(CustomFormatter())
2023-09-10 14:27:09 +00:00
# configure logger default
logging.basicConfig(
2023-09-13 16:55:04 +00:00
level=logging_settings['log_level'] if not DEBUG_LOGGIN else logging.DEBUG,
2023-09-10 14:27:09 +00:00
format=logging_settings['logging_format'],
handlers=[
logging.FileHandler(main_settings['log_file']),
2024-01-16 09:37:22 +00:00
stream_handler
2023-09-10 14:27:09 +00:00
]
)
if main_settings['modify_gc']:
2023-04-04 20:07:56 +00:00
"""
At the start I modify the garbage collector to run a bit fewer times.
This should increase speed:
https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/
"""
# Clean up what might be garbage so far.
gc.collect(2)
allocs, gen1, gen2 = gc.get_threshold()
allocs = 50_000 # Start the GC sequence every 50K not 700 allocations.
gen1 = gen1 * 2
gen2 = gen2 * 2
gc.set_threshold(allocs, gen1, gen2)