Skip to content

Commit

Permalink
remove logging
Browse files Browse the repository at this point in the history
  • Loading branch information
w1png committed Dec 15, 2022
1 parent d343ba6 commit ee25eb4
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 60 deletions.
7 changes: 0 additions & 7 deletions installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,6 @@ def create_db():
print("Папка \"backups\" была удалена.")
mkdir("backups")
print("Папка \"backups\" была создана.")
if exists("logs"):
for file in listdir("logs"):
remove("logs/" + file)
rmdir("logs")
print("Папка \"logs\" была удалена.")
mkdir("logs")
print("Папка \"logs\" была создана.")
else:
print("Неверный ID главного администратора.")
else:
Expand Down
47 changes: 1 addition & 46 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from os.path import getsize, exists
from shutil import copyfile
import datetime
import logging

import markups
import state_handler
Expand All @@ -31,7 +30,6 @@
conn = sqlite3.connect("data.db")
c = conn.cursor()

logging.basicConfig(level=logging.INFO, format="%(asctime)s:%(levelname)s:%(message)s", filename=f"logs/{datetime.date.today().strftime('%d-%m-%Y')}.log")
settings = Settings()

storage = MemoryStorage()
Expand All @@ -48,7 +46,6 @@ def create_backup():
mkdir(folder_path)
copyfile("config.ini", folder_path + "/config.ini")
copyfile("data.db", folder_path + "/data.db")
logging.info("backup created")
print("Backup created!")

def clean_backups(days_ago=0):
Expand All @@ -60,18 +57,6 @@ def clean_backups(days_ago=0):
cleaned_size += getsize(file)
remove(file)
rmdir("backups/" + folder)
logging.info(f"backups cleaned ({'{:.2f}'.format(cleaned_size / 1048576)}mb)")
return cleaned_size / 1048576

def clean_logs():
cleaned_size = 0
for file in listdir("logs"):
if file == f"{datetime.date.today().strftime('%d-%m-%Y')}.log":
continue
cleaned_size = getsize(f"logs/{file}")
remove(f"logs/{file}")
logging.info(f"logs cleaned ({'{:.2f}'.format(cleaned_size / 1048576)}mb)")

return cleaned_size / 1048576

def clean_images():
Expand All @@ -80,7 +65,6 @@ def clean_images():
if file not in [item.get_image_id() for item in itm.get_item_list()]:
cleaned_size = getsize(f"images/{file}")
remove(f"images/{file}")
logging.info(f"unused images cleaned ({'{:.2f}'.format(cleaned_size / 1048576)}mb)")
return cleaned_size / 1048576


Expand All @@ -101,14 +85,12 @@ async def notify_admins(text):
text=text
)
except:
logging.warning(f"FAILED TO SEND TO [{user.get_id()}]")
if settings.is_debug():
print(f"FAILED TO SEND TO [{user.get_id()}]")


@dp.message_handler(commands=["start"])
async def welcome(message: types.Message):
logging.info(f"COMMAND [{message.chat.id}] {message.text}")
if settings.is_debug():
print(f"DEBUG: COMMAND [{message.chat.id}] {message.text}")
user = usr.User(message.chat.id)
Expand All @@ -127,7 +109,6 @@ async def welcome(message: types.Message):
else:
raise Exception
except:
logging.warning(f"FAILED TO SEND STICKER TO {message.chat.id}. sticker.tgs is probably missing in the bot's root folder.")
if settings.is_debug():
print(f"DEBUG: FAILED TO SEND STICKER TO {message.chat.id}. sticker.tgs is probably missing in the bot's root folder.")
await bot.send_message(
Expand All @@ -139,7 +120,6 @@ async def welcome(message: types.Message):

@dp.message_handler()
async def handle_text(message):
logging.info(f"MESSAGE [{message.chat.id}] {message.text}")
if settings.is_debug():
print(f"DEBUG: MESSAGE [{message.chat.id}] {message.text}")
user = usr.User(message.chat.id)
Expand Down Expand Up @@ -203,7 +183,6 @@ async def process_callback(callback_query: types.CallbackQuery):
call_data = callback_query.data
user = usr.User(chat_id)

logging.info(f"CALL [{chat_id}] {call_data}")
if settings.is_debug():
print(f"DEBUG: CALL [{chat_id}] {call_data}")

Expand Down Expand Up @@ -691,7 +670,6 @@ async def process_callback(callback_query: types.CallbackQuery):
)
except:
if settings.is_debug():
logging.warning(f"[{user.get_id()}] FAILED TO SEND MESSAGE TO [{editUser.get_id()}]")
print(f"DEBUG [{user.get_id()}] FAILED TO SEND MESSAGE TO [{editUser.get_id()}]")

await bot.edit_message_text(
Expand Down Expand Up @@ -723,7 +701,6 @@ async def process_callback(callback_query: types.CallbackQuery):
reply_markup=markupMain
)
except:
logging.warning(f"[{user.get_id()}] FAILED TO SEND MESSAGE TO [{editUser.get_id()}]")
if settings.is_debug():
print(f"DEBUG [{user.get_id()}] FAILED TO SEND MESSAGE TO [{editUser.get_id()}]")

