logging.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import os
  2. from datetime import datetime
  3. from django.conf import settings
  4. from django.core.files import File
  5. def set():
  6. if not os.path.exists(settings.MEDIA_ROOT):
  7. try:
  8. os.mkdir(settings.MEDIA_ROOT)
  9. except OSError:
  10. return
  11. if not os.path.exists(settings.MEDIA_ROOT+'/download'):
  12. try:
  13. os.mkdir(settings.MEDIA_ROOT+'/download')
  14. except OSError:
  15. return
  16. if not os.path.exists(settings.BASE_DIR + "/log"):
  17. try:
  18. os.mkdir(settings.BASE_DIR + "/log")
  19. except OSError:
  20. return
  21. if not os.path.exists(settings.BASE_DIR + "/log/message"):
  22. try:
  23. os.mkdir(settings.BASE_DIR + "/log/message")
  24. except OSError:
  25. return
  26. if not os.path.exists(settings.BASE_DIR + "/log/error"):
  27. try:
  28. os.mkdir(settings.BASE_DIR + "/log/error")
  29. except OSError:
  30. return
  31. if not os.path.exists(settings.BASE_DIR + "/log/log"):
  32. try:
  33. os.mkdir(settings.BASE_DIR + "/log/log")
  34. except OSError:
  35. return
  36. if not os.path.exists(settings.MEDIA_ROOT + "/tgbot"):
  37. try:
  38. os.mkdir(settings.MEDIA_ROOT + "/tgbot")
  39. except OSError:
  40. return
  41. def message(message):
  42. DirLogs = settings.BASE_DIR + "/log"
  43. if not os.path.exists(DirLogs):
  44. try:
  45. os.mkdir(DirLogs)
  46. except OSError:
  47. return
  48. DirLogs = settings.BASE_DIR + "/log/message"
  49. if not os.path.exists(DirLogs):
  50. try:
  51. os.mkdir(DirLogs)
  52. except OSError:
  53. return
  54. date = datetime.now()
  55. month = "0" if date.month < 10 else ""
  56. month += str(date.month)
  57. day = "0" if date.day < 10 else ""
  58. day += str(date.day)
  59. StrDate = "%s%s%s" % (str(date.year), month, day)
  60. file = open(DirLogs + '/message_' + StrDate + '.log', 'a')
  61. my_file = File(file)
  62. my_file.write("[%s]: %s\n" % (
  63. str(datetime.now().strftime("%d-%m-%Y %H:%M:%S")),
  64. str(message)
  65. ))
  66. my_file.closed
  67. file.closed
  68. def log(message):
  69. DirLogs = settings.BASE_DIR + "/log"
  70. if not os.path.exists(DirLogs):
  71. try:
  72. os.mkdir(DirLogs)
  73. except OSError:
  74. return
  75. DirLogs = settings.BASE_DIR + "/log/log"
  76. if not os.path.exists(DirLogs):
  77. try:
  78. os.mkdir(DirLogs)
  79. except OSError:
  80. return
  81. date = datetime.now()
  82. month = "0" if date.month < 10 else ""
  83. month += str(date.month)
  84. day = "0" if date.day < 10 else ""
  85. day += str(date.day)
  86. StrDate = "%s%s%s" % (str(date.year), month, day)
  87. file = open(DirLogs + '/message_' + StrDate + '.log', 'a')
  88. my_file = File(file)
  89. my_file.write("[%s]: %s\n" % (
  90. str(datetime.now().strftime("%d-%m-%Y %H:%M:%S")),
  91. str(message)))
  92. my_file.closed
  93. file.closed
  94. def error(message):
  95. DirLogs = settings.BASE_DIR + "/log"
  96. if not os.path.exists(DirLogs):
  97. try:
  98. os.mkdir(DirLogs)
  99. except OSError:
  100. return
  101. DirLogs = settings.BASE_DIR + "/log/error"
  102. if not os.path.exists(DirLogs):
  103. try:
  104. os.mkdir(DirLogs)
  105. except OSError:
  106. return
  107. date = datetime.now()
  108. month = "0" if date.month < 10 else ""
  109. month += str(date.month)
  110. day = "0" if date.day < 10 else ""
  111. day += str(date.day)
  112. StrDate = "%s%s%s" % (str(date.year), month, day)
  113. file = open(DirLogs + '/errors_' + StrDate + '.log', 'a')
  114. my_file = File(file)
  115. my_file.write("[%s]: %s\n" % (
  116. str(datetime.now().strftime("%d-%m-%Y %H:%M:%S")),
  117. str(message)))
  118. my_file.closed
  119. file.closed
  120. def check_dir():
  121. try:
  122. if not os.path.exists(settings.MEDIA_ROOT):
  123. try:
  124. os.mkdir(settings.MEDIA_ROOT)
  125. except OSError:
  126. logging.error(traceback.format_exc())
  127. return
  128. if not os.path.exists(settings.MEDIA_ROOT+"/att"):
  129. try:
  130. os.mkdir(settings.MEDIA_ROOT+"/att")
  131. except OSError:
  132. logging.error(traceback.format_exc())
  133. return
  134. if not os.path.exists(settings.MEDIA_ROOT+"/att/biophoto"):
  135. try:
  136. os.mkdir(settings.MEDIA_ROOT+"/att/biophoto")
  137. except OSError:
  138. logging.error(traceback.format_exc())
  139. return
  140. if not os.path.exists(settings.ATT_ROOT):
  141. try:
  142. os.mkdir(settings.ATT_ROOT)
  143. except OSError:
  144. logging.error(traceback.format_exc())
  145. return
  146. if not os.path.exists(settings.ATT_ROOT+"/USERPIC"):
  147. try:
  148. os.mkdir(settings.ATT_ROOT+"/USERPIC")
  149. except OSError:
  150. logging.error(traceback.format_exc())
  151. return
  152. except Exception as err:
  153. logging.error('%s\n%s' % (traceback.format_exc(), str(err)))