You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

71 line
1.7 KiB

  1. from __future__ import unicode_literals
  2. import frappe
  3. import logging
  4. import logging.config
  5. import os
  6. import json
  7. from pprint import pformat
  8. class ContextFilter(logging.Filter):
  9. """
  10. This is a filter which injects request information (if available) into the log.
  11. """
  12. def filter(self, record):
  13. record.form_dict = pformat(getattr(frappe.local, 'form_dict', None))
  14. record.site = getattr(frappe.local, 'site', None)
  15. record.tb = frappe.utils.get_traceback()
  16. return True
  17. def setup_logging():
  18. conf = frappe.get_site_config(sites_path=os.environ.get('SITES_PATH', '.'))
  19. if conf.logging_conf:
  20. logging_conf = conf.logging_conf
  21. else:
  22. logging_conf = {
  23. "version": 1,
  24. "disable_existing_loggers": True,
  25. "level": logging.DEBUG,
  26. "filters": {
  27. "context_filter": {
  28. "()": "frappe.setup_logging.ContextFilter"
  29. }
  30. },
  31. "formatters": {
  32. "site_wise": {
  33. "format": "\n%(asctime)s %(message)s \n site: %(site)s\n form: %(form_dict)s\n\n%(tb)s\n--------------"
  34. }
  35. },
  36. "loggers": {
  37. "frappe": {
  38. "level": "INFO",
  39. "propagate": False,
  40. "filters": ["context_filter"],
  41. "handlers": ["request_exception"]
  42. },
  43. "rq.worker": {
  44. "level": "DEBUG",
  45. }
  46. },
  47. "handlers": {
  48. "request_exception": {
  49. "level": "ERROR",
  50. "formatter": "site_wise",
  51. "class": "logging.StreamHandler",
  52. }
  53. }
  54. }
  55. if conf.request_exception_log_file:
  56. logging_conf.update({
  57. "handlers": {
  58. "request_exception": {
  59. "level": "ERROR",
  60. "formatter": "site_wise",
  61. "class": "logging.handlers.WatchedFileHandler",
  62. "filename": conf.request_exception_log_file
  63. }
  64. }
  65. })
  66. logging.config.dictConfig(logging_conf)