Put logger in the open source pip package and analyze the operation

Overview

--To check the operation of the pip package whose source is open to the public, create a package with logger and install it locally.

Target of this time

Preparation

--Check the currently installed version - pip list | grep django-allauth

--Clone source code from GitHub - mkdir django-allauth-with-log && cd django-allauth-with-log - git clone https://github.com/pennersr/django-allauth.git - cd django-allauth

Check the procedure to install with pip locally

Change version number

#VERSION = (0, 44, 0, "dev", 0)
VERSION = (0, 44, 0, "dev", 99)

Uninstall installed packages

Install locally

--Move to a directory one level above the directory containing setup.py - cd .. --Install by specifying a directory with pip - pip install ./django-allauth/

Check the installed version

--If the version number django-allauth 0.44.0.dev99 rewritten above appears, it is successful.

Modify for analysis

Added logger settings

--Add a configuration file and load it from ʻallauth / app_settings.py`

from .logging_settings import *    #add to
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,

    'formatters': {
        'django.server': {
            '()': 'django.utils.log.ServerFormatter',
            'format': '[%(server_time)s] %(message)s a',
        },
        'develop': {
          'format': '%(asctime)s [%(levelname)s] %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'develop',
        },
        'django.server': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'django.server',
        },
    },
    'loggers': {
        '': {
          'handlers': ['console'],
          'level': 'DEBUG',
          'propagate': False,
        },
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
        },
        'django.server': {
            'handlers': ['django.server'],
            'level': 'INFO',
            'propagate': False,
        },
    }
}

Add log output to any location

--For example, add the following to ʻallauth / account / views.py`

--logger settings

import sys
import logging
logger = logging.getLogger(__name__)

--Add the following anywhere (eg def dispatch in class SignupView)

    logger.info(f'class={self.__class__.__name__}, function={sys._getframe().f_code.co_name}, Line={sys._getframe().f_lineno} called.')

--Note that the class name is the name of the inheritance destination?

Reinstall with pip locally

Operation check

--Start the server, access the URL like http://127.0.0.1:8000/member/signup/, and it is OK if it is displayed on the console as below.

2020-11-03 01:23:45,678 [INFO] class=SignupView, function=dispatch, Line=238 called.

Recommended Posts

Put logger in the open source pip package and analyze the operation
Put Tkinter in Macbook and check operation
Put pip in Blender
Put the module in the Pepper app
Put postfix 2.11 in source with ansible
Installing and uninstalling the egg package
Enclose the cat result in double quotes and put it in a variable
Open an Excel file in Python and color the map of Japan
I can't find the commands in the package introduced by pip from Emacs