Skip to content

Commit 0e5d5ec

Browse files
Yifang DengYifang Deng
authored andcommitted
refactor: replace print statements with logging for better error tracking
1 parent 6fedd72 commit 0e5d5ec

34 files changed

+171
-103
lines changed

backend/open_webui/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def filter(self, record: logging.LogRecord) -> bool:
4444

4545
# Function to run the alembic migrations
4646
def run_migrations():
47-
print("Running migrations")
47+
log.info("Running migrations")
4848
try:
4949
from alembic import command
5050
from alembic.config import Config
@@ -57,7 +57,7 @@ def run_migrations():
5757

5858
command.upgrade(alembic_cfg, "head")
5959
except Exception as e:
60-
print(f"Error: {e}")
60+
log.exception(f"Error running migrations: {e}")
6161

6262

6363
run_migrations()
@@ -1094,7 +1094,7 @@ class BannerModel(BaseModel):
10941094
banners = json.loads(os.environ.get("WEBUI_BANNERS", "[]"))
10951095
banners = [BannerModel(**banner) for banner in banners]
10961096
except Exception as e:
1097-
print(f"Error loading WEBUI_BANNERS: {e}")
1097+
log.exception(f"Error loading WEBUI_BANNERS: {e}")
10981098
banners = []
10991099

11001100
WEBUI_BANNERS = PersistentConfig("WEBUI_BANNERS", "ui.banners", banners)

backend/open_webui/models/chats.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import logging
12
import json
23
import time
34
import uuid
45
from typing import Optional
56

67
from open_webui.internal.db import Base, get_db
78
from open_webui.models.tags import TagModel, Tag, Tags
8-
9+
from open_webui.env import SRC_LOG_LEVELS
910

1011
from pydantic import BaseModel, ConfigDict
1112
from sqlalchemy import BigInteger, Boolean, Column, String, Text, JSON
@@ -16,6 +17,8 @@
1617
# Chat DB Schema
1718
####################
1819

20+
log = logging.getLogger(__name__)
21+
log.setLevel(SRC_LOG_LEVELS["MODELS"])
1922

