Logging in Odoo 15 is a standard Python module that analyzes all the operations performed inside the Odoo server.
Through the Python logging module in Odoo, one can identify and solve various sorts of bugs, errors, or warnings to help the operations run smoothly.
One can view all these logging errors/bugs in the log messages available in all the default or custom Odoo modules.
How to Perform Logging in Odoo 15?
In Odoo, there is a _logger at the top of the Python module, which one can use it in different methods to output log messages. Here’s how to perform the logging:
- To display a log message, a standard Python logging module is used:
import logging
_logger = logging.getLogger(__name__)
class Lead(models.Model):
_inherit = “crm.lead”
@api.onchange(‘name’)
def _onchange_name(self):
_logger.info(‘Lead/Opportunity name changed to %s!’ % (self.name))
- In the above code, the “logging” module has been imported into the Odoo module.
- Here the “_logger” object is initialized by the current module’s name.
- Upon changing the field name in “crm.lead” a log associated with it will be displayed, as shown in the above image of Server Log, where “New Blog” is the new name of the Lead/Opportunity.
- Other than _logger.info, we can achieve other Python logging levels:
- debug: diagnoses problems in a detailed format.
- info: confirms that things are working as expected.
- warning: indicates an unexpected problem or warning in the near future
- error: shows errors due to which the program was unable to perform a function
- critical: logs serious error due to which the program may not be able to continue executing.
All these six logging levels in Python are associated with an integer that indicates the log severity: NOTSET=0, DEBUG=10, INFO=20, WARN=30, ERROR=40, and CRITICAL=50.
- Odoo server configuration file:
- To make settings via config file use the keyword log_handler and set the values as a comma-separated list, e.g., log_handler=werkzeug: CRITICAL,odoo.api:DEBUG
- –log-handler=PREFIX:LEVEL: setups a handler at LEVEL for a given PREFIX while starting the server.
- The option can be repeated to configure multiple loggers e.g., $ odoo-bin –log-handler:DEBUG –log-handler werkzeug: CRITICAL –log-handler odoo.fields:WARNING.
- One can also filter logs using –log-handler to view specific results:
- To have DEBUG level for module sync only: –log-handler=odoo.addons.sync:DEBUG
- To disable werkzeug logs: –log-handler=werkzeug:CRITICAL
- To see all odoo log messages: –log-handler=odoo:DEBUG
- To see all log messages:–log-handler=:DEBUG
The Python logging module is very convenient but contains some quirks that can cause problems for even the best Python developers. Contact us to learn more about the best practices for using Python Logging in Odoo 15.