Logging is one of the most important tools for analyzing the operations happening inside our Odoo server while running the service. Moreover, in most cases, by analyzing the server log we can get a clear idea regarding an error or any bug present.
This blog will provide you an insight into how these log messages can be added to our custom modules.
Import logging
Initially, we need to import a module from the python standard library i.e. ‘logging’ if we want to use a logging option, therefore, it's vital to import this python module.
_logger = logging.getLogger(__name__)
Here _logger object is initialized by the name of the current code file, __name__.
Now let's move on to understanding different types of log levels.
* DEBUG
logs a message with level DEBUG on this logger
* WARNING
logs a message with level WARNING on this logger
* INFO
logs a message with level INFO on this logger
* ERROR
logs a message with level ERROR on this logger
* CRITICAL
logs a message with level CRITICAL on this logger
It can be well described by using an example as shown below. Here, I am putting this log message under my custom function in my custom module.
Firstly, we import the logging using the following code:
_logger = logging.getLogger(__name__)
class CustomModel(models.Model):
_name = ‘custom.model’
Now we will define the log messages as shown in the following command:
def _onchange_custom_field(self):
_logger.debug("IT IS DEBUG")
_logger.info("IT IS INFO")
_logger.error("IT IS Error")
_logger.warning("IT IS warn")
_logger.critical("IT IS Critical")
In the context of the above-mentioned example, the module name is product_parameter and the code is in the file product.py whose path is product_parameter/models/product.py.
As we have imported the logger and defined the log message now let's check the server log
From the log message itself, we can obtain an idea regarding the file path and where this log message is showing i.e. odoo.addons.product_parameter.models.product. Moreover, it helps the user to identify the bug/error easily. Now let's move to explore certain other logging options available in the next section.
Other Logging options
We have different options for logging that can be configured in our Odoo conf file.
* logfile = None // Specify the log file to store the log details.
* log_handler = LEVEL: LEVEL // set up a handler at LEVEL for a given PREFIX.
For example, if you want to have DEBUG level for module product_parameter only, you can run it with the parameter as shown below:
log-handler=odoo.addons.product_parameter:DEBUG
* log_level = info // specify the level of the logging.
* log_db_level = warning // Logging database level.
* log_db = False // Specify the Logging database.
There are the certain other logging options available in Odoo which can be configured in the Odoo server configuration file.