How to Use Logging in Odoo 17

Walter White
cybrosys

--

When running Odoo, we can analyze the operations that take place inside the Odoo server by using logging methods. We will get the errors or warnings from the logs, and by doing so, we can find better solutions to overcome these errors or warnings. Logging contains different levels to analyze the operations inside the server. This blog will explain how to log in to Odoo 17.

Logging

* First, import the logging module in python: ie, import logging

* Next, create logger instance for the odoo class: ie, _logger = logging.getLogger(__name__)

* Then use _logger.info( ),_logger.warning( ) or _logger.error( ) to log the messages.

Also possible to add logging settings in the Odoo configuration file (odoo.conf). For example,

; Set the log level (debug, info, warning, error, or critical).

log_level = info

; Set the path of the log file.

logfile = /path/log/file.log

Here the path is the real path where the log file is going to store. There are different log levels to categorize the log messages.

; Set up the handler at a LEVEL for a given PREFIX.

log_handler = LEVEL: LEVEL

It means, if we have to set WARNING level for the module example_price_list only, then we can run as: log-handler=odoo.addons.example_price_list:WARNING

Other options are:

*log_level = info //Set the logging level.

* log_db_level = warning // Set the Logging database level.

*log_db = False // Set the Logging database.

1. DEBUG

It’s mainly for troubleshooting the operations or verifying the operations are proceeding.

2. WARNING

If any unexpected issue will occur in the future,, in that case use this log level and it will continue the same operations.

3. ERROR

If any issue occurs, then it will not execute other functions.

4. CRITICAL

If any critical issue happens, suggest the inability to execute the program and sustain the operations.

Let’s check an example for these different log levels.

* First, import the logging module and initialize the logger to a variable.

import logging
_logger = logging.getLogger(__name__)

* Then, create any function to show the different log levels. Ie,

class ProductPriceList(models.Model):
_inherit = "product.pricelist"
@api.onchange('currency_id')
def onchange_currency_id(self):
""" Logging at different levels """
_logger.debug("------------IT IS DEBUG-----------")
_logger.info("-------------IT IS INFO------------")
_logger.error("------------IT IS Error-----------")
_logger.warning("----------IT IS warning---------")
_logger.critical("----------IT IS Critical---------")

Here, created an Onchange function for logging at different levels. Whenever we execute this function, we can check on the log. ie,

Here, we can see the log with the specified levels. If we specify a log level, then that level or higher will be logged. So If we want to add debug messages on log then set the log level to debug or lower.

This way we can analyze and track the system behavior using the logging tool in Odoo.

To read more about Logging in Odoo 16, refer to our blog Logging in Odoo 16

Originally published at https://www.cybrosys.com.

--

--

Walter White
cybrosys

Hi , Walter White here. I love to post blogs on topics related to technology. Hope you guys will like my writings.