Enable Dark Mode!
how-does-standard-logging-work-in-odoo-19.jpg
By: Hafsana CA

How Does Standard Logging Work in Odoo 19

Technical Odoo 19 Odoo Community Odoo Enterprises

While the Odoo application is being developed or deployed, a lot of things are happening behind the scenes. Requests are processed, scheduled actions are performed, modules do what they have been designed for, including database operations, and many automatic processes that can be both successful and unsuccessful. The standard logging feature available in Odoo 19 helps developers and system administrators understand what is happening inside the system. Rather than being dependent only on the UI, developers may write down valuable information, including server logs, and use the information in troubleshooting processes.

The logging functionality in Odoo 19 is based on the built-in logging libraries existing in Python. The developers may create a new logger in the Odoo module and send messages with different priorities. For example, an INFO message may represent successful completion of a job, while a WARNING signals something worthy of paying attention to.

What is Standard Logging in Odoo 19?

Logging is used to effectively ascertain the operations being performed by the Odoo software system during its functioning. When users create invoices or confirm sales orders or even update records, several processes execute at the backend. Therefore, only observing the graphical interface does not help in knowing the exact problem if something goes wrong.

Odoo employs the logging package of the Python programming language for logging backend activities. The developers are able to create loggers in their Python source code, enabling them to write messages into log files on the Odoo server.

Typically, a logger is created at the beginning of an Odoo Python file:

import logging
logger = logging.getLogger(__name__)

Here __name__ actually refers to the name of the Python module where the logger is used making it easy to identify the location of generation of logging message in Odoo code.

Why is Logging Useful in Odoo?

Odoo modules can have a lot of business logic that depends on customizations for a certain enterprise. Sometimes problems are disguised and can’t be seen on the screen. For instance, it’s possible that in an automated action, the reason for failure is an unexpected value or something like that.

The usage of logging allows the developer to see beyond those situations.

Some cases when logging is useful in Odoo include:

  • Finding the causes of unexpected behavior of custom modules.
  • Checking if some methods are executed.
  • Monitoring important actions during the development process.
  • Finding bugs in automated processes and scheduled actions.
  • Knowing what data is being evaluated by particular backend logic.
  • Solving users’ issues.
  • Controlling integrations with third-party systems.

However, please keep in mind that logging cannot substitute the existing error handling mechanism. A logged message explains what happened, but the application must deal with errors.

Typically, a module logger is created at the module level with the help of Python's logging module.

For instance:

import logging
from odoo import models
_logger = logging.getLogger(__name__)
class SaleOrder(models.Model):
    _inherit = "sale.order"
    def action_confirm(self):
        _logger.info("Confirming sales order: %s", self.name)
        return super().action_confirm()

In this instance, a log entry is generated every time the action_confirm() method is utilized. The %s in the text is removed and substituted with the sales order name.

In general, logging is better than using print() statements since print() simply outputs messages to the console, while logging provides different log levels and has a variety of configuration options according to the environment.

How to Understand Odoo Log Levels

Not all events are equal, and a successful process must be treated differently from a major failure in the application. This is the reason why Python has different logging levels for developers, as per the situation.

Here are some of the common levels that you should know:

  • DEBUG - Very detailed information, which may be helpful when trying to find the cause of an issue or a bug.
  • INFO - A helpful general piece of information about the normal operation of the application.
  • WARNING - An unexpected event has occurred but continues working.
  • ERROR - One of the operations has failed.
  • CRITICAL - A serious problem that may prevent an important part of the application from functioning correctly.

For instance, an info message can be printed in case the application has processed an external API request successfully

_logger.info("Customer synchronization has been finished successfully.")

When something unexpected happens in the application, a warning can be a better option for what to log:

_logger.warning(
    "Customer %s does not have an external reference.",
    partner.name
)

When there is an error in the operation, an error message may really help with troubleshooting.

_logger.error(
    "Could not synchronize customer %s with external API.",
    partner.name
)

How does standard logging function in Odoo 19?

The logging process is relatively easy. When a logging statement is reached by a method within Odoo, the logging framework is used to record a log.

The log record contains details like the message, the name of the logger, and the level of severity.

Then, Odoo's logging configuration processes the record, and it appears in the Odoo server log file or the Odoo server.

The flow can be illustrated in the form of:

Odoo Python code > Logger > Level of logs > Logging configuration in Odoo > Odoo server log file

For example, take the following method:

def process_order(self):
    _logger.info("Start processing the order for %s", self.name)
    # Business logic
    _logger.info("Finished processing the order for %s", self.name)

