Skip to content

Commit 325a2b1

Browse files
authored
Merge pull request #676 from transformerlab/fix/logging-print
Change all incorrect logging statements in jobs router to print
2 parents d2dd64c + 41df212 commit 325a2b1

File tree

5 files changed

+46
-62
lines changed

5 files changed

+46
-62
lines changed

transformerlab/routers/data.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,19 @@
3030

3131
from jinja2 import Environment
3232
from jinja2.sandbox import SandboxedEnvironment
33-
import logging
3433
from transformerlab.services import dataset_service as dataset_service_module
3534

3635

3736
jinja_environment = Environment()
3837
sandboxed_jinja2_environment = SandboxedEnvironment()
3938

40-
logging.basicConfig(level=logging.ERROR)
41-
42-
43-
# Configure logging
44-
4539

4640
def log(msg):
4741
global_log_path = get_global_log_path()
4842
with open(global_log_path, "a") as f:
4943
f.write(msg + "\n")
5044

5145

52-
# logging.basicConfig(filename=GLOBAL_LOG_PATH, level=logging.INFO,
53-
# format='%(asctime)s - %(levelname)s - %(message)s')
54-
55-
5646
router = APIRouter(prefix="/data", tags=["datasets"])
5747

5848
# Get list of datasets that we have in our hardcoded gallery
@@ -193,7 +183,7 @@ async def dataset_preview(
193183
else:
194184
dataset = load_dataset(dataset_id, trust_remote_code=True, streaming=streaming)
195185
except Exception as e:
196-
logging.error(f"Exception occurred: {type(e).__name__}: {e}")
186+
print(f"Exception occurred: {type(e).__name__}: {e}")
197187
return {"status": "error", "message": "An internal error has occurred."}
198188

199189
if split is None or split == "":
@@ -335,7 +325,7 @@ async def load_and_slice_dataset(dataset_id: str, offset: int, limit: int):
335325
try:
336326
dataset = dataset_service_module.load_local_dataset(dirs.dataset_dir_by_id(dataset_id))
337327
except Exception as e:
338-
logging.error(f"Error loading dataset: {type(e).__name__}: {e}")
328+
print(f"Error loading dataset: {type(e).__name__}: {e}")
339329
return {"status": "error", "message": "An internal error has occurred."}
340330
dataset_len = len(dataset["train"])
341331
result["columns"] = dataset["train"][offset : min(offset + limit, dataset_len)]
@@ -492,7 +482,7 @@ async def dataset_edit_with_template(
492482
else:
493483
continue
494484
except Exception as e:
495-
logging.error(f"Failed to read metadata from {metadata_path}: {e}")
485+
print(f"Failed to read metadata from {metadata_path}: {e}")
496486
return {"status": "error", "message": "Failed to read metadata file!"}
497487

498488
for entry in data:
@@ -523,7 +513,7 @@ async def dataset_edit_with_template(
523513
encoded_img = base64.b64encode(buffer.getvalue()).decode("utf-8")
524514
image_data_url = f"data:image/jpeg;base64,{encoded_img}"
525515
except Exception as e:
526-
logging.error(f"Failed to process image {image_path}: {e}")
516+
print(f"Failed to process image {image_path}: {e}")
527517
return {"status": "error", "message": "Failed to process images!"}
528518

529519
row = dict(entry) # Start with all metadata fields
@@ -583,7 +573,7 @@ async def save_metadata(dataset_id: str, new_dataset_id: str, file: UploadFile):
583573
try:
584574
updates = json.loads(updates_raw.decode("utf-8"))
585575
except Exception as e:
586-
logging.error(f"Invalid JSON file: {e}")
576+
print(f"Invalid JSON file: {e}")
587577
return {"status": "error", "message": "Invalid JSON file!"}
588578

589579
# Scan source metadata
@@ -627,7 +617,7 @@ async def save_metadata(dataset_id: str, new_dataset_id: str, file: UploadFile):
627617
"metadata_root": root,
628618
}
629619
except Exception as e:
630-
logging.error(f"Error reading metadata {metadata_path}: {e}")
620+
print(f"Error reading metadata {metadata_path}: {e}")
631621
return {"status": "error", "message": "Failed to read metadata!"}
632622

633623
metadata_accumulator = {}
@@ -660,7 +650,7 @@ async def save_metadata(dataset_id: str, new_dataset_id: str, file: UploadFile):
660650
try:
661651
shutil.copy2(source_path, dest_path)
662652
except Exception as e:
663-
logging.error(f"Failed to copy {source_path} to {dest_path}: {e}")
653+
print(f"Failed to copy {source_path} to {dest_path}: {e}")
664654
return {"status": "error", "message": "Failed to copy from source to destination"}
665655

666656
# Prepare metadata entry
@@ -687,7 +677,7 @@ async def save_metadata(dataset_id: str, new_dataset_id: str, file: UploadFile):
687677
full_entry = {col: entry.get(col, "") for col in all_columns}
688678
f.write(json.dumps(full_entry) + "\n")
689679
except Exception as e:
690-
logging.error(f"Failed to write metadata file {metadata_file}: {e}")
680+
print(f"Failed to write metadata file {metadata_file}: {e}")
691681
return {"status": "error", "message": "Failed to write metadata file!"}
692682

693683
result = await dataset_new(dataset_id=new_dataset_id, generated=False)
@@ -778,7 +768,7 @@ async def dataset_download(dataset_id: str, config_name: str = None):
778768
)
779769
log(f"Dataset created in filesystem for dataset_id: {dataset_id}")
780770
except Exception as e:
781-
logging.error(f"Failed to write dataset metadata to SDK store: {type(e).__name__}: {e}")
771+
print(f"Failed to write dataset metadata to SDK store: {type(e).__name__}: {e}")
782772

783773
# Download the dataset
784774
# Later on we can move this to a job
@@ -892,7 +882,7 @@ async def dataset_new(dataset_id: str, generated: bool = False):
892882
json_data={"generated": True} if generated else {},
893883
)
894884
except Exception as e:
895-
logging.error(f"Failed to write dataset metadata to SDK store: {type(e).__name__}: {e}")
885+
print(f"Failed to write dataset metadata to SDK store: {type(e).__name__}: {e}")
896886
return {"status": "success", "dataset_id": dataset_id}
897887

