Python:文件处理程序问题:删除文件而不留下.nsf文件

时间:2013-03-12 04:13:44

标签: python logging nsfilehandle

我有以下方法来处理我的python程序中的日志记录

def createLogger(logger, logLang):
    """
    Setting up logger
    """
    log_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    file_handler = logging.FileHandler(filename=(os.path.join(OUT_DIR_LOGS, logLang + '-userdynamics.log')))
    file_handler.setFormatter(log_format)
    logger.setLevel(logging.INFO)
    logger.addHandler(file_handler)

这是一个大型数据收集代码库,为了避免远程服务器上的配额限制,我实现了以下gzip和tar过程,

def gzipLogs(lang):
    """
    Compressing and tar
    """
    # Compressing logfiles and removing the old logfile
    original_filePath = OUT_DIR_LOGS + "/" +lang + "-userdynamics.log"
    gzip_filePath = OUT_DIR_LOGS + "/" + lang +"-userdynamics.gz"
    with open(original_filePath , 'rb') as original_file:
        with gzip.open(gzip_filePath, 'wb') as zipped_file:
            zipped_file.writelines(original_file)
    os.remove(original_filePath)
    # Compressing language folders that has data
    folder_path = OUT_DIR + "/" + lang
    tar_file = tarfile.open(folder_path + ".tgz", "w:gz")
    # add timestamp to arch file
    tar_file.add(folder_path, arcname = NOW + "_" + lang)
    tar_file.close()
    # delete the original file
    shutil.rmtree(folder_path)

我在嵌套的for循环中进行数据收集过程,并按下面提到的方式调用记录器:

for something in somethings:
    for item in items:
        log = logging.getLogger()
        # Calling the logging configuration function.
        createLogger(log, lang)

一切正常但是当它被执行时,在删除文件之后.nsf文件残留被遗忘,同时导致配额问题保持不变。

所以我添加了以下代码段来关闭日志文件处理程序但是现在我最终得到以下错误:

关闭日志文件的代码:

unclosed_logs = list(log.handlers)
for uFile in unclosed_logs:
    print uFile
    log.removeHandler(uFile)
    uFile.flush()
    uFile.close()

上面的代码最终给了我这个错误:

Traceback (most recent call last):
  File "/somefilepath/SomePythonFile.py", line 529, in <module>
    main()
  File "/somefilepath/SomePythonFile.py", line 521, in main
    gzipLogs(lang)
  File "/somefilepath/SomePythonFile.py", line 61, in gzipLogs
    with gzip.open(gzip_filePath, 'wb') as zipped_file:
AttributeError: GzipFile instance has no attribute '__exit__'

这是main方法在处理程序关闭代码段时的样子:

for something in somethings:
    for item in items:
        log = logging.getLogger()
        # Calling the logging configuration function.
        createLogger(log, lang)
    unclosed_logs = list(log.handlers)
    for uFile in unclosed_logs:
        print uFile
        log.removeHandler(uFile)
        uFile.flush()
        uFile.close()

我做错了什么?我处理记录器错了吗?或者我是否过早关闭文件?

2 个答案:

答案 0 :(得分:1)

有很多事情可能导致问题:

  1. 您应该只在程序的一个位置配置日志记录(例如设置级别,添加处理程序),最好是从if __name__ == '__main__'子句。你似乎不是这样做的。请注意,您可以使用WatchedFileHandler并使用外部旋转器来旋转日志文件 - 例如logrotate提供轮换和压缩功能。
  2. 您与__exit__相关的错误与日志记录无关 - 它可能与Python版本问题有关。 GZipFile仅在Python 2.7 / 3.2中与with一起使用 - 在旧版本中,如果您尝试在GZipFile语句中使用with,则会收到错误消息。

答案 1 :(得分:0)

经过一番研究后,我意识到我正在执行该文件的服务器是针对python 2.6运行的,而2.6 GZip模块中的服务器没有with open。要回答这个问题,要么切换到python 2.7,要么在try catch块中将实现更改为旧时尚文件打开文件。

try:
    inOriginalFile = open(original_filePath, 'rb')
    outGZipFile = gzip.open(gzip_filePath, 'wb')
    try:
        outGZipFile.writelines(inOriginalFile)
    finally:
        outGZipFile.close()
        inOriginalFile.close()
except IOError as e:
    logging.error("Unable to open gzip files, GZIP FAILURE")

这就是我解决这个问题的方法。

相关问题