在python中将日志消息的一部分抓取到变量中

时间:2017-03-14 02:46:16

标签: python bash logging

如何将一部分日志消息抓取到变量中,然后在python中的bash脚本中使用它?

以下代码

if new_file is not None and new_file.get('status', '') != "FAIL":
    message = "Storage service created {}: {}".format(sip_type, new_file)
    logging.info(message)
    print(message)
    sys.exit(0)

生成以下输出:

Storage service created SIP: {'size': 113509, 'status': 'UPLOADED', 'origin_path': 'currentlyProcessing/teshhdhl88gekkjjgkg29-ccdf0a5b-4eda-4948-99fa-c5947082fa41/teshhdhl88gekkjjgkg29-ccdf0a5b-4eda-4948-99fa-c5947082fa41.7z', 'origin_pipeline': '/api/v2/pipeline/053ec0d5-6510-4b0c-a5ef-2949a2f53e9a/', 'uuid': 'ccdf0a5b-4eda-4948-99fa-c5947082fa41', 'related_package_uuid': None, 'current_path': 'ccdf/0a5b/4eda/4948/99fa/c594/7082/fa41/teshhdhl88gekkjjgkg29-ccdf0a5b-4eda-4948-99fa-c5947082fa41.7z', 'origin_location': '/api/v2/location/8bfed394-bb74-4442-b33c-f0766862daa9/', 'misc_attributes': {}, 'current_full_path': '/var/archivematica/sharedDirectory/www/AIPsStore/ccdf/0a5b/4eda/4948/99fa/c594/7082/fa41/teshhdhl88gekkjjgkg29-ccdf0a5b-4eda-4948-99fa-c5947082fa41.7z', 'current_location': '/api/v2/location/587ecb7a-fb95-4076-a5bc-d80528e0d19f/', 'related_packages': [], 'resource_uri': '/api/v2/file/ccdf0a5b-4eda-4948-99fa-c5947082fa41/', 'package_type': 'AIP'}

我需要将'current_full_path'放入要运行的变量中:

dropbox ='/bkpdir'
bashCommand = "bash /home/charles/Dropbox-Uploader/dropbox_uploader.sh upload /VARIABLE WOULD BE HERE " + dropbox
os.system(bashCommand)

1 个答案:

答案 0 :(得分:1)

以下是如何从current_full_path变量中获取message

import ast

if new_file is not None and new_file.get('status', '') != "FAIL":
    message = "Storage service created {}: {}".format(sip_type, new_file)
    logging.info(message)
    print(message)

    current_full_path = ast.literal_eval(message.split("SIP: ")[1])['current_full_path']

    print current_full_path
    #/var/archivematica/sharedDirectory/www/AIPsStore/ccdf/0a5b/4eda/4948/99fa/c594/7082/fa41/teshhdhl88gekkjjgkg29-ccdf0a5b-4eda-4948-99fa-c5947082fa41.7z

    sys.exit(0)

如果return语句在函数中,请记住current_full_path if

要将current_full_path转换为bashCommand,请使用:

dropbox ='/bkpdir'
bashCommand = "bash/home/charles/Dropbox-Uploader/dropbox_uploader.sh upload" + current_full_path + dropbox

print bashCommand
#bash/home/charles/Dropbox-Uploader/dropbox_uploader.sh upload/var/archivematica/sharedDirectory/www/AIPsStore/ccdf/0a5b/4eda/4948/99fa/c594/7082/fa41/teshhdhl88gekkjjgkg29-ccdf0a5b-4eda-4948-99fa-c5947082fa41.7z/bkpdir

os.system(bashCommand)