Sunday, May 30, 2021

Django log

Use 2 logger patterns: django and django.db.backends.
Need to make sure to add 'propagte': False to django.db.backends
Otherwise, the db log will show twice.


# Separate DB SQL log from django log
LOGGING = {
    'version'1,
    'filters': {
        'require_debug_true': {
            '()''django.utils.log.RequireDebugTrue',
        }
    },
    'handlers': {
        'db_handler': {
            'level''DEBUG',
            'filters': ['require_debug_true'],
            'class''logging.StreamHandler',
            'formatter''db_format'
        },
        'log_handler': {
            'level''DEBUG',
            'filters': ['require_debug_true'],
            'class''logging.StreamHandler',
            'formatter''log_format'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['log_handler'],
            'level'os.getenv('DJANGO_LOG_LEVEL''INFO'),
        },
        'django.db.backends': {
            'level''DEBUG',
            'handlers': ['db_handler'],
            'propagate'False
        },
    },
    'formatters': {
        'db_format': {
            'format''sql> %(message)s'
        },
        'log_format': {
            'format''django> %(message)s'
        }
    }
}


In a api request, the console show logs like this:


Django version 3.2.3, using settings 'bookstore_openapi.settings'
Starting development server at http://0.0.0.0:8001/
Quit the server with CTRL-BREAK.
sql> (0.000) SELECT "book_book"."id", "book_book"."title", "book_book"."category_id", "book_book"."publisher_id", "book_book"."author_id" FROM "book_book" WHERE "book_book"."id" = 1 LIMIT 21; args=(1,)
django> "GET /api/books/1 HTTP/1.1" 200 72

 

 

 

Monday, May 17, 2021

Github main/master branches

Today, I just found out why my git pull not always up to day. Then, I found out not my current working "main" branch.

The main branch is just like come out of middle of somewhere with shorter life.

To fix the git pull issue, the easy way is just go to github repository setting > branches, then fix the default branch.





Saturday, May 15, 2021

Response and JSONResponse

JsonResponse is from vanilla django.

Response is from restframework.

I have code in the following respectively.

from django.http.response import JsonResponse
 
from book.models import Publisher
from book.serializers import PublisherSerializer



def get_publisher(publisher):
    publisher_serializer = PublisherSerializer(publisher
    return JsonResponse(publisher_serializer.data) 

 

from rest_framework.response import Response
 
from book.models import Publisher
from book.serializers import PublisherSerializer


def get_publisher(publisher):
    publisher_serializer = PublisherSerializer(publisher
    return Response(publisher_serializer.data)

What are the differences?

 In the settings, I have REST_FRAMEWORK overwritten.

REST_FRAMEWORK = {

    'DEFAULT_RENDERER_CLASSES': (
        'utils.api.renderers.CamelCaseJSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),

    'DEFAULT_PARSER_CLASSES': (
        'utils.api.parsers.CamelCaseJSONRenderer',
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser'
    ),
}

That is the conversion between snake case and camel case for all the keys of json.
In the python, we use snake case for all the variable naming.
In the json, the convention is camel case.

In order to have the case conversion work, the Response from rest_framework.response should be used in here.






 

Sunday, May 09, 2021

Node.js and Elastic Search

There are two elastic search node.js packages: elasticsearch and @elastic/elasticserach.
Both works. Feel confusing?

According to https://openbase.com/categories/js/best-nodejs-elasticsearch-libraries?orderBy=RECOMMENDED&,
looks like @elastic/elasticsearch is the chosen one now.