How do I properly set up logging

Frappe v15 and ERPNext v15

I am relatively new to Frappe development, and the one who worked on the project before me did a pretty poor job of managing logs, using print statements everywhere. And now a bug happened in the prod environment of our custom ERPNext app and I am unable to figure out where in the code it happened because there are no logs. So I was tasked to solve the bug and, on the way add proper logging (errors, info, warning, debug).
What I did was just adding :

# Info
frappe.logger().info("Some logs")

# Error
except Exception as e:
            frappe.logger().error(f"Erreur lors de l'analyse des données: {str(e)}", exc_info=True)

# Warning
frappe.logger().warning("Unauthorized access attempt to reject_request")

But then I couldn’t find them on the “bench start” logs nor in any of the bench log files (bench/logs/*log) nor in any site-specific log file.

Did I do something the wrong way or is there any specific configuration I need to do ?

I would recommend you should use frappe.log_error() instead of logger.

Because log file will save in production server , you need to access server and then need to read file it is too complicate to debug.

Example:

def test():
    frappe.log_error(title="Test Error", message="This is a test error log")

1 Like

Thank you, I’ll try this one

Thank you very much, it worked fine, for the error logs.
But do you have any tips about the other log levels ?

Hi,

You can start with something like this:

logger = frappe.logger("import", allow_site=True, file_count=5, max_size=250000)
logger.setLevel("INFO")
logger.info("My first log")

docs: Logging

Thank you so so much, you just saved me