What does logging mean?

Logging is an operation in Odoo to check, analyze, and rectify errors in the Odoo server. It is an inherent functionality from Python that allows developers to output certain data on a terminal window, or into a log file for future analysis. 

Why is logging required?

Logging can be added to any module during the development stages, where the developers find the need to analyze certain data. Such information can be used to debug errors or pinpoint incorrect values getting passed over into APIs and across modules. This helps in perfecting Odoo modules and keeping the business functioning as expected. 

One of the key advantages of having logging in place is to determine user access. Every time a user authenticates into the system, a log entry is made. This helps administrators quickly identify breaching and hacking events, along with unauthorized access attempts. Logging also keeps track of timestamps and IP addresses from where the event has occurred. This helps in the traceability of events happening on the Odoo system.

Steps to perform logging in Odoo 16

1. Import: The first step if we want to log any data is to import the Logging module from the standard libraries provided by Python into their code. This step is executed with a simple import statement as follows:

import logging

With the logging module now ready to use, we can create an object for logging inside the Service request module file of Odoo by invoking the getLogger method as

_logger = logging.getLogger(__name__)

2. Add loggers in code: With the logging object available, we can now simply start adding log statements into our module. There are 5 different levels of logging that we can do, which are:

a. DEBUG: _logger.debug is used to display information as a debug message

b. INFO: _logger.info is used to categorize the log as an info message

c. WARNING: _logger.warning is used for logging messages as a warning

d. ERROR:_logger.error Error levels are used in highlighting any error-based data

e. CRITICAL: _logger.critical is the highest level of logging, generally used to symbolize failure in the system

3. Verify Odoo log output: Let’s take a closer look at these different levels and how they function when a request date field is modified in our Service Request module:

To verify these log messages, we need to view them on the server console:

4. Additional configurations: In the above example, we have all 5 levels enabled, thereby displaying all the loggers. However, in the real world, there would only be certain log categories that will be enabled, and this is done using some additional configurations that can be added in the odoo.conf file:

a. logfile = odoo.log // All log information will be stored on a file called odoo.log.

b. log_handler = : DEBUG // This will enable all Debug category logs to be displayed

For example, if we require only an ERROR level for the module service_request only, then we can run as shown below:

log_handler=odoo.ServiceRequest:ERROR

c. log_level = critical // Specify the critical logging level for all modules. However, this has been deprecated and replaced with log_handler.

d. log_db_level = info // Set the info Logging level for the database.

e. log_db = true // When using the above and logging database execution statements, we need to set log_db as true.

Things to consider when logging data

While logging is an extremely important and useful feature for all developers, there are a few important things to consider:

  • Choose the correct category of log in the code: If we select an incorrect category, we might land up in a situation where we land up enabling unwanted information in the log
  • Amount of logger statements: Avoid unnecessary logging of data, as it only slows down the application. It is important to know where log statements are most useful and add them there only.
  • Data being logged: A lot of data is client specific and sensitive as Odoo modules deal with Banking, Purchasing, Invoicing, etc. This means not everything can be added and displayed on logs, and it’s necessary to keep confidentiality in mind.

Conclusion

Odoo logging is an essential part of the development process, and it helps in debugging and resolving issues quickly, identifying potential problems in the data flow. While the use of logging is an integral part of code, we always need to vary “what and how much” we are logging.

Hire Odoo Developer from Confianz Global

Confianz Global® is a software solutions company based in Charlotte, North Carolina. We specialize in providing development services to our clients. Operating from our headquarters in the United States, we take pride in offering highly skilled and dedicated Odoo developers and Odoo Functional Consultants to cater to the unique needs of our customers. Our team of proficient Odoo programmers has successfully implemented Odoo ERP solutions for a wide range of businesses, spanning various industries, both within the United States and internationally. Through our extensive experience in executing Odoo projects, we have garnered valuable insights and expertise in serving small and medium-sized businesses as well as large corporations.

We have a team of certified Odoo developers and consultants who can help you with all your Odoo ERP Customization and Odoo implementation needs. Call us today!

Talk to our experts now

    Talk To Our Experts Now