repcofkk/app/sabron/util/logging.py

258 lines
7.3 KiB
Python

import os
import json
from datetime import datetime
from django.conf import settings
from django.core.files import File
def set():
if not os.path.exists(settings.MEDIA_ROOT):
try:
os.mkdir(settings.MEDIA_ROOT)
except OSError:
return
if not os.path.exists(settings.MEDIA_ROOT + "/download"):
try:
os.mkdir(settings.MEDIA_ROOT + "/download")
except OSError:
return
if not os.path.exists(settings.BASE_DIR + "/log"):
try:
os.mkdir(settings.BASE_DIR + "/log")
except OSError:
return
if not os.path.exists(settings.BASE_DIR + "/log/message"):
try:
os.mkdir(settings.BASE_DIR + "/log/message")
except OSError:
return
if not os.path.exists(settings.BASE_DIR + "/log/error"):
try:
os.mkdir(settings.BASE_DIR + "/log/error")
except OSError:
return
if not os.path.exists(settings.BASE_DIR + "/log/log"):
try:
os.mkdir(settings.BASE_DIR + "/log/log")
except OSError:
return
if not os.path.exists(settings.MEDIA_ROOT + "/tgbot"):
try:
os.mkdir(settings.MEDIA_ROOT + "/tgbot")
except OSError:
return
def message(message):
DirLogs = settings.BASE_DIR + "/log"
if not os.path.exists(DirLogs):
try:
os.mkdir(DirLogs)
except OSError:
return
DirLogs = settings.BASE_DIR + "/log/message"
if not os.path.exists(DirLogs):
try:
os.mkdir(DirLogs)
except OSError:
return
date = datetime.now()
month = "0" if date.month < 10 else ""
month += str(date.month)
day = "0" if date.day < 10 else ""
day += str(date.day)
StrDate = "%s%s%s" % (str(date.year), month, day)
file = open(DirLogs + "/message_" + StrDate + ".log", "a")
my_file = File(file)
my_file.write(
"[%s]: %s\n" % (str(datetime.now().strftime("%d-%m-%Y %H:%M:%S")), str(message))
)
my_file.closed
file.closed
def log(message):
DirLogs = settings.BASE_DIR + "/log"
if not os.path.exists(DirLogs):
try:
os.mkdir(DirLogs)
except OSError:
return
DirLogs = settings.BASE_DIR + "/log/log"
if not os.path.exists(DirLogs):
try:
os.mkdir(DirLogs)
except OSError:
return
date = datetime.now()
month = "0" if date.month < 10 else ""
month += str(date.month)
day = "0" if date.day < 10 else ""
day += str(date.day)
StrDate = "%s%s%s" % (str(date.year), month, day)
file = open(DirLogs + "/message_" + StrDate + ".log", "a")
my_file = File(file)
my_file.write(
"[%s]: %s\n" % (str(datetime.now().strftime("%d-%m-%Y %H:%M:%S")), str(message))
)
my_file.closed
file.closed
def log_save(message, mystr):
name = "test_api_tags.st"
DirLogs = settings.BASE_DIR + "/log"
if not os.path.exists(DirLogs):
try:
os.mkdir(DirLogs)
except OSError:
return
DirLogs = settings.BASE_DIR + "/log/log"
if not os.path.exists(DirLogs):
try:
os.mkdir(DirLogs)
except OSError:
return
if not os.path.exists(DirLogs + name):
file = open(DirLogs + name, "a")
my_file = File(file)
my_file.write(message)
my_file.closed
file.closed
return
with open(DirLogs + name) as json_file:
data = json.load(json_file)
for dat in data["items"]:
f = False
for tag in mystr["items"]:
if tag["sn"] == dat["sn"]:
f = True
break
if f == False:
log(" tag not found [%s]" % dat["sn"])
def error(message):
DirLogs = settings.BASE_DIR + "/log"
if not os.path.exists(DirLogs):
try:
os.mkdir(DirLogs)
except OSError:
return
DirLogs = settings.BASE_DIR + "/log/error"
if not os.path.exists(DirLogs):
try:
os.mkdir(DirLogs)
except OSError:
return
date = datetime.now()
month = "0" if date.month < 10 else ""
month += str(date.month)
day = "0" if date.day < 10 else ""
day += str(date.day)
StrDate = "%s%s%s" % (str(date.year), month, day)
file = open(DirLogs + "/errors_" + StrDate + ".log", "a")
my_file = File(file)
my_file.write(
"[%s]: %s\n" % (str(datetime.now().strftime("%d-%m-%Y %H:%M:%S")), str(message))
)
my_file.closed
file.closed
def check_dir():
try:
if not os.path.exists(settings.MEDIA_ROOT):
try:
os.mkdir(settings.MEDIA_ROOT)
except OSError:
logging.error(traceback.format_exc())
return
if not os.path.exists(settings.MEDIA_ROOT + "/att"):
try:
os.mkdir(settings.MEDIA_ROOT + "/att")
except OSError:
logging.error(traceback.format_exc())
return
if not os.path.exists(settings.MEDIA_ROOT + "/att/biophoto"):
try:
os.mkdir(settings.MEDIA_ROOT + "/att/biophoto")
except OSError:
logging.error(traceback.format_exc())
return
if not os.path.exists(settings.ATT_ROOT):
try:
os.mkdir(settings.ATT_ROOT)
except OSError:
logging.error(traceback.format_exc())
return
if not os.path.exists(settings.ATT_ROOT + "/USERPIC"):
try:
os.mkdir(settings.ATT_ROOT + "/USERPIC")
except OSError:
logging.error(traceback.format_exc())
return
except Exception as err:
logging.error("%s\n%s" % (traceback.format_exc(), str(err)))
def requestlog(request):
meta = request.META
if settings.DEBUG == True:
meta.update(HTTP_X_REAL_IP="127.0.0.1")
message = (
"["
+ meta["HTTP_X_REAL_IP"]
+ "] : "
+ meta["QUERY_STRING"]
+ "\n"
+ str(request.headers).replace("', '", "'\n '")
+ "\n"
)
message = message + "{\n"
for mt in meta:
try:
message = message + "'" + str(mt) + "':'" + str(meta[mt]) + "'\n"
except Exception as err:
error("Error : updateMeta" + str(err))
message = message + "\n}"
DirLogs = settings.BASE_DIR + "/log"
if os.path.exists(DirLogs) == False:
try:
os.mkdir(DirLogs)
except OSError:
return
DirLogs = settings.BASE_DIR + "/log/reques"
if os.path.exists(DirLogs) == False:
try:
os.mkdir(DirLogs)
except OSError:
return
date = datetime.now()
if date.month < 10:
month = "0" + str(date.month)
else:
month = str(date.month)
if date.day < 10:
day = "0" + str(date.day)
else:
day = str(date.day)
StrDate = str(date.year) + month + day
file = open(DirLogs + "/reques_" + StrDate + ".log", "a")
my_file = File(file)
my_file.write(
str(datetime.now().strftime("%d-%m-%Y %H:%M:%S")) + ": " + str(message) + "\n"
)
my_file.write(
"=============================================================================\n"
)
my_file.closed
file.closed