def config_logger(*logger_names, log_dir=BASE_LOG_FOLDER): """ Used to allow isolated parts of this project to easily change the log output folder, e.g. allow Django management commands to change the logs folder to ``crons/`` Currently only used by :class:`payments.management.CronLoggerMixin` Usage: >>> config_logger('someapp', 'otherlogger', 'mylogger', log_dir='/full/path/to/log/folder') :param str logger_names: List of logger names to replace logging config for (see LOGGER_NAMES) :param str log_dir: Fully qualified path. Set each logger's timed_file log directory to this :return: :class:`logging.Logger` instance of BASE_LOGGER """ _lh = LogHelper(BASE_LOGGER, formatter=LOG_FORMATTER, handler_level=logging.DEBUG) _lh.log.handlers.clear() # Force reset the handlers on the base logger to avoid double/triple logging. _lh.add_console_handler(level=CONSOLE_LOG_LEVEL) # Log to console with CONSOLE_LOG_LEVEL _dbg_log = os.path.join(log_dir, 'debug.log') _err_log = os.path.join(log_dir, 'error.log') _lh.add_timed_file_handler(_dbg_log, when='D', interval=1, backups=14, level=DBGFILE_LEVEL) _lh.add_timed_file_handler(_err_log, when='D', interval=1, backups=14, level=ERRFILE_LEVEL) l = _lh.get_logger() # Use the same logging configuration for all privex modules _lh.copy_logger(*logger_names) return l
from privex.db import SqliteWrapper, BaseQueryBuilder, SqliteQueryBuilder, QueryMode from privex.db import _setup_logging from privex.db.sqlite import SqliteAsyncWrapper try: dotenv.read_dotenv() except AttributeError: dotenv.load_dotenv() LOG_LEVEL = env('LOG_LEVEL') LOG_LEVEL = logging.getLevelName(str(LOG_LEVEL).upper()) if LOG_LEVEL is not None else logging.WARNING _setup_logging(LOG_LEVEL) LOG_FORMATTER = logging.Formatter('[%(asctime)s]: %(name)-55s -> %(funcName)-20s : %(levelname)-8s:: %(message)s') _lh = LogHelper('privex.db.tests', handler_level=LOG_LEVEL, formatter=LOG_FORMATTER) _lh.copy_logger('privex.db') _lh.add_console_handler() log = _lh.get_logger() class PrivexTestBase(TestCase): pass class PrivexDBTestBase(PrivexTestBase): """ Base class for all privex-db test classes. Includes :meth:`.tearDown` to reset database after each test. """ def setUp(self) -> None: self.wrp = ExampleWrapper()
lh = LogHelper('lg', formatter=LOG_FORMATTER) CONSOLE_LOG_LEVEL = env('LOG_LEVEL', None) CONSOLE_LOG_LEVEL = logging.getLevelName(str(CONSOLE_LOG_LEVEL).upper()) if CONSOLE_LOG_LEVEL is not None else None if CONSOLE_LOG_LEVEL is None: CONSOLE_LOG_LEVEL = logging.DEBUG if cf['DEBUG'] else logging.INFO lh.add_console_handler(level=CONSOLE_LOG_LEVEL) DBG_LOG, ERR_LOG = os.path.join(BASE_DIR, 'logs', 'debug.log'), os.path.join(BASE_DIR, 'logs', 'error.log') lh.add_timed_file_handler(DBG_LOG, when='D', interval=1, backups=14, level=CONSOLE_LOG_LEVEL) lh.add_timed_file_handler(ERR_LOG, when='D', interval=1, backups=14, level=logging.WARNING) log = lh.get_logger() lh.copy_logger('privex') ####################################### # # RabbitMQ, Redis, and CouchDB Configuration # ####################################### RMQ_HOST = cf['RMQ_HOST'] = env('RMQ_HOST', 'localhost') RMQ_PORT = cf['RMQ_PORT'] = env('RMQ_PORT', pika.ConnectionParameters._DEFAULT) RMQ_QUEUE = cf['RMQ_QUEUE'] = env('RMQ_QUEUE', 'privexlg') hlp_settings.REDIS_HOST = REDIS_HOST = env('REDIS_HOST', 'localhost') hlp_settings.REDIS_PORT = REDIS_PORT = int(env('REDIS_PORT', 6379)) hlp_settings.REDIS_DB = REDIS_DB = int(env('REDIS_DB', 0))
from privex.loghelper import LogHelper from tests.base import PrivexBaseCase from privex.helpers import thread as modthread, LockConflict, random_str, OrderedDictObject from privex.helpers.thread import BetterEvent, event_multi_wait_all, event_multi_wait_any, lock_acquire_timeout, SafeLoopThread from collections import namedtuple from threading import Event, Lock import threading import queue import logging LOG_FORMATTER = logging.Formatter('[%(asctime)s]: %(name)-25s -> %(funcName)-35s : %(levelname)-8s:: %(message)s') _lh = LogHelper(__name__, handler_level=logging.DEBUG, formatter=LOG_FORMATTER) _lh.add_console_handler() _lh.copy_logger('privex.helpers.thread') log = logging.getLogger(__name__) # release_lock = BetterEvent(name='Global Release Lock event') shared_lock = threading.Lock() shared_queue = queue.Queue() stop_threads = BetterEvent(name='Global stop_threads') LockCheck = namedtuple('LockCheck', 'thread_id was_locked lock_exception thread_name') UnlockEvent = namedtuple('UnlockEvent', 'thread_id thread_name') class LockerThread(SafeLoopThread): loop_sleep = 0.05
# Log to console with LOG_LEVEL, as well as output logs >=debug / >=warning to respective files # with automatic daily log rotation (up to 14 days of logs) _lh.add_console_handler(level=LOG_LEVEL) _lh.add_timed_file_handler(str(BASE_DIR / 'logs' / 'debug.log'), when='D', interval=1, backups=14, level=LOG_LEVEL) _lh.add_timed_file_handler(str(BASE_DIR / 'logs' / 'error.log'), when='D', interval=1, backups=14, level=logging.WARNING) _lh.copy_logger('pincore') TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, },