898888

transformerlab/routers/evals.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,5 @@ async def compare_eval(job_list: str = ""):
8787
return JSONResponse(content=combined.to_json(orient="records"), media_type="application/json")
8888

8989
except Exception:
90-
import logging
91-
92-
logging.error("An error occurred while comparing evaluations", exc_info=True)
90+
print("An error occurred while comparing evaluations")
9391
return {"error": "An internal error has occurred. Please try again later."}

transformerlab/routers/experiment/export.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,8 @@ async def run_exporter_script(
167167
}
168168

169169
except Exception as e:
170-
import logging
171170

172-
logging.error(f"Failed to export model. Exception: {e}")
171+
print(f"Failed to export model. Exception: {e}")
173172
job = job_get(job_id)
174173
experiment_id = job["experiment_id"]
175174
await job_update_status(job_id=job_id, status="FAILED", experiment_id=experiment_id)

transformerlab/routers/experiment/jobs.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import csv
66
import pandas as pd
7-
import logging
87
from fastapi import APIRouter, Body, Response, Request
98
from fastapi.responses import StreamingResponse, FileResponse
109

@@ -169,7 +168,7 @@ async def get_tasks_job_output(job_id: str, sweeps: bool = False):
169168
try:
170169
job_data = json.loads(job_data)
171170
except JSONDecodeError:
172-
logging.error(f"Error decoding job_data for job {job_id}. Using empty job_data.")
171+
print(f"Error decoding job_data for job {job_id}. Using empty job_data.")
173172
job_data = {}
174173

