Enable Dark Mode!
how-to-use-logging-in-odoo-19.jpg
By: Mohammed Farhan

How to Use Logging in Odoo 19

Technical Odoo 19 Odoo Enterprises Odoo Community

When you run Odoo, you can view the server's activity by checking the logs. The logs display errors and warnings, which help you identify and resolve issues. Logs have different levels so you can choose how much detail to see. This blog explains how to use logging in Odoo 19.

Logging

  • Start by importing the logging module in Python: import logging
  • Next, set up a logger for your Odoo class using: _logger = logging.getLogger(__name__)
  • Now you can use _logger.info(), _logger.warning(), _logger.critical(), _logger.debug(), or _logger.error() inside your code to record messages and track what’s happening.

You can also set up logging options directly inside the Odoo configuration file (odoo.conf). For example:

  • log_level = info : This decides the log level you want to use (like debug, info, warning, error, or critical).
  • logfile = /path/log/file.log : This sets the location where the log file will be saved. Make sure to give the actual file path on your server. Odoo supports different log levels to help you categorize log messages.

Another useful setting is the log handler, which lets you apply logging rules to a specific module or prefix:

  • log_handler = LEVEL:LEVEL : For example, if you only want to show WARNING messages for a module called my_custom_module, then you can use: log_handler = odoo.addons.my_custom_module:WARNING

Some other helpful options are:

  • log_level = info — sets the main logging level
  • log_db_level = warning — sets the logging level for database logs
  • log_db = False — turns database logging on or off

Types of log levels

  • DEBUG: Used for checking and tracking what’s happening in the system. This level is mainly for troubleshooting and confirming that each step is working as expected.
  • WARNING: Used when something unexpected might happen later. It alerts you about a possible issue, but the system will still continue running normally.
  • ERROR: Used when a problem occurs that stops the related function from continuing. The system won’t run that part further until the issue is fixed.
  • CRITICAL: Used for very serious problems. This means the system cannot continue working properly and might stop running until the issue is resolved.

Here's a basic example of how you can use logging in Odoo 19:

# -*- coding: utf-8 -*-
import logging
from odoo import api, models
_logger = logging.getLogger(__name__)
class SaleOrder(models.Model):
   _inherit = 'sale.order'
   @api.onchange('partner_id')
   def onchange_partner_id(self):
       """ Logging at different levels """
       _logger.debug("------------Debug Message-----------")
       _logger.info("-------------INFO Message------------")
       _logger.error("------------Error Message-----------")
       _logger.warning("----------Warning Message---------")
       _logger.critical("----------Critical Message---------")

In this example, we created an onchange method to demonstrate how logging works at different levels in Odoo. Whenever this function is triggered, the messages will be recorded in the log, allowing us to see each log level in action

  1. import logging: This brings in Python’s logging module so we can record messages in Odoo
  2. logger = logging.getLogger(__name__): This creates a logger for your Odoo class.
  3. Inside your methods, you can use _logger.info( ), _logger.warning( ), _logger.debug( ), _logger.critical( ) and _logger.error( ) to log different types of messages based on what you want to track or debug.

How to Use Logging in Odoo 19-cybrosys

When we check the logs, we can clearly see entries based on the log levels we defined. It's important to remember that Odoo will only record messages at the selected level and above. For example, if the log level is set to debug, then all messages—including debug, info, warning, error, and critical—will be captured. However, if we choose a higher level like warning, debug and info messages will not appear.

By configuring the right log level, we can easily monitor system activity, troubleshoot issues, and gain better insights into how our Odoo environment is performing. Logging becomes a powerful tool for understanding and maintaining smooth operations in the system.

To read more about How to Use Logging in Odoo 17, refer to our blog How to Use Logging in Odoo 17


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message