最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Django中如何打印输出log到log文件中

Django crifan 4548浏览 0评论

折腾:

【已解决】Django+jwt-token-auth的后端和Antd Pro的Reactjs的前端实现用户名密码错误人性化提示

期间,对于代码:

xxx/apps/util/jwt.py

from apps.user.serializers import UserSerializer

import logging

def jwt_response_payload_handler(token, user=None, request=None):

    logging.info("jwt_response_payload_handler: token=%s, user=%s, request=%s", token, user, request)

    return {

        ‘token’: token,

        ‘user’: UserSerializer(user, context={‘request’: request}).data

    }

中的logging,没有输出对应日志到log文件:

xxx/logs/development.log

只有之前的看似mysql的log:

DEBUG 2018-07-09 15:27:30,360 utils 9697 123145329958912 (0.001) SELECT @@SQL_AUTO_IS_NULL; args=None

DEBUG 2018-07-09 15:27:30,361 utils 9697 123145329958912 (0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None

DEBUG 2018-07-09 15:27:30,362 utils 9697 123145329958912 (0.001) SELECT VERSION(); args=None

DEBUG 2018-07-09 15:27:30,366 utils 9697 123145329958912 (0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None

DEBUG 2018-07-09 15:27:30,367 utils 9697 123145329958912 (0.001) SELECT `user_user`.`password`, `user_user`.`last_login`, `user_user`.`is_superuser`, `user_user`.`username`, `user_user`.`first_name`, `user_user`.`last_name`, `user_user`.`email`, `user_user`.`is_staff`, `user_user`.`date_joined`, `user_user`.`id`, `user_user`.`is_active`, `user_user`.`name`, `user_user`.`mobile_phone_number` FROM `user_user` WHERE `user_user`.`username` = ‘crifan’; args=(‘crifan’,)

且终端中调试信息中,也看不到log:

127.0.0.1 – – [09/Jul/2018 15:27:30] "OPTIONS /api/v1/jwt-token-auth/ HTTP/1.1" 200 –

127.0.0.1 – – [09/Jul/2018 15:27:30] "POST /api/v1/jwt-token-auth/ HTTP/1.1" 200 –

此处需要:

搞清楚,此处的Django+DWF中,自己的额外加入的文件中,如何输出log能到上面的log日志文件中

是看到了:

xx/conf/development/settings.py

# logging settings

LOGGING = {

    ‘version’: 1,

    ‘disable_existing_loggers’: False,

    ‘formatters’: {

        ‘verbose’: {

            ‘format’:

            ‘%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s’

        },

        ‘simple’: {

            ‘format’: ‘%(levelname)s %(message)s’

        },

    },

    ‘handlers’: {

        ‘file’: {

            ‘level’: ‘DEBUG’,

            ‘class’: ‘logging.FileHandler’,

            ‘filename’: os.path.join(BASE_DIR, ‘logs’, ‘development.log’),

            ‘formatter’: ‘verbose’,

        },

    },

    ‘loggers’: {

        ‘django’: {

            ‘handlers’: [‘file’],

            ‘level’: ‘DEBUG’,

            ‘propagate’: True,

        },

    },

}

有log的配置

但是不知道别处的代码中,如何输出log

Django output info to log

python – Django logging to console – Stack Overflow

https://docs.djangoproject.com/zh-hans/2.0/topics/logging/

Django Logging, The Right Way | Lincoln Loop

django logging 配置

Django的日志配置 | 卡瓦邦噶!

django 日志logging的配置以及处理-David-51CTO博客

django日志logging 的配置以及处理 – CSDN博客

Django 日志模块 logging 的配置 – CSDN博客

然后试了试:

import logging

logger = logging.getLogger(‘django’)

logger.info("Test Django logging work or not")

然后看到log信息了

然后调试发现:

(在正确执行代码后)是可以输出log的:

【总结】

此处Django中已经配置了log:

xx/conf/development/settings.py

# logging settings

LOGGING = {

    ‘version’: 1,

    ‘disable_existing_loggers’: False,

    ‘formatters’: {

        ‘verbose’: {

            ‘format’:

            ‘%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s’

        },

        ‘simple’: {

            ‘format’: ‘%(levelname)s %(message)s’

        },

    },

    ‘handlers’: {

        ‘file’: {

            ‘level’: ‘DEBUG’,

            ‘class’: ‘logging.FileHandler’,

            ‘filename’: os.path.join(BASE_DIR, ‘logs’, ‘development.log’),

            ‘formatter’: ‘verbose’,

        },

    },

    ‘loggers’: {

        ‘django’: {

            ‘handlers’: [‘file’],

            ‘level’: ‘DEBUG’,

            ‘propagate’: True,

        },

    },

}

然后其他的代码中:

import logging

logger = logging.getLogger(‘django’)

logger.info("Test Django logging work or not")

即可:

获取配置LOGGING中的名为django的logger

然后接着就可以用logger去输出log信息了。

【后记】

后续参考:

django logging 配置

django logging config

django 日志logging的配置以及处理-David-51CTO博客

Django的日志配置 | 卡瓦邦噶!

django中日志模块使用 – 简书

18:django 日志系统 – 2BiTT – 博客园

Settings | Django documentation | Django

16.7. logging.config — Logging configuration — Python 3.7.0 documentation

16.6. logging — Logging facility for Python — Python 3.7.0 documentation

去优化一下log日志输出格式:

把之前的:

LOGGING中的:

‘format’: ‘%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s’

改为:

‘format’: ‘%(levelname)s|%(asctime)s|%(module)s:%(funcName)s:%(lineno)d|%(message)s’,

‘datefmt’ : "%Y%m%d %H:%M:%S"

转载请注明:在路上 » 【已解决】Django中如何打印输出log到log文件中

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.188 seconds, using 22.17MB memory