logging_2.py 823 B

1234567891011121314151617181920212223242526272829
  1. def message(message):
  2. DirLogs = settings.BASE_DIR + "/log"
  3. if not os.path.exists(DirLogs):
  4. try:
  5. os.mkdir(DirLogs)
  6. except OSError:
  7. return
  8. DirLogs = settings.BASE_DIR + "/log/message"
  9. if not os.path.exists(DirLogs):
  10. try:
  11. os.mkdir(DirLogs)
  12. except OSError:
  13. return
  14. date = datetime.now()
  15. month = "0" if date.month < 10 else ""
  16. month += str(date.month)
  17. day = "0" if date.day < 10 else ""
  18. day += str(date.day)
  19. StrDate = "%s%s%s" % (str(date.year), month, day)
  20. file = open(DirLogs + '/message_' + StrDate + '.log', 'a')
  21. my_file = File(file)
  22. my_file.write("[%s]: %s\n" % (
  23. str(datetime.now().strftime("%d-%m-%Y %H:%M:%S")),
  24. str(message)
  25. ))
  26. my_file.closed
  27. file.closed