add better logger
This commit is contained in:
43
main.py
43
main.py
@@ -3,6 +3,7 @@ import config # config.py
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
import json
|
||||
import requests
|
||||
import tempfile
|
||||
@@ -13,15 +14,29 @@ import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from pynetdicom.sop_class import PatientRootQueryRetrieveInformationModelGet
|
||||
|
||||
|
||||
# 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.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:
|
||||
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)]
|
||||
|
||||
try:
|
||||
# Create association with the peer
|
||||
assoc = ae.associate(
|
||||
self.pacs_host,
|
||||
self.pacs_port,
|
||||
@@ -138,7 +152,6 @@ class DicomUploader:
|
||||
if assoc.is_established:
|
||||
ds = Dataset()
|
||||
|
||||
# For IMAGE level retrieval
|
||||
if sop_instance_uid:
|
||||
ds.QueryRetrieveLevel = 'IMAGE'
|
||||
ds.StudyInstanceUID = study_instance_uid
|
||||
@@ -154,13 +167,7 @@ class DicomUploader:
|
||||
|
||||
# Send C-GET request and collect responses
|
||||
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:
|
||||
if status:
|
||||
status_int = getattr(status, 'Status', 0)
|
||||
@@ -170,8 +177,7 @@ class DicomUploader:
|
||||
if status_int == 0xa702: # Failed SOP Instance
|
||||
if identifier and hasattr(identifier, 'FailedSOPInstanceUIDList'):
|
||||
logger.error(f"Failed to retrieve: {identifier.FailedSOPInstanceUIDList}")
|
||||
|
||||
# Release the association
|
||||
|
||||
assoc.release()
|
||||
else:
|
||||
logger.error("Association with PACS failed")
|
||||
@@ -283,7 +289,6 @@ def main():
|
||||
uploader.cleanup()
|
||||
|
||||
logger.info("Finished processing orders")
|
||||
# exit
|
||||
exit(0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user