2023
class Chat(Base):
2124
__tablename__ = "chat"
@@ -670,7 +673,7 @@ def get_chats_by_user_id_and_search_text(
670673
# Perform pagination at the SQL level
671674
all_chats = query.offset(skip).limit(limit).all()
672675

673-
print(len(all_chats))
676+
log.info(f"The number of chats: {len(all_chats)}")
674677

675678
# Validate and return chats
676679
return [ChatModel.model_validate(chat) for chat in all_chats]
@@ -731,7 +734,7 @@ def get_chat_list_by_user_id_and_tag_name(
731734
query = db.query(Chat).filter_by(user_id=user_id)
732735
tag_id = tag_name.replace(" ", "_").lower()
733736

734-
print(db.bind.dialect.name)
737+
log.info(f"DB dialect name: {db.bind.dialect.name}")
735738
if db.bind.dialect.name == "sqlite":
736739
# SQLite JSON1 querying for tags within the meta JSON field
737740
query = query.filter(
@@ -752,7 +755,7 @@ def get_chat_list_by_user_id_and_tag_name(
752755
)
753756

754757
all_chats = query.all()
755-
print("all_chats", all_chats)
758+
log.debug(f"all_chats: {all_chats}")
756759
return [ChatModel.model_validate(chat) for chat in all_chats]
757760

758761
def add_chat_tag_by_id_and_user_id_and_tag_name(
@@ -810,7 +813,7 @@ def count_chats_by_tag_name_and_user_id(self, tag_name: str, user_id: str) -> in
810813
count = query.count()
811814

812815
# Debugging output for inspection
813-
print(f"Count of chats for tag '{tag_name}':", count)
816+
log.info(f"Count of chats for tag '{tag_name}': {count}")
814817

815818
return count
816819

backend/open_webui/models/feedbacks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def insert_new_feedback(
118118
else:
119119
return None
120120
except Exception as e:
121-
print(e)
121+
log.exception(f"Error creating a new feedback: {e}")
122122
return None
123123

124124
def get_feedback_by_id(self, id: str) -> Optional[FeedbackModel]:

backend/open_webui/models/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def insert_new_file(self, user_id: str, form_data: FileForm) -> Optional[FileMod
119119
else:
120120
return None
121121
except Exception as e:
122-
print(f"Error creating tool: {e}")
122+
log.exception(f"Error inserting a new file: {e}")
123123
return None
124124

125125
def get_file_by_id(self, id: str) -> Optional[FileModel]:

backend/open_webui/models/folders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def insert_new_folder(
8282
else:
8383
return None
8484
except Exception as e:
85-
print(e)
85+
log.exception(f"Error inserting a new folder: {e}")
8686
return None
8787

8888
def get_folder_by_id_and_user_id(

backend/open_webui/models/functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def insert_new_function(
105105
else:
106106
return None
107107
except Exception as e:
108-
print(f"Error creating tool: {e}")
108+
log.exception(f"Error creating a new function: {e}")
109109
return None
110110

111111
def get_function_by_id(self, id: str) -> Optional[FunctionModel]:
@@ -170,7 +170,7 @@ def get_function_valves_by_id(self, id: str) -> Optional[dict]:
170170
function = db.get(Function, id)
171171
return function.valves if function.valves else {}
172172
except Exception as e:
173-
print(f"An error occurred: {e}")
173+
log.exception(f"Error getting function valves by id {id}: {e}")
174174
return None
175175

176176
def update_function_valves_by_id(
@@ -202,7 +202,7 @@ def get_user_valves_by_id_and_user_id(
202202

203203
return user_settings["functions"]["valves"].get(id, {})
204204
except Exception as e:
205-
print(f"An error occurred: {e}")
205+
log.exception(f"Error getting user values by id {id} and user id {user_id}: {e}")
206206
return None
207207

208208
def update_user_valves_by_id_and_user_id(
@@ -225,7 +225,7 @@ def update_user_valves_by_id_and_user_id(
225225

226226
return user_settings["functions"]["valves"][id]
227227
except Exception as e:
228-
print(f"An error occurred: {e}")
228+
log.exception(f"Error updating user valves by id {id} and user_id {user_id}: {e}")
229229
return None
230230

231231
def update_function_by_id(self, id: str, updated: dict) -> Optional[FunctionModel]:

backend/open_webui/models/models.py

100644100755
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def insert_new_model(
166166
else:
167167
return None
168168
except Exception as e:
169-
print(e)
169+
log.exception(f"Failed to insert a new model: {e}")
170170
return None
171171

172172
def get_all_models(self) -> list[ModelModel]:
@@ -246,8 +246,7 @@ def update_model_by_id(self, id: str, model: ModelForm) -> Optional[ModelModel]:
246246
db.refresh(model)
247247
return ModelModel.model_validate(model)
248248
except Exception as e:
249-
print(e)
250-
249+
log.exception(f"Failed to update the model by id {id}: {e}")
251250
return None
252251

253252
def delete_model_by_id(self, id: str) -> bool:

backend/open_webui/models/tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def insert_new_tag(self, name: str, user_id: str) -> Optional[TagModel]:
6161
else:
6262
return None
6363
except Exception as e:
64-
print(e)
64+
log.exception(f"Error inserting a new tag: {e}")
6565
return None
6666

6767
def get_tag_by_name_and_user_id(

backend/open_webui/models/tools.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def insert_new_tool(
131131
else:
132132
return None
133133
except Exception as e:
134-
print(f"Error creating tool: {e}")
134+
log.exception(f"Error creating a new tool: {e}")
135135
return None
136136

137137
def get_tool_by_id(self, id: str) -> Optional[ToolModel]:
@@ -175,7 +175,7 @@ def get_tool_valves_by_id(self, id: str) -> Optional[dict]:
175175
tool = db.get(Tool, id)
176176
return tool.valves if tool.valves else {}
177177
except Exception as e:
178-
print(f"An error occurred: {e}")
178+
log.exception(f"Error getting tool valves by id {id}: {e}")
179179
return None
180180

181181
def update_tool_valves_by_id(self, id: str, valves: dict) -> Optional[ToolValves]:
@@ -204,7 +204,7 @@ def get_user_valves_by_id_and_user_id(
204204

205205
return user_settings["tools"]["valves"].get(id, {})
206206
except Exception as e:
207-
print(f"An error occurred: {e}")
207+
log.exception(f"Error getting user values by id {id} and user_id {user_id}: {e}")
208208
return None
209209

210210
def update_user_valves_by_id_and_user_id(
@@ -227,7 +227,7 @@ def update_user_valves_by_id_and_user_id(
227227

228228
return user_settings["tools"]["valves"][id]
229229
except Exception as e:
230-
print(f"An error occurred: {e}")
230+
log.exception(f"Error updating user valves by id {id} and user_id {user_id}: {e}")
231231
return None
232232

233233
def update_tool_by_id(self, id: str, updated: dict) -> Optional[ToolModel]:

backend/open_webui/retrieval/models/colbert.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import os
2+
import logging
23
import torch
34
import numpy as np
45
from colbert.infra import ColBERTConfig
56
from colbert.modeling.checkpoint import Checkpoint
67

8+
from open_webui.env import SRC_LOG_LEVELS
9+
10+
log = logging.getLogger(__name__)
11+
log.setLevel(SRC_LOG_LEVELS["RAG"])
12+
713

814
class ColBERT:
915
def __init__(self, name, **kwargs) -> None:
10-
print("ColBERT: Loading model", name)
16+
log.info("ColBERT: Loading model", name)
1117
self.device = "cuda" if torch.cuda.is_available() else "cpu"
1218

1319
DOCKER = kwargs.get("env") == "docker"

0 commit comments

Comments
 (0)