Expand Down Expand Up @@ -1190,22 +1167,7 @@ async def process_callback(callback_query: types.CallbackQuery):
message_id=callback_query.message.message_id,
text=f"Неиспользуемые фотографии были успешно удалены!\nОчищено: {'{:.1f}'.format(clean_images())}мб",
reply_markup=markups.single_button(markups.btnBackSystemSettings)
)
elif call_data == "cleanLogsMenu":
await bot.edit_message_text(
chat_id=callback_query.message.chat.id,
message_id=callback_query.message.message_id,
text=tt.clean_logs_text,
reply_markup=markups.get_markup_cleanLogsMenu()
)
elif call_data == "cleanLogs":
await bot.edit_message_text(
chat_id=callback_query.message.chat.id,
message_id=callback_query.message.message_id,
text=f"Логи были успешно удалены!\nОчишено: {'{:.2f}'.format(clean_logs())}мб",
reply_markup=markups.single_button(markups.btnBackSystemSettings)
)

)
elif call_data == "cleanDatabaseMenu":
await bot.edit_message_text(
chat_id=callback_query.message.chat.id,
Expand Down Expand Up @@ -1265,7 +1227,6 @@ async def process_callback(callback_query: types.CallbackQuery):
try:
copyfile(f"{backup_path}/{file}", f"{getcwd()}/{file}")
except:
logging.error(f"Failed to copy \"{file}\" to \".\"")
if settings.is_debug():
print(f"DEBUG: Failed to copy \"{file}\" to \".\"")
text = tt.load_backup + f"\nРезервная копия за {call_data[10:]} была успешно загружена!"
Expand Down Expand Up @@ -1822,7 +1783,6 @@ async def editItemSetPrice(message: types.Message, state: FSMContext):
chat_id=message.chat.id
)
except:
logging.warning(f"[{message.chat.id}] FAILED TO DELETE MESSAGE WITH ID {data['state_message']}")
if settings.is_debug():
print(f"DEBUG: [{message.chat.id}] FAILED TO DELETE MESSAGE WITH ID {data['state_message']}")
await bot.send_message(
Expand Down Expand Up @@ -1888,7 +1848,6 @@ async def editItemSetDesc(message: types.Message, state: FSMContext):
chat_id=message.chat.id
)
except:
logging.warning(f"[{message.chat.id}] FAILED TO DELETE MESSAGE WITH ID {data['state_message']}")
if settings.is_debug():
print(f"DEBUG: [{message.chat.id}] FAILED TO DELETE MESSAGE WITH ID {data['state_message']}")
await bot.send_message(
Expand All @@ -1914,7 +1873,6 @@ async def editItemSetName(message: types.Message, state: FSMContext):
chat_id=message.chat.id
)
except:
logging.warning(f"[{message.chat.id}] FAILED TO DELETE MESSAGE WITH ID {data['state_message']}")
if settings.is_debug():
print(f"DEBUG: [{message.chat.id}] FAILED TO DELETE MESSAGE WITH ID {data['state_message']}")
await bot.send_message(
Expand Down Expand Up @@ -1942,7 +1900,6 @@ async def editItemStockSetStock(message: types.Message, state: FSMContext):
chat_id=message.chat.id
)
except:
logging.warning(f"[{message.chat.id}] FAILED TO DELETE MESSAGE WITH ID {data['state_message']}")
if settings.is_debug():
print(f"DEBUG: [{message.chat.id}] FAILED TO DELETE MESSAGE WITH ID {data['state_message']}")
await bot.send_message(
Expand Down Expand Up @@ -2260,7 +2217,6 @@ async def cancelState(callback_query: types.CallbackQuery, state: FSMContext):
data = await state.get_data()
user = usr.User(callback_query.message.chat.id)

logging.info(f"CALL [{chat_id}] {call_data} (STATE)")
if settings.is_debug():
print(f"DEBUG: CALL [{chat_id}] {call_data} (STATE)")

Expand Down Expand Up @@ -2514,7 +2470,6 @@ async def cancelState(callback_query: types.CallbackQuery, state: FSMContext):
reply_markup=markups.get_markup_seeOrder(order)
)
except:
logging.warning(f"FAIL MESSAGE TO [{user.get_id()}]")
if settings.is_debug():
print(f"DEBUG: FAIL MESSAGE TO [{user.get_id()}]")
except:
Expand Down
7 changes: 0 additions & 7 deletions src/markups.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ def get_markup_systemSettings():
markup.add(types.InlineKeyboardButton(text=tt.clean_images, callback_data="admin_cleanImagesMenu"))
markup.add(types.InlineKeyboardButton(text=tt.reset_settings, callback_data="admin_resetSettingsMenu"))
markup.add(types.InlineKeyboardButton(text=tt.clean_database, callback_data="admin_cleanDatabaseMenu"))
markup.add(types.InlineKeyboardButton(text=tt.clean_logs, callback_data="admin_cleanLogsMenu"))
markup.add(types.InlineKeyboardButton(text=tt.backups, callback_data="admin_backups"))
markup.add(types.InlineKeyboardButton(text=tt.disable_debug if settings.is_debug() else tt.enable_debug, callback_data="admin_changeEnableDebug"))
markup.add(btnBackAdditionalSettings)
Expand Down Expand Up @@ -417,12 +416,6 @@ def get_markup_cleanBackupsMenu():
markup.add(btnBackBackups)
return markup

def get_markup_cleanLogsMenu():
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton(text=tt.delete, callback_data="admin_cleanLogs"))
markup.add(btnBackSystemSettings)
return markup

def get_markup_cleanImagesMenu():
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton(text=tt.delete, callback_data="admin_cleanImages"))
Expand Down

0 comments on commit ee25eb4

Please sign in to comment.