Using Odoo 16 to log In

RAMSHAD
March 7, 2024
logging-module-in-odoo16

For developers and administrators in Odoo and other software, logging is essential for tracking and understanding system behavior. Using Python’s logging module, Odoo provides logging capabilities as an ERP platform. Loggers are a useful tool in Odoo 16 that allows you to capture data about errors, system performance, and other pertinent information.

This is a basic illustration of how to use Odoo’s logging feature.

import logging
_logger = logging.getLogger(__name__)
class YourOdooClass(models.Model):
    _name = 'your.odoo.class'
    def your_method(self):
        try:
            # Your code logic here
            # Example log messages
            _logger.info("Your information log message")
            _logger.warning("Your warning log message")
            _logger.error("Your error log message")
        except Exception as e:
            # Log an error if an exception occurs
            _logger.exception("An error occurred: %s", e)

Examples :

  1. Import the logging module from Python.
  2. Create a logger instance for your Odoo class using logging.getLogger(_name__). The actual name of your Odoo class should be replaced with ‘your.odoo.class’
  3. Logging different types of messages is possible using the logger.info( ), logger.warning( ), and loggererror( ) methods.

In addition, the Odoo user interface and the Odoo configuration file (odoo. conf) allow you to customize the logging settings. Configurations like log file path, log level, and log format can be included.

Here is an example of configuring logging settings in the odoo.conf file:

[options]

; Set the log level to debug, info, warning, error, or critical

log_level = info

; Set the log file path

logfile = /path/to/your/log/file.log

Make sure to replace /path/to/your/log/file.log with the actual path where you want to store the log file.

The standard logging module in Python is the main source of logging functionality in Odoo. Your log messages can be prioritized and categorized using various log levels. The typical log levels are as follows, in increasing order of severity:

  • DEBUG Detailed information, usually relevant only for troubleshooting: Confirmation that everything is running according to plan.
  • WARNING: A sign of an unforeseen event or an early warning sign of a possible problem soon (e.g., “disk space low”). The program is still functioning as intended.
  • ERROR: Some features of the software have not been able to be performed because of a more serious problem.
  • CRITICAL: A very serious mistake indicating that the program might not be able to continue running.

The logger object contains These log levels in Odoo through matching methods. The Odoo tools module provides access to the standard logger.

The following describes how to use various log levels in Odoo:

import odoo.tools as tools
# Example usage in a method of an Odoo model
def some_method(self):
    # ...
    # Logging at different levels
    tools.log(level=tools.LOG_DEBUG, message="This is a debug message")
    tools.log(level=tools.LOG_INFO, message="This is an info message")
    tools.log(level=tools.LOG_WARNING, message="This is a warning message")
    tools.log(level=tools.LOG_ERROR, message="This is an error message")
    tools.log(level=tools.LOG_CRITICAL, message="This is a critical message")
    # ...

If you find yourself inside a method within an Odoo model, replace yourself in the example above with the relevant instance.

The Odoo logging system will record these log messages, and based on your configuration, they may be shown in the user interface, saved in a file, or stored in a database.

Investigate the various log levels and how they behave in the Odoo log output, especially when editing a request date field in our Service Request module.

from odoo import http
import logging
_logger = logging.getLogger(__name__)
class CaseSync(http.Controller):
    @http.route('/Test/RestApi/case_sync', type='json', method=['POST'],
                auth='public', csrf=False)
    def case_sync(self):
        _logger.info("****************************INFOOOOOOOOOO*****************************************")
        _logger.warning("*********************************WARNING***************************************")
        _logger.error("***********************************ERORR*****************************************")
from odoo import http
import logging
_logger = logging.getLogger(__name__)
class CaseSync(http.Controller):
    @http.route('/Test/RestApi/case_sync', type='json', method=['POST'],
                auth='public', csrf=False)
    def case_sync(self):
        _logger.info("****************************INFOOOOOOOOOO*****************************************")
        _logger.warning("*********************************WARNING***************************************")
        _logger.error("***********************************ERORR*****************************************")
        _logger.exception("*****************************EXCEPTION***************************************")
        _logger.debug("*********************************DEBUG*******************************************")
        _logger.critical("******************************CRITICAL****************************************")

Of course! Several logging levels are used in Python’s logging module to classify messages according to their severity. The levels are DEBUG, INFO, WARNING, ERROR, and CRITICAL, in increasing order of severity.

Only messages at that level or higher will be recorded in the log when you set the logging level. As an illustration:

* INFO, WARNING, ERROR, and CRITICAL level messages will be logged if the logging level is set to “info,” while DEBUG messages will be ignored.

* Only messages of level WARNING, ERROR, and CRITICAL will be logged if the logging level is set to “warning,” while lower-level messages like DEBUG and INFO will be ignored.

Consequently, when I said, “If the logging level is set to a higher level (e.g., ‘info’ or ‘warning’), debug messages will not appear in the log,” I meant that debug messages, which have a lower severity level, won’t be included in the log output if you configure Odoo to log messages at a higher severity level (e.g., “info” or “warning”).

You must set the logging level to “debug” or a lower level to record debug messages in the log. This makes sure that all messages—DEBUG, INFO, WARNING, ERROR, and CRITICAL—are logged, regardless of their severity.

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *