Hi guys,
In this tutorial I will learn you how to log in Odoo. Logging can be very handy to see what actually happens when you do something, to show warnings in the terminal or to notify for extreme problems in the logfile.
1. Importing logging
The first thing that you need to do is to import the logger in your python file. This library is not something from Odoo but this is simply a python library for logging that works in Odoo. Add the import statement at the top of your python file and then call the logger.
1 2 3 4 |
#Import logger import logging #Get the logger _logger = logging.getLogger(__name__) |
2. Using logging to print/log statements
After importing logging you can directly start writing log statements. There are five different states for logging pre-built:
- logger.debug: for debugging purposes.
- logger.info: for showing information.
- logger.warning: for showing minor issues.
- logger.error: for showing that an operation has failed
- logger.critical: for showing that something has gone horribly wrong. In Odoo these will be used to notify a module doesn’t work anymore.
You can use logging anywhere in Python and at any time as you wish. In this tutorial I will create a simple field and button. When the button is printed I will print all kind of pre-built logging states to the terminal for demo purposes. I’ve created a button that calls the function print_log_data:
1 2 3 4 5 6 7 8 9 |
def print_log_data(self, cr, uid, ids,record,context=None): #These are all the logging types. _logger.debug("Use _logger.debug for debugging purposes, nothing else.") _logger.info("Use _logger.info for information messages. This is used for notifying about something important.") _logger.warning("Use _logger.warning for minor issues, which will not crash your module.") _logger.error("Use _logger.error to report a failed operation.") _logger.critical("Use _logger.critical for critical message: when this goes off the module will not work anymore.") #Want to include data from your field? Pass it along in the context, get it from the pool or from the dict. _logger.critical("The name '" + str(record.get('name')) + "' is not okay for us!") |
As you can see logging is very simple in Odoo. Choose what kind of logging it is and then simply add a text to it. if you want to use variables from your view you can simply call them in Python and add them to the message. For example:
1 |
logger.critical("The name '" + str(record.get('name')) + "' is not okay for us!") |
When you click on the button the logging statements will be printed in your terminal. For example with my function:
Thats it! It is as simple as that to log data in Odoo. If you want to go further though, there are quite some extra options.
3. Advanced logging configuration
Odoo goes a bit further than this though! You can configure multiple logging options in Odoo:
- logfile: the log filename. If this is not filled in all the log details will be written to stdout (terminal).
- log_level: any value in the list of [‘debug_rpc_answer’, ‘debug_rpc’, ‘debug’, ‘debug_sql’, ‘info’, ‘warn’, ‘error’, ‘critical’]. Odoo changed the log_level meaning here because these level values are mapped to a set of predefined ‘module:log_level’ pairs.
- log_handler: can be a list of ‘module:log_level’ pairs. The default value is ‘:INFO’ — it means the default logging level is ‘INFO’ for all modules.
- logrotate: True/False. If True, create a daily log file and keep 30 files.
- log_db: True/False. If this is set to ‘True’ the logging will also be written to the ‘ir_logging’ table in the database.
For example if I define a logfile in the terminal:
When you now go to this location you will see there is a new logfile created and that it is automatically filled with the data that you defined:
Do you want to see the source code or try this module for yourself? You can download / view it on Github.
Has this tutorial helped you, do you have any feedback or questions? Post away!