As the method gets executed, the messages will be displayed in Odoo server logs, allowing the developer to see how the method was executed without showing any technical details.

Recording Exceptions in Odoo

Logging is especially helpful for capturing exceptions. Each time an operation fails in the try block, developers can log the error using _logger.exception() along with the traceback.

For instance:

try:
    result = self.env["some.model"].create(values)
except Exception:
    _logger.exception("Failed to create the record.")

What is important is that the operator logger.exception() should be used inside the exception handler. It logs not only the error message, but the traceback as well.

Using logger.exception() is more useful than just logging something like:

_logger.error("Something went wrong.")

Thus, a successful error log contains enough context to allow a developer to comprehend what operation failed and which record or process (if applicable) was involved in it.

Logging Data During Development

Logging data in a development stage can be useful to programmers in understanding the process of how information is handled by a function.

As a concrete example, the following command can be used:

_logger.debug(
  "Processing partner %s with country %s",
  partner.name,
  partner.country_id.name
)

This technology can help when problems are encountered while working on complex business operations. Instead of revising the code by passing different print () functions, programmers can use DEBUG statements and activate logging when needed.

Nevertheless, it is necessary to keep in mind that no sensitive data such as passwords, access tokens, API keys, or irrelevant personal data shall be logged. The information logged on the server may include a lot of technical data, and its handling should be careful.

Setting up Logging in Odoo

Odoo gives several command-line options for working with logs, which allow users to control how much information will be recorded in the logs.

Specifically, a programmer can start working with Odoo by setting a necessary log level:

python3 odoo-bin --log-level=debug.

For the standard usage of the program, it would be better to have a less verbose level. Otherwise, logging in

Logging Best Practices in Odoo 19

Simply adding a logger to each method does not mean that the application will become any easier to troubleshoot; on the contrary, an excess of unnecessary messages can have a negative effect. Effective logging should offer useful context without overwhelming the server output.

A few practices can make Odoo logs much easier to work with:

  • Use _logger = logging.getLogger(__name__) instead of making up random logger names.
  • Select the log level according to the significance of the event.
  • It is useful to include record names or IDs where appropriate.
  • DEBUG can be used to obtain detailed troubleshooting information.
  • INFO should be used for normal operations that are meaningful.
  • Use WARNING in cases where attention is warranted even though they are not failures.
  • ERROR should be used when an operation has failed.
  • It is necessary to use _logger.exception() when dealing with exceptions, and it is useful to have a traceback.
  • Do not record passwords, tokens, API keys, or any other sensitive information.
  • It is advisable not to include excessive logging within frequently executed loops or methods.
  • Stick to logging in production only those items that are actually useful.

What is really important about logging is context. A message like 'Process completed' doesn't tell a developer very much, whereas a message such as 'Invoice INV/2026/0042 successfully synchronized with the external service' provides a great deal more information when looking into a real problem.

I find that standard logging in Odoo 19 is an important tool for understanding what happens inside an Odoo application. By using Python’s built-in logging framework, Odoo developers can record useful information while keeping technical details hidden from end users. Different log levels also make it easier to distinguish normal application activity from warnings and serious errors.

For custom Odoo development, good logging can save a lot of time when troubleshooting issues, particularly when working with scheduled actions, automated workflows, database operations, and external API integrations. The key is to log information at the appropriate level while avoiding sensitive data and unnecessary messages. When used properly, standard logging gives developers valuable insight into the application’s behavior and makes maintaining an Odoo 19 system much easier.

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


Frequently Asked Questions

What is logging in Odoo 19?

Logging in Odoo 19 is a way to keep track of what the application's doing. It saves details about what happens when the application runs, what errors occur, and how the back end works.

How do I create a logger in Odoo?

You can make a logger by using the logging module in Python. The code looks like _logger = logging.getLogger(__name__)

What are the main Odoo logging levels?

The main levels in Odoo are DEBUG, INFO, WARNING, ERROR, and CRITICAL.

Why use _logger of print()?

Using _logger is better than using print() because it gives you control. It lets you set levels of importance, and it is easier to manage and change settings.

How can I log an exception in Odoo?

To log an error, you can use _logger.exception() inside a part of the code that handles errors. This records the error and the steps that led to it.

Can Odoo logs help with debugging?

Yes, Odoo logs are very helpful when trying to find problems. They show details that can help to fix issues in custom modules, how things work, how tasks run, and how different parts connect.

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



0
Comments



Leave a comment



WhatsApp