You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

加解密程序日志路径正确却写入错误文件夹问题求助

日志写入路径异常排查求助

我开发了一款具备加解密功能的小型程序,预期将加密操作的日志写入Encryption文件夹,解密操作的日志写入Decryption文件夹。

步骤1:解密文件函数

def perform_decryption(mainbox, operation_unique_identifier: str, file_path: str):
    operation_code = {'OK': 200, 'BAD REQUEST': 404}
    path_validator = PathValidator(file_path)

    operation_id = operation_code['BAD REQUEST']
    if not path_validator.check_path_existence(file_path):
        log_message = reusable_functions.path_invalid_message(operation_unique_identifier, file_path)
        log_functions.update_file_log(log_message, operation_unique_identifier)
        reusable_functions.update_display_log(mainbox, operation_unique_identifier, operation_id)
        return

    operation_id = operation_code['OK']
    try:
        decryption = Decryption(file_path)
        key = decryption.load_key()
        decryption.decrypt_file(key)

        reusable_functions.update_display_log(mainbox, operation_unique_identifier, operation_id)
        log_decryption_result(operation_unique_identifier, operation_id, decryption.file_key_pair)
    except Exception as e:
        print(e)

步骤2:生成解密日志消息并传递给log_functions模块

def log_decryption_result(operation_unique_identifier: str, operation_id: int, file_key_pair: dict[str, str]) -> None:
    log_file_message = (f'\n[{operation_unique_identifier.upper()}: {operation_id}]: '
                        f'\nDecrypted the following file:')
    for file_name, key_path in file_key_pair.items():
        log_file_message += f'\n\t{file_name}'
        log_file_message += '\nKey from the folowing location deleted:'
        log_file_message += f'\n\t{key_path}'

    log_functions.update_file_log(log_file_message, operation_unique_identifier)

步骤3:传递日志消息生成日志文件路径

def update_file_log(log_message: str, operation_specific_indentifier: str) -> None:
    log_configurer = LogConfigurer()
    file_name = log_configurer.give_log_name()
    file_path = os.path.join(log_configurer.get_month_folder_path(), operation_specific_indentifier, file_name)
    print(f'This file_path: {file_path}')
    formatting_log(file_path, '%(asctime)s %(levelname)s: %(message)s')
    logging.info(log_message)

步骤4:格式化日志函数

def formatting_log(file_path: str, provided_format: str) -> None:
    logging.basicConfig(filename=file_path, format=provided_format, level=logging.INFO, datefmt='%d/%m/%Y %H:%M:%S')

验证结果

  • 打印显示解密操作的日志路径正确:
    This file_path: D:\2xProjects\Python\UtilityBox\resources\results\logs\2023\September\Decryption\13_ublog
    
  • 但实际所有日志(包括解密日志)都写入了以下文件:
    This file_path: D:\2xProjects\Python\UtilityBox\resources\results\logs\2023\September\Encryption\13_ublog
    
  • Decryption文件夹下的日志文件为空,Encryption文件夹下的日志文件包含内容:
    13/09/2023 15:11:11 INFO: 
    [ENCRYPTION: 200]: 
    Encrypted the following file:
        ewrjuerwjsaluecex
    Key saved in the following location:
        D:\2xProjects\Python\UtilityBox\resources\results\keys\ewrjuerwjsaluecex.key
    13/09/2023 15:11:17 INFO: 
    [DECRYPTION: 200]: 
    Decrypted the following file:
        ewrjuerwjsaluecex
    Key from the folowing location deleted:
        D:\2xProjects\Python\UtilityBox\resources\results\keys\ewrjuerwjsaluecex.key
    

我已通过调试逐步跟踪整个流程,但仍无法定位问题所在,恳请协助排查该日志写入异常的原因。

内容的提问来源于stack exchange,提问作者Dragos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 21:40:57