def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first settingfrom django.utils.version import get_version VERSION = (3, 0, 4, 'final', 0) __version__ = get_version(VERSION) def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. """ from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix( '/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME ) apps.populate(settings.INSTALLED_APPS)
def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. 1 配置Django 加载配置; 设置日志; 初始化应用注册表 2 自动调用该函数 运行一个通过 DJANGO的 WSGI 支持的HTTP 服务 调用管理命令 """ # 0 加载配置 from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging # 1 配置日志(导入dictConfig模块, 载入自定义配置) configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME) # 2 初始化apps, 遍历调用AppConfig.create() apps.populate(settings.INSTALLED_APPS)
def test_logger_itself( self, debug_status: DebugStatus, log_level: LoggingLevel, mocked_stderr: MagicMock, ) -> None: with override_settings( DEBUG=debug_status, LOGGING=TestLoggerItself.get_logging_configuration( debug_status, log_level, ), ): configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) # really change logging settings logger_header: logging.Logger = logging.getLogger(f'header_{__name__}') logger_header.debug(f'') # write *header* to stderr mocked_stderr.reset_mock() # the test doesn't start yet, resetting the mock object logger: logging.Logger = logging.getLogger(__name__) level: LoggingLevel # type hints for level in LOGGING_LEVELS: logger.log(level, f'hello "{logging.getLevelName(log_level)}"') if level >= log_level: self.assertTrue(mocked_stderr.method_calls) else: self.assertFalse(mocked_stderr.method_calls) mocked_stderr.reset_mock() configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) # return to the native settings back
def setup(set_prefix=True): from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME) apps.populate(settings.INSTALLED_APPS)
def setup(): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. """ from django.apps import apps from django.conf import settings from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) apps.populate(settings.INSTALLED_APPS)
def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. """ from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.encoding import force_text from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else force_text(settings.FORCE_SCRIPT_NAME)) apps.populate(settings.INSTALLED_APPS)
def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. """ from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix( '/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME ) apps.populate(settings.INSTALLED_APPS)
def setup(): """ 初始化Django Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. """ # TODO: 为什么在函数内部导入,减少导入模块的时间,不调用不导入. from django.apps import apps from django.conf import settings from django.utils.log import configure_logging # 这里就开始访问setting的属性了,那么setting的延迟初始化在这里并没有体现出优势,难道是其他路径不用setting的很多? # # The callable to use to configure logging,这个东西是ligging模块里面的,表示用字典配置(将配置放在字典然后解析??) # LOGGING_CONFIG = 'logging.config.dictConfig' # # Custom logging configuration. # LOGGING = {} configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) apps.populate(settings.INSTALLED_APPS)
def setup(self, set_prefix=True): # don't allow DJANGO_SETTINGS_MODULE because it loads settings differently than settings.configure (see dj docs) if "DJANGO_SETTINGS_MODULE" in os.environ: raise RuntimeError('DJANGO_SETTINGS_MODULE environment variable is not supported.') # load settings if isinstance(self.settings, str): module = importlib.import_module(self.settings) self.settings = {k: getattr(module, k) for k in dir(module) if not k.startswith('_') and k.isupper()} # inject urls into settings self.settings['ROOT_URLCONF'] = self.urls # inject root asgi app setting if 'ASGI_APPLICATION' not in self.settings: self.settings['ASGI_APPLICATION'] = find_asgi_path() # set default migrations module if 'MIGRATIONS_MODULES' not in self.settings and importlib.util.find_spec('migrations'): self.settings['MIGRATION_MODULES'] = {self.app: 'migrations'} # add this application to installed apps self.settings['INSTALLED_APPS'] = [self.app] + self.settings.get('INSTALLED_APPS', default.INSTALLED_APPS) # configure settings settings.configure(default, **self.settings) # configure logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) # set prefix if set_prefix: set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME) # populate apps apps.populate(settings.INSTALLED_APPS) # setup urls self.urls.setup()
def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. 配置设置(这是访问的副作用 配置日志并填充应用注册表。 如果' set_prefix '为真,则设置线程本地 rlresolvers 脚本前缀 """ from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) # 配置 日志 设置 if set_prefix: # # 参数为 '/' set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME) apps.populate(settings.INSTALLED_APPS) # 开始 填充
def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. """ import threading ct = threading.current_thread() print('【django.__init__.setup】当前线程:', ct.name, ct.ident) from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME) # 这个 apps 是 django.apps.registry.Apps 类的实例,叫做「应用收集对象」 # 这里调用 populate 方法将项目的配置项 INSTALLED_APPS 中的应用都放到实例自身的 app_configs 属性字典中 # key 就是 INSTALLED_APPS 列表里的字符串 # value 是 django.apps.config.AppConfig 类的实例,此实例就是应用对象 apps.populate(settings.INSTALLED_APPS)
def setUp(self): configure_logging(settings.LOGGING_CONFIG, cfg) self.log = logging.getLogger('test.logging')
from django.utils.version import get_version
from django.apps import apps from django.utils.encoding import force_text from django.utils.log import configure_logging import logging ALLOWED_HOSTS = ['*'] # The callable to use to configure logging LOGGING_CONFIG = 'logging.config.dictConfig' # Custom logging configuration. LOGGING = {} configure_logging(LOGGING_CONFIG, LOGGING) logging.config.dictConfig({ 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'intermediate': { 'format': '%(name)s <%(process)d> [%(levelname)s] "%(funcName)s() %(message)s"' }, }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'intermediate'
def setup_logging(*args, **kwargs): from django.conf import settings from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
def configure_logger(conf=None, **kwargs): from django.conf import settings configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
def configure_logging(sender=None, **kwargs): from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) patch_celery_traceback()
from importlib import import_module from django.conf import settings from django.core.management.base import BaseCommand, CommandError from django.db import models from PageView.models import AUDaily, KeyValue, UserData, VideoPlayedLog, LogLinkVisitAction, LogVisit, LogAction # from PaymentSystem.models import User, PaymentOrders, ProductOrdered, ConsumeLog from django.db.models import Sum, Count from common import Utils from ..CollectCommon import UpdateUserDataFromLogVisit import logging from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.COMMAND_LOGGING) # init command logging logger = logging.getLogger("command") one_day = datetime.timedelta(days=1) class Command(BaseCommand): help = "从多个模块中收集数据填充到 AUDaily 中" db_key = "DailyAUCollect" def add_arguments(self, parser): """ 添加命令参数 @param parser: see also: https://docs.python.org/3/library/argparse.html#module-argparse """
import sys # when we run #celery -A basic_django worker --loglevel=debug # it will run this file. #we have to set the DJANGO_SETTINGS_MODULE so that the when we run # celery -A basic_django worker --loglevel=debug, it will look for the env varaible # and it will activate the django and then get all the tasks. in all the installed apps. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'basic_django.settings') # This we will need for using the logging config defined in settings.py # We can use custom logging we created for django here also. from django.conf import settings from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) #This defines the celery app instance redis = 'redis://:gauranga@redis:6379/0' app = Celery('basic_django', broker=redis, backend=redis ) #Celery instance is assign to app variable by convention. Keep your project #simple and #consistent. Celery instance should be named same as project. For #example if project’s name is “gift-catalogue” then app = #Celery('gift-catalogue', broker=redis, backend=redis)