add better logger
This commit is contained in:
@@ -20,4 +20,6 @@ BATCH_SIZE = 10 # Process orders in batches
|
|||||||
|
|
||||||
# Logging Configuration
|
# Logging Configuration
|
||||||
LOG_LEVEL = 'INFO'
|
LOG_LEVEL = 'INFO'
|
||||||
LOG_FILE = 'server.log'
|
LOG_FILE = 'server.log'
|
||||||
|
LOG_MAX_SIZE = 10 * 1024 * 1024 # 10 MB
|
||||||
|
LOG_BACKUP_COUNT = 5 # Keep 5 backup log files
|
||||||
43
main.py
43
main.py
@@ -3,6 +3,7 @@ import config # config.py
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
from logging.handlers import RotatingFileHandler
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -13,15 +14,29 @@ import uuid
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from pynetdicom.sop_class import PatientRootQueryRetrieveInformationModelGet
|
from pynetdicom.sop_class import PatientRootQueryRetrieveInformationModelGet
|
||||||
|
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
logging.basicConfig(
|
|
||||||
level=logging.INFO,
|
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
||||||
datefmt='%Y-%m-%d %H:%M:%S',
|
|
||||||
handlers=[logging.StreamHandler()]
|
|
||||||
)
|
|
||||||
logger = logging.getLogger('DicomUploader')
|
logger = logging.getLogger('DicomUploader')
|
||||||
|
logger.setLevel(getattr(logging, config.LOG_LEVEL))
|
||||||
|
|
||||||
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', '%Y-%m-%d %H:%M:%S')
|
||||||
|
|
||||||
|
# Add console handler
|
||||||
|
console_handler = logging.StreamHandler()
|
||||||
|
console_handler.setFormatter(formatter)
|
||||||
|
logger.addHandler(console_handler)
|
||||||
|
|
||||||
|
# Add rotating file handler
|
||||||
|
file_handler = RotatingFileHandler(
|
||||||
|
config.LOG_FILE,
|
||||||
|
maxBytes=config.LOG_MAX_SIZE,
|
||||||
|
backupCount=config.LOG_BACKUP_COUNT,
|
||||||
|
encoding='utf-8'
|
||||||
|
)
|
||||||
|
file_handler.setFormatter(formatter)
|
||||||
|
logger.addHandler(file_handler)
|
||||||
|
|
||||||
|
# Prevent propagation to root logger
|
||||||
|
logger.propagate = False
|
||||||
|
|
||||||
class DicomUploader:
|
class DicomUploader:
|
||||||
def __init__(self, pacs_host, pacs_port, pacs_ae_title, local_ae_title, proxy_url):
|
def __init__(self, pacs_host, pacs_port, pacs_ae_title, local_ae_title, proxy_url):
|
||||||
@@ -126,7 +141,6 @@ class DicomUploader:
|
|||||||
handlers = [(evt.EVT_C_STORE, handle_store)]
|
handlers = [(evt.EVT_C_STORE, handle_store)]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Create association with the peer
|
|
||||||
assoc = ae.associate(
|
assoc = ae.associate(
|
||||||
self.pacs_host,
|
self.pacs_host,
|
||||||
self.pacs_port,
|
self.pacs_port,
|
||||||
@@ -138,7 +152,6 @@ class DicomUploader:
|
|||||||
if assoc.is_established:
|
if assoc.is_established:
|
||||||
ds = Dataset()
|
ds = Dataset()
|
||||||
|
|
||||||
# For IMAGE level retrieval
|
|
||||||
if sop_instance_uid:
|
if sop_instance_uid:
|
||||||
ds.QueryRetrieveLevel = 'IMAGE'
|
ds.QueryRetrieveLevel = 'IMAGE'
|
||||||
ds.StudyInstanceUID = study_instance_uid
|
ds.StudyInstanceUID = study_instance_uid
|
||||||
@@ -154,13 +167,7 @@ class DicomUploader:
|
|||||||
|
|
||||||
# Send C-GET request and collect responses
|
# Send C-GET request and collect responses
|
||||||
responses = assoc.send_c_get(ds, PatientRootQueryRetrieveInformationModelGet)
|
responses = assoc.send_c_get(ds, PatientRootQueryRetrieveInformationModelGet)
|
||||||
|
|
||||||
# for (status, identifier) in responses:
|
|
||||||
# if status:
|
|
||||||
# print('C-GET query status: 0x{0:04x}'.format(status.Status))
|
|
||||||
# else:
|
|
||||||
# print('Connection timed out, was aborted or received invalid response')
|
|
||||||
|
|
||||||
for status, identifier in responses:
|
for status, identifier in responses:
|
||||||
if status:
|
if status:
|
||||||
status_int = getattr(status, 'Status', 0)
|
status_int = getattr(status, 'Status', 0)
|
||||||
@@ -170,8 +177,7 @@ class DicomUploader:
|
|||||||
if status_int == 0xa702: # Failed SOP Instance
|
if status_int == 0xa702: # Failed SOP Instance
|
||||||
if identifier and hasattr(identifier, 'FailedSOPInstanceUIDList'):
|
if identifier and hasattr(identifier, 'FailedSOPInstanceUIDList'):
|
||||||
logger.error(f"Failed to retrieve: {identifier.FailedSOPInstanceUIDList}")
|
logger.error(f"Failed to retrieve: {identifier.FailedSOPInstanceUIDList}")
|
||||||
|
|
||||||
# Release the association
|
|
||||||
assoc.release()
|
assoc.release()
|
||||||
else:
|
else:
|
||||||
logger.error("Association with PACS failed")
|
logger.error("Association with PACS failed")
|
||||||
@@ -283,7 +289,6 @@ def main():
|
|||||||
uploader.cleanup()
|
uploader.cleanup()
|
||||||
|
|
||||||
logger.info("Finished processing orders")
|
logger.info("Finished processing orders")
|
||||||
# exit
|
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user