Skip to content

Commit ad7caef

Browse files
committed
refactor: update logger decorator
1 parent 9a36830 commit ad7caef

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

backend/common/audit/schemas/logger_decorator.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ async def create_log(
123123
return log
124124
except Exception as e:
125125
session.rollback()
126-
print(f"[SystemLogger] Log -14 : {str(traceback.format_exc())}")
127126
print(f"Failed to create system log: {e}")
128127
return None
129128

@@ -261,6 +260,14 @@ def extract_resource_id(
261260

262261
# Process attribute expressions
263262
return SystemLogger.extract_value_from_object(expression, func_args)
263+
elif isinstance(source, dict):
264+
# Simple parameter name
265+
if expression in source:
266+
value = source[expression]
267+
return value if value is not None else None
268+
269+
# complex expression
270+
return SystemLogger.extract_value_from_object(expression, source)
264271

265272
elif source_type == "kwargs":
266273
# Extract from keyword parameters
@@ -281,7 +288,7 @@ def extract_resource_id(
281288
@staticmethod
282289
def extract_from_function_params(
283290
expression: Optional[str],
284-
func_args: tuple,
291+
func_args: any,
285292
func_kwargs: dict
286293
):
287294
"""Extract values from function parameters"""
@@ -386,7 +393,6 @@ async def create_log_record(
386393
) -> Optional[SystemLog]:
387394
"""Create log records"""
388395
try:
389-
print(f"[SystemLogger] Log -9")
390396
# Obtain user information
391397
user_info = cls.get_current_user(request)
392398
user_id = user_info.id if user_info else -1
@@ -402,7 +408,6 @@ async def create_log_record(
402408
if config.extract_params:
403409
request_params = cls.extract_request_params(request)
404410

405-
print(f"[SystemLogger-Info] :user_info {str(user_info)},oid {str(oid)}")
406411
# Create log object
407412
log = SystemLog(
408413
operation_type=opt_type_ref if opt_type_ref else config.operation_type,
@@ -426,7 +431,6 @@ async def create_log_record(
426431
)
427432

428433
with Session(engine) as session:
429-
print(f"[SystemLogger] Log -10")
430434
session.add(log)
431435
session.commit()
432436
session.refresh(log)
@@ -469,20 +473,22 @@ async def async_wrapper(*args, **kwargs):
469473
try:
470474
# Get current request
471475
request = RequestContext.get_request()
472-
print(f"[SystemLogger] Log -1 : {str(request)}")
476+
func_signature = inspect.signature(func)
477+
bound_args = func_signature.bind(*args, **kwargs)
478+
bound_args.apply_defaults()
479+
unified_kwargs = dict(bound_args.arguments)
473480

474481
# Step 1: Attempt to extract the resource ID from the parameters
475482
if config.resource_id_expr:
476483
resource_id = SystemLogger.extract_from_function_params(
477484
config.resource_id_expr,
478-
args,
485+
unified_kwargs,
479486
kwargs
480487
)
481-
print(f"[SystemLogger] Log -2 : {str(resource_id)}")
482488
if config.remark_expr:
483489
remark = SystemLogger.extract_from_function_params(
484490
config.remark_expr,
485-
args,
491+
unified_kwargs,
486492
kwargs
487493
)
488494

@@ -504,7 +510,6 @@ async def async_wrapper(*args, **kwargs):
504510
resource_id = -1
505511
oid = -1
506512
resource_name = '-' + input_account
507-
print(f"[SystemLogger] Log -3 : {str(resource_id)}")
508513
if config.operation_type == OperationType.DELETE:
509514
with Session(engine) as session:
510515
resource_name = get_resource_name_by_id_and_module(session, resource_id, config.module)
@@ -513,10 +518,8 @@ async def async_wrapper(*args, **kwargs):
513518
opt_type_ref = OperationType.UPDATE if resource_id is not None else OperationType.CREATE
514519
else:
515520
opt_type_ref = config.operation_type
516-
print(f"[SystemLogger] Log -4 : {str(opt_type_ref)}")
517521
# Execute the original function
518522
result = await func(*args, **kwargs)
519-
print(f"[SystemLogger] Log -5 : {str(result)}")
520523
# Step 2: If the resource ID is configured to be extracted from the results and has not been extracted before
521524
if config.result_id_expr and not resource_id and result:
522525
resource_id = SystemLogger.extract_resource_id(
@@ -527,11 +530,9 @@ async def async_wrapper(*args, **kwargs):
527530
if config.operation_type != OperationType.DELETE:
528531
with Session(engine) as session:
529532
resource_name = get_resource_name_by_id_and_module(session, resource_id, config.module)
530-
print(f"[SystemLogger] Log -6 : {str(resource_name)}")
531533
return result
532534

533535
except Exception as e:
534-
print(f"[SystemLogger] Log -6 : {str(traceback.format_exc())}")
535536
status = OperationStatus.FAILED
536537
error_message = str(e)
537538

@@ -552,7 +553,6 @@ async def async_wrapper(*args, **kwargs):
552553

553554
# Calculate execution time
554555
execution_time = int((time.time() - start_time) * 1000)
555-
print(f"[SystemLogger] Log -8 ")
556556
# Asynchronous creation of log records
557557
try:
558558
await SystemLogger.create_log_record(
@@ -568,7 +568,6 @@ async def async_wrapper(*args, **kwargs):
568568
opt_type_ref=opt_type_ref
569569
)
570570
except Exception as log_error:
571-
print(f"[SystemLogger] Log -12 : {str(traceback.format_exc())}")
572571
print(f"[SystemLogger] Log creation failed: {log_error}")
573572

574573
@functools.wraps(func)
@@ -584,12 +583,16 @@ def sync_wrapper(*args, **kwargs):
584583
try:
585584
# Get current request
586585
request = RequestContext.get_request()
586+
func_signature = inspect.signature(func)
587+
bound_args = func_signature.bind(*args, **kwargs)
588+
bound_args.apply_defaults()
589+
unified_kwargs = dict(bound_args.arguments)
587590

588591
# Extract resource ID from parameters
589592
if config.resource_id_expr:
590593
resource_id = SystemLogger.extract_from_function_params(
591594
config.resource_id_expr,
592-
args,
595+
unified_kwargs,
593596
kwargs
594597
)
595598

@@ -661,7 +664,6 @@ def sync_wrapper(*args, **kwargs):
661664
)
662665
)
663666
except Exception as log_error:
664-
print(f"[SystemLogger] Log -13 : {str(traceback.format_exc())}")
665667
print(f"[SystemLogger] Log creation failed: {log_error}")
666668

667669
# Return appropriate wrapper based on function type

0 commit comments

Comments
 (0)