Spaces:
Runtime error
Runtime error
Commit ·
b73a352
1
Parent(s): 572da64
persistent logging with testing
Browse files- persistStorage.py +18 -11
persistStorage.py
CHANGED
|
@@ -6,22 +6,26 @@ import os
|
|
| 6 |
from config import HUGGING_FACE_TOKEN
|
| 7 |
import csv
|
| 8 |
|
| 9 |
-
|
| 10 |
-
TIME_ZONE = 'US/Pacific'
|
| 11 |
-
TIMEZONE_OBJ = pytz.timezone(TIME_ZONE)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
os.
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def append_dict_to_csv(file_path, row_data):
|
| 22 |
fieldnames = row_data.keys()
|
| 23 |
|
| 24 |
-
with
|
| 25 |
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
|
| 26 |
|
| 27 |
# Check if the file is empty, and if so, write the header
|
|
@@ -29,10 +33,13 @@ def append_dict_to_csv(file_path, row_data):
|
|
| 29 |
csv_writer.writeheader()
|
| 30 |
csv_writer.writerow(row_data)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
def saveLog(message, level='info') -> None:
|
|
|
|
| 33 |
current_time = datetime.now(TIMEZONE_OBJ)
|
| 34 |
message = str(message)
|
| 35 |
-
|
| 36 |
-
log_file_path = os.path.join(log_path, f"{current_time.strftime('%Y-%m')}.csv")
|
| 37 |
data_dict = {"time":str(current_time), "level": level, "message": message}
|
| 38 |
append_dict_to_csv(log_file_path, data_dict)
|
|
|
|
| 6 |
from config import HUGGING_FACE_TOKEN
|
| 7 |
import csv
|
| 8 |
|
| 9 |
+
logs_dir = os.getenv("HF_HOME", "/data")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# # Create a new file
|
| 12 |
+
# with open(os.path.join(data_dir, "my_data.txt"), "a") as f:
|
| 13 |
+
# f.write("Hello World! From pesistent storage line 2")
|
| 14 |
|
| 15 |
+
# # Read the data from the file
|
| 16 |
+
# with open(os.path.join(data_dir, "my_data.txt"), "r") as f:
|
| 17 |
+
# data = f.read()
|
| 18 |
+
# # Print the data
|
| 19 |
+
# print(data)
|
| 20 |
|
| 21 |
+
# Set the time zone to Pacific Time Zone
|
| 22 |
+
TIME_ZONE = 'US/Pacific'
|
| 23 |
+
TIMEZONE_OBJ = pytz.timezone(TIME_ZONE)
|
| 24 |
|
| 25 |
def append_dict_to_csv(file_path, row_data):
|
| 26 |
fieldnames = row_data.keys()
|
| 27 |
|
| 28 |
+
with open(file_path, 'a') as csv_file:
|
| 29 |
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
|
| 30 |
|
| 31 |
# Check if the file is empty, and if so, write the header
|
|
|
|
| 33 |
csv_writer.writeheader()
|
| 34 |
csv_writer.writerow(row_data)
|
| 35 |
|
| 36 |
+
with open(file_path, 'r') as fh:
|
| 37 |
+
print(fh.read())
|
| 38 |
+
|
| 39 |
def saveLog(message, level='info') -> None:
|
| 40 |
+
global logs_dir
|
| 41 |
current_time = datetime.now(TIMEZONE_OBJ)
|
| 42 |
message = str(message)
|
| 43 |
+
log_file_path = os.path.join(logs_dir, f"{current_time.strftime('%Y-%m')}.csv")
|
|
|
|
| 44 |
data_dict = {"time":str(current_time), "level": level, "message": message}
|
| 45 |
append_dict_to_csv(log_file_path, data_dict)
|