Day19 Let's ODOO: Logging

在我们写Service时,我们通常会记录自己想要的logs以供分析,本篇来介绍如何使用Odoo的log。

我们於controllers/main.xml 内汇入 logging,并增加log:

import logging
_logger = logging.getLogger(__name__)

class StudtentController(Controller):
    
	@route('/student', methods=['POST'], type='json', auth='public', cors='*', csrf=False)
	    def create_student(self):
	        _logger.info('---------> %s \n', request.httprequest.data)
	        student_data = json.loads(request.httprequest.data)
	        val = {
	            'name': student_data.get('name'),
	            'nickname': student_data.get('nickname'),
	            'gender': student_data.get('gender'),
	            'birthday': student_data.get('birthday')
	        }
	
	
	        student_obj = request.env['res.student'].sudo()
	        student_obj.create(val)
	        result = {'code': 200, 'message': 'Created Successfully'}
	        body = json.dumps(result, default=date_utils.json_default)
	        return Response(
	            body, status=200,
	            headers=[('Content-Type', 'application/json'), ('Content-Length', len(body))]
	        )

我们import loggin,并且将它初始化

import logging
_logger = logging.getLogger(__name__)

印出自己想要的层级与字串

_logger.info('---------> %s \n', request.httprequest.data)

Log level

logs有分五个层级,infodebugerrorwarningcritical,写法就如同上方一样

写好以後别忘了在odoo.conf内设定路径,否则只有在terminal才会看到

logfile = /var/log/odoo/odoo.log
...

如此一来,当我们打API的时候便会显示log,并会储存於指定档案位置:

request body:

{
	"name": "Andy Chen",
	"nickname": "Andy",
	"gender": "male",
	"birthday": "2020-02-02"
}

log:

2021-10-02 17:52:04,987 1630 INFO odoo.service werkzeug: 127.0.0.1- - [02/Oct/2021 17:52:04] "POST /longpolling/poll HTTP/1.0" 200 - 19 0.015 0.059
2021-10-02 17:52:55,080 1630 INFO odoo.service werkzeug: 127.0.0.1 - - [02/Oct/2021 17:52:55] "POST /longpolling/poll HTTP/1.0" 200 - 7 0.002 0.006
2021-10-02 17:53:45,184 1630 INFO odoo.service werkzeug: 127.0.0.1 - - [02/Oct/2021 17:53:45] "POST /longpolling/poll HTTP/1.0" 200 - 7 0.002 0.006
2021-10-02 17:54:35,275 1630 INFO odoo.service werkzeug: 127.0.0.1 - - [02/Oct/2021 17:54:35] "POST /longpolling/poll HTTP/1.0" 200 - 7 0.003 0.007
2021-10-02 17:54:52,248 1630 INFO odoo.service odoo.addons.student.controllers.main: ---------> b' {\n            "name": "Andy Chen",\n            "nickname": "Andy",\n            "gender": "male",\n            "birthday": "2020-02-02"\n            }'

<<:  Day19 - 读取更多推文

>>:  Day19 Android - NestedScrollView

Engineering, Life Cycle Stages, and Processes

Engineering, Life Cycle Stages, and Processes Eng...

DAY1:拉开序幕:比赛前言

大家好!此次比赛是我第一次参赛,我会想将Android Studio作为我这次的主题,原因是在於我想...

D5: [漫画]工程师太师了-第3话

工程师太师了: 第3话 杂记: 继续聊天聊满30天~ 之前说过在编剧时发现, 工程师的生活也就只有上...

学习Ruby、Rails事前准备工作

专有名词 整理了我觉得该先了解的一些专有名词 wiki-物件导向程序设计、菜鸟式回答:是一种将资料,...

如果你真的够渴望做点什麽,任何事情你都可以持续做三十天。

如果你真的够渴望做点什麽,任何事情你都可以持续做三十天。 If you really want so...