175174
# Handle sweeps case first
@@ -198,7 +197,7 @@ async def get_tasks_job_output(job_id: str, sweeps: bool = False):
198197
# If the value error starts with "No output file found for job" then wait 4 seconds and try again
199198
# because the file might not have been created yet
200199
if str(e).startswith("No output file found for job"):
201-
logging.info(f"Output file not found for job {job_id}, retrying in 4 seconds...")
200+
print(f"Output file not found for job {job_id}, retrying in 4 seconds...")
202201
await asyncio.sleep(4)
203202
try:
204203
output_file_name = await shared.get_job_output_file_name(job_id)
@@ -212,7 +211,7 @@ async def get_tasks_job_output(job_id: str, sweeps: bool = False):
212211
return ["Output file not found after retry"]
213212
except Exception as retry_e:
214213
# If still no file after retry, create an empty one in the jobs directory
215-
logging.warning(
214+
print(
216215
f"Still no output file found for job {job_id} after retry, creating empty file: {retry_e}"
217216
)
218217
# Use the Job class to get the proper directory and create the file
@@ -223,11 +222,11 @@ async def get_tasks_job_output(job_id: str, sweeps: bool = False):
223222
f.write("")
224223
return []
225224
else:
226-
logging.error(f"ValueError in get_tasks_job_output: {e}")
225+
print(f"ValueError in get_tasks_job_output: {e}")
227226
return ["An internal error has occurred!"]
228227
except Exception as e:
229228
# Handle general error
230-
logging.error(f"Error in get_tasks_job_output: {e}")
229+
print(f"Error in get_tasks_job_output: {e}")
231230
return ["An internal error has occurred!"]
232231

233232

@@ -252,10 +251,10 @@ async def update_training_template(
252251
datasets = configObject["dataset_name"]
253252
job_service.update_training_template(template_id, name, description, type, datasets, config)
254253
except JSONDecodeError as e:
255-
logging.error(f"JSON decode error: {e}")
254+
print(f"JSON decode error: {e}")
256255
return {"status": "error", "message": "An error occurred while processing the request."}
257256
except Exception as e:
258-
logging.error(f"Unexpected error: {e}")
257+
print(f"Unexpected error: {e}")
259258
return {"status": "error", "message": "An internal error has occurred."}
260259
return {"status": "success"}
261260

@@ -276,7 +275,7 @@ async def stream_job_output(job_id: str, sweeps: bool = False):
276275
try:
277276
job_data = json.loads(job_data)
278277
except JSONDecodeError:
279-
logging.error(f"Error decoding job_data for job {job_id}. Using empty job_data.")
278+
print(f"Error decoding job_data for job {job_id}. Using empty job_data.")
280279
job_data = {}
281280

282281
# Handle sweeps case first
@@ -295,13 +294,13 @@ async def stream_job_output(job_id: str, sweeps: bool = False):
295294
# If the value error starts with "No output file found for job" then wait 4 seconds and try again
296295
# because the file might not have been created yet
297296
if str(e).startswith("No output file found for job"):
298-
logging.info(f"Output file not found for job {job_id}, retrying in 4 seconds...")
297+
print(f"Output file not found for job {job_id}, retrying in 4 seconds...")
299298
await asyncio.sleep(4)
300299
try:
301300
output_file_name = await shared.get_job_output_file_name(job_id)
302301
except Exception as retry_e:
303302
# If still no file after retry, create an empty one in the jobs directory
304-
logging.warning(
303+
print(
305304
f"Still no output file found for job {job_id} after retry, creating empty file: {retry_e}"
306305
)
307306
# Use the Job class to get the proper directory and create the file
@@ -311,15 +310,15 @@ async def stream_job_output(job_id: str, sweeps: bool = False):
311310
with open(output_file_name, "w") as f:
312311
f.write("")
313312
else:
314-
logging.error(f"ValueError in stream_job_output: {e}")
313+
print(f"ValueError in stream_job_output: {e}")
315314
return StreamingResponse(
316315
iter(["data: Error: An internal error has occurred!\n\n"]),
317316
media_type="text/event-stream",
318317
headers={"Cache-Control": "no-cache", "Connection": "keep-alive", "Access-Control-Allow-Origin": "*"},
319318
)
320319
except Exception as e:
321320
# Handle general error
322-
logging.error(f"Error in stream_job_output: {e}")
321+
print(f"Error in stream_job_output: {e}")
323322
return StreamingResponse(
324323
iter(["data: Error: An internal error has occurred!\n\n"]),
325324
media_type="text/event-stream",
@@ -461,7 +460,7 @@ async def get_eval_images(job_id: str):
461460
}
462461
)
463462
except OSError as e:
464-
logging.error(f"Error reading images directory {images_dir}: {e}")
463+
print(f"Error reading images directory {images_dir}: {e}")
465464
return {"images": []}
466465

467466
# Sort by filename for consistent ordering
@@ -625,7 +624,7 @@ async def get_checkpoints(job_id: str, request: Request):
625624
# Format the timestamp as ISO 8601 string
626625
formatted_time = datetime.fromtimestamp(modified_time).isoformat()
627626
except Exception as e:
628-
logging.error(f"Error getting stat for file {file_path}: {e}")
627+
print(f"Error getting stat for file {file_path}: {e}")
629628
formatted_time = None
630629
filesize = None
631630
checkpoints.append({"filename": filename, "date": formatted_time, "size": filesize})
@@ -675,14 +674,14 @@ async def get_artifacts(job_id: str, request: Request):
675674
filename = os.path.basename(artifact_path)
676675
artifacts.append({"filename": filename, "date": formatted_time, "size": filesize})
677676
except Exception as e:
678-
logging.error(f"Error getting stat for artifact {artifact_path}: {e}")
677+
print(f"Error getting stat for artifact {artifact_path}: {e}")
679678
continue
680679

681680
# Sort artifacts by filename in reverse (descending) order for consistent ordering
682681
artifacts.sort(key=lambda x: x["filename"], reverse=True)
683682
return {"artifacts": artifacts}
684683
except Exception as e:
685-
logging.info(f"SDK artifact method failed for job {job_id}, falling back to legacy method: {e}")
684+
print(f"SDK artifact method failed for job {job_id}, falling back to legacy method: {e}")
686685

687686
# Fallback to the original logic if SDK method doesn't work or returns nothing
688687
# Get artifacts directory from job_data or use default location
@@ -708,12 +707,12 @@ async def get_artifacts(job_id: str, request: Request):
708707
# Format the timestamp as ISO 8601 string
709708
formatted_time = datetime.fromtimestamp(modified_time).isoformat()
710709
except Exception as e:
711-
logging.error(f"Error getting stat for file {file_path}: {e}")
710+
print(f"Error getting stat for file {file_path}: {e}")
712711
formatted_time = None
713712
filesize = None
714713
artifacts.append({"filename": filename, "date": formatted_time, "size": filesize})
715714
except OSError as e:
716-
logging.error(f"Error reading artifacts directory {artifacts_dir}: {e}")
715+
print(f"Error reading artifacts directory {artifacts_dir}: {e}")
717716

718717
# Sort artifacts by filename in reverse (descending) order for consistent ordering
719718
artifacts.sort(key=lambda x: x["filename"], reverse=True)
@@ -752,11 +751,11 @@ async def get_training_job_output_jobpath(job_id: str, sweeps: bool = False):
752751
return output
753752
except ValueError as e:
754753
# Handle specific error
755-
logging.error(f"ValueError: {e}")
754+
print(f"ValueError: {e}")
756755
return "An internal error has occurred!"
757756
except Exception as e:
758757
# Handle general error
759-
logging.error(f"Error: {e}")
758+
print(f"Error: {e}")
760759
return "An internal error has occurred!"
761760

762761

@@ -773,12 +772,12 @@ async def sweep_results(job_id: str):
773772
output = json.load(f)
774773
return {"status": "success", "data": output}
775774
except json.JSONDecodeError as e:
776-
logging.error(f"JSON decode error for job {job_id}: {e}")
775+
print(f"JSON decode error for job {job_id}: {e}")
777776
return {"status": "error", "message": "Invalid JSON format in sweep results file."}
778777
else:
779-
logging.warning(f"Sweep results file not found for job {job_id}: {output_file}")
778+
print(f"Sweep results file not found for job {job_id}: {output_file}")
780779
return {"status": "error", "message": "Sweep results file not found."}
781780

782781
except Exception as e:
783-
logging.error(f"Error loading sweep results for job {job_id}: {e}")
782+
print(f"Error loading sweep results for job {job_id}: {e}")
784783
return {"status": "error", "message": "An internal error has occurred!"}

0 commit comments

Comments
 (0)