Logging is a tool that is used in Odoo for analyzing operations that are happening inside the odoo server when running the service. We can analyze errors or bugs clearly from the logs and we can find the best solutions for this. So logging is an important feature in Odoo for analyzing all the operations happening on our server.
Now we can check how we can add the log messages inside our custom modules.
Import logging
If we are using the logging option in our custom module then first we want to import a module logging from the python standard library. ie,
import logging
_logger = logging.getLogger(__name__)
Here initialized an object _logger by the name of our code file( __name__).
There are different levels for log messages.
DEBUG
_logger.debug is used for debugging.
WARNING
_logger.warning is used for showing the warnings.
INFO
_logger.info is used for showing the information
ERROR
_logger.error is used for showing the errors.
CRITICAL
_logger.critical is used for showing the critical messages.
Then we can describe these levels through an example.
from odoo import api, fields, models, _
import logging
_logger = logging.getLogger(__name__)
class HospitalManagement(models.Model):
_name = 'hospital.patient'
_description = 'Patient'
Here we imported logging and also initialized _logger by the name of code and created a new model of hospital management.
@api.onchange('doctor_id')
def onchange_doctor_id(self):
_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")
Inside the onchange function of a field, doctor_id add all the log messages. Then check the server log for seeing the log messages.
Here is the server log. hospital_patient is the python file that I added in the log messages and hospital_management is the corresponding module name. Here in this server log, we can see the whole path for the log.
Other logging options
We can also configure other logging options.
logfile = None // log file for storing the log details.
log_handler = LEVEL: LEVEL // we can set up the handler at a LEVEL for a given PREFIX.
For example, if we need to have a WARNING level for the module hospital_management only, then we can run as shown below:
log-handler=odoo.addons.hospital_management:WARNING
log_level = info //we can specify the logging level.
log_db_level = warning // We can specify the Logging database level.
log_db = False // We can Specify the Logging database.
These are the other options that can be configured in the configuration file.