Logging Writing Failed maximum recursion depth exceeded in comparison

这篇具有很好参考价值的文章主要介绍了Logging Writing Failed maximum recursion depth exceeded in comparison。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

这个报错是什么原因?

Logging Writing Failed maximum recursion depth exceeded in comparison Logging Writing Failed maximum recursion depth exceeded while calling a Python object Logging Writing Failed maximum recursion depth exceeded while calling a Python object Logging Writing Failed maximum recursion depth exceeded in comparison Logging Writing Failed maximum recursion depth exceeded while calling a Python object Logging Writing Failed maximum recursion depth exceeded while calling a Python object Logging Writing Failed maximum recursion depth exceeded while calling a Python object Logging Writing Failed maximum recursion depth exceeded in comparison Logging Writing Failed maximum recursion depth exceeded while calling a Python object Logging Writing Failed maximum recursion depth exceeded while calling a Python object

这个错误信息表明在你的Python程序中发生了递归深度超过最大限制的问题。Python设置了递归深度的最大限制,以防止无限递归导致栈溢出。通常情况下,递归深度超过1000或者更多次就会触发这个错误。

这种错误可能是由于代码中存在无限递归的情况,或者递归调用层次太深而触发的。要解决这个问题,你可以考虑以下几种方法:

  1. 优化递归算法:检查你的代码,确保递归函数正确地终止条件,并且递归调用不会无限制地进行。你可能需要重新设计递归算法,以减少递归深度。

  2. 使用迭代代替递归:有时,可以将递归算法改写成迭代算法,以避免递归深度问题。这通常需要使用循环来模拟递归过程。

  3. 增加递归深度限制:尽管这不是解决问题的最佳方法,但你可以使用sys.setrecursionlimit()函数来增加Python的递归深度限制。但要注意,这可能会导致栈溢出或其他不稳定的行为,因此谨慎使用。

  4. 重新审视问题:有时,递归并不是解决问题的最佳方法,可能存在更有效的非递归解决方案。重新审视问题并考虑其他算法可能有助于避免递归深度问题。

最好的方法是检查你的代码,确保递归算法被正确实现,并且没有导致无限递归的情况。如果问题仍然存在,考虑使用其他方法来解决问题或减少递归深度。

可以举例说明吗?

当出现"Logging Writing Failed maximum recursion depth exceeded"错误时,这通常是因为在记录日志或执行某些操作时发生了无限递归。以下是一个简单的例子,展示了如何触发这种错误以及如何解决它:

import logging

# 创建一个简单的日志记录器
logging.basicConfig()

def recursive_function(n):
    # 模拟递归操作,但存在无限递归的问题
    if n <= 0:
        return
    else:
        logging.error("Logging Writing Failed maximum recursion depth exceeded")
        recursive_function(n - 1)

try:
    recursive_function(10000)  # 尝试调用具有大量递归深度的函数
except RecursionError as e:
    print(f"RecursionError: {e}")

在这个例子中,recursive_function函数尝试执行递归操作,但没有正确的终止条件,导致无限递归。这将导致"Logging Writing Failed maximum recursion depth exceeded"错误。

要解决这个问题,你可以修改recursive_function函数,添加一个合适的终止条件,以确保递归的结束。例如:

def recursive_function(n):
    # 添加终止条件
    if n <= 0:
        return
    else:
        logging.error("Logging Writing Failed maximum recursion depth exceeded")
        recursive_function(n - 1)

try:
    recursive_function(1000)  # 调用带有终止条件的函数
except RecursionError as e:
    print(f"RecursionError: {e}")

在这个修改后的版本中,递归将在n达到0时终止,不再无限递归,因此不会触发递归深度错误。你应该根据你的具体问题检查代码,并确保递归函数有适当的终止条件,以避免递归深度错误。

我的代码使用了递归吗?如何修改成循环,避免这个报错:

def get_log_path(SERVER_ADDR,SERVER_PORT):

    file_path = os.path.abspath(__file__)

    file_name = os.path.basename(file_path).split(".")[0]

    return os.path.join(base_path, f'logs_{file_name}_{SERVER_ADDR}_{SERVER_PORT}')

 

def cleanup_logs():

    log_path = get_log_path(SERVER_ADDR,SERVER_PORT)

    current_time = time.time()

    for file_name in os.listdir(log_path):

        # 获取文件名中的时间部分

        try:

            creation_str = file_name.split("_")[0] + "_" + file_name.split("_")[1] + "_" + file_name.split("_")[2]

            creation_time = time.mktime(time.strptime(creation_str, "%Y-%m-%d_%H-%M_%S"))

 

        except (ValueError, IndexError):

            # 文件名格式不正确,跳过

            continue

 

        # 如果文件创建时间早于 180 天前,就删除

        if current_time - creation_time > (30 * 24 * 60 * 60):

            file_path = os.path.join(log_path, file_name)

            os.remove(file_path)

 

def configure_logging():

    log_path = get_log_path(SERVER_ADDR,SERVER_PORT)

    os.makedirs(log_path, exist_ok=True)

    log_filename = get_log_filename()

    log_file = os.path.join(log_path, log_filename)

    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', filename=log_file)

 

def get_log_filename():

    now = datetime.datetime.now()

    return now.strftime("%Y-%m-%d.log")

 

def create_new_log():

    log_path = get_log_path(SERVER_ADDR,SERVER_PORT)

    log_files = os.listdir(log_path)

    if len(log_files) >= 300:

        oldest_file = min(log_files)

        os.remove(os.path.join(log_path, oldest_file))

    log_filename = get_log_filename()

    log_filepath = os.path.join(log_path, log_filename)

    return log_filepath

 

def check_log_size(log_filepath):

    log_size = os.path.getsize(log_filepath)

    if log_size > 2 * 1024 * 1024:

        # 创建新的日志文件

        new_log_filepath = create_new_log()

        try:

            shutil.move(log_filepath, new_log_filepath)

            return new_log_filepath

        except PermissionError:

            insert_log(logger, f'{log_filepath} {PermissionError}', log_filepath)

            time.sleep(1)

            return log_filepath

    return log_filepath

 

def insert_log(logger, log_message, log_filepath):

    cleanup_logs()

    try:

        log_filepath = check_log_size(log_filepath)

        # 创建文件处理器

        file_handler = RotatingFileHandler(log_filepath, maxBytes=2 * 1024 * 1024, backupCount=1)

        file_handler.setLevel(logging.DEBUG)

        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

        file_handler.setFormatter(formatter)

        # 添加文件处理器到日志记录器

        logger.addHandler(file_handler)

 

        with log_lock:

            # 添加文件处理器到日志记录器

            logger.addHandler(file_handler)

            try:

                logger.debug(log_message)

                print("log:",log_message)

            except PermissionError:

                insert_log(logger, f'{log_message} {PermissionError}', log_filepath)

                time.sleep(1)  # 延迟0.1秒

 

            # 移除文件处理器

            logger.removeHandler(file_handler)

    except Exception as e:

        print(f"Logging Writing Failed {e}")

        insert_log(logger, f"Logging Writing Failed {e}", log_filepath)

你的代码中没有显式的递归函数,因此不是递归函数导致报错的问题。相反,你的代码涉及到文件处理和日志记录,这些操作通常不会导致递归深度错误。

根据你的代码,问题可能出现在以下几个地方:

  1. 文件处理:你的代码中涉及到了文件的创建、删除和移动操作。如果这些操作在某种情况下导致了无限循环或者递归操作,那么可能会触发递归深度错误。

  2. 日志记录:你的代码使用了Python的logging模块来记录日志,但并没有直接与递归有关的问题。但是,在insert_log函数中,你使用了一个RotatingFileHandler来处理日志文件,这可能会引发问题,尤其是在多线程或多进程环境中,因为你在该函数中多次调用logger.addHandler(file_handler),而没有移除处理器。

为了避免可能的问题,你可以考虑以下几个步骤:

  1. 检查文件操作:确保文件的创建、删除和移动操作没有导致无限循环或递归。你可以在文件操作之前添加日志来跟踪代码的执行。

  2. 日志处理器添加和移除:在insert_log函数中,确保只在必要时添加和移除处理器,以防止重复添加处理器。你可以在处理器添加和移除的地方加入日志来跟踪处理器的操作。

  3. 异常处理:在异常处理部分,你可以更仔细地记录错误信息,以便更好地理解问题的根本原因。

如果你可以提供更多关于具体报错信息或问题的上下文,我将能够提供更具体的建议来解决问题。如果问题仍然存在,请提供更多信息以便我可以帮助你进一步。

如何修改,详细一点

根据你提供的代码,我会逐步指导你如何修改代码以确保避免递归深度错误以及其他潜在问题。首先,我们将关注以下几个主要问题:

  1. 处理器添加和移除:确保只在必要时添加和移除处理器,以防止重复添加处理器。
  2. 异常处理:更仔细地记录错误信息,以便更好地理解问题的根本原因。

我将针对每个问题提供详细的建议。

处理器添加和移除:

在你的代码中,你在insert_log函数中多次添加处理器,但并没有在必要时将它们移除。这可能导致处理器重复添加,可能会导致日志文件被多次写入。为了解决这个问题,你可以修改insert_log函数如下:

def insert_log(logger, log_message, log_filepath):
    cleanup_logs()

    try:
        log_filepath = check_log_size(log_filepath)

        # 创建文件处理器
        file_handler = RotatingFileHandler(log_filepath, maxBytes=2 * 1024 * 1024, backupCount=1)
        file_handler.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        file_handler.setFormatter(formatter)

        # 添加文件处理器到日志记录器
        logger.addHandler(file_handler)

        with log_lock:
            try:
                logger.debug(log_message)
                print("log:", log_message)
            except PermissionError:
                insert_log(logger, f'{log_message} {PermissionError}', log_filepath)
                time.sleep(1)  # 延迟0.1秒

        # 移除文件处理器,以避免重复添加
        logger.removeHandler(file_handler)

    except Exception as e:
        print(f"Logging Writing Failed {e}")
        insert_log(logger, f"Logging Writing Failed {e}", log_filepath)

在上述修改中,我添加了一个移除处理器的步骤,确保每次只添加一个处理器并在使用后将其移除。这可以防止处理器的重复添加。

异常处理:

在异常处理部分,你可以更仔细地记录错误信息,以便更好地理解问题的根本原因。修改如下:

except Exception as e:
    print(f"Logging Writing Failed {e}")
    insert_log(logger, f"Logging Writing Failed {e}", log_filepath)

这段代码将错误信息以字符串的形式记录下来,并将其传递给insert_log函数,以便更好地诊断问题。你可以根据需要进一步处理错误信息,以便更好地理解问题的来源。

通过这些修改,你的代码应该更容易管理处理器的添加和移除,以及更好地记录错误信息。这将有助于避免递归深度错误以及其他潜在的问题。如果还有其他特定问题或需要进一步的修改,请提供更多信息,我将尽力提供更多帮助。

根本问题:log的数量超过了设定值,处理机制不够科学

解决办法就是当log数量达到限制,则新建一个log。文章来源地址https://www.toymoban.com/news/detail-700855.html

def get_log_path(SERVER_ADDR, SERVER_PORT):
    file_path = os.path.abspath(__file__)
    file_name = os.path.basename(file_path).split(".")[0]
    return os.path.join(base_path, f'logs_{file_name}_{SERVER_ADDR}_{SERVER_PORT}')

def cleanup_logs():
    log_path = get_log_path(SERVER_ADDR,SERVER_PORT)
    current_time = time.time()
    for file_name in os.listdir(log_path):
        # 获取文件名中的时间部分
        try:
            creation_str = file_name.split("_")[0] + "_" + file_name.split("_")[1] + "_" + file_name.split("_")[2]
            creation_time = time.mktime(time.strptime(creation_str, "%Y-%m-%d_%H-%M_%S"))

        except (ValueError, IndexError):
            # 文件名格式不正确,跳过
            continue

        # 如果文件创建时间早于 180 天前,就删除
        if current_time - creation_time > (30 * 24 * 60 * 60):
            file_path = os.path.join(log_path, file_name)
            os.remove(file_path)

def configure_logging():
    log_path = get_log_path(SERVER_ADDR,SERVER_PORT)
    os.makedirs(log_path, exist_ok=True)
    log_filename = get_log_filename()
    log_file = os.path.join(log_path, log_filename)
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', filename=log_file)

def get_log_filename():
    now = datetime.datetime.now()
    return now.strftime("%Y-%m-%d_%H-%M.log")

def create_new_log():
    log_path = get_log_path(SERVER_ADDR,SERVER_PORT)
    log_files = os.listdir(log_path)
    if len(log_files) >= 300:
        oldest_file = min(log_files)
        os.remove(os.path.join(log_path, oldest_file))
    log_filename = get_log_filename()
    log_filepath = os.path.join(log_path, log_filename)
    return log_filepath

def check_log_size(log_filepath):
    log_size = os.path.getsize(log_filepath)
    print("======================================================================")
    print("log_size:", log_size)
    if log_size > 2*1024*1024:
        # 创建新的日志文件
        new_log_filepath = create_new_log()
        return new_log_filepath
    return log_filepath

到了这里,关于Logging Writing Failed maximum recursion depth exceeded in comparison的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 彻底解决 git push 的【pack exceeds maximum allowed size】

    其实一个 Git 远程仓库除了限制单文件大小和仓库大小之外,还有一个单次推送的大小。比如 Github 它的单次推送大小大约是 5GB,如果超出就会报这个错误。 一些历史久远的仓库,虽然单文件大小和仓库大小没有超出限制,但是如果你一次性pull再push,就会超出这个限制。 解

    2024年02月15日
    浏览(42)
  • gitlab 仓库迁移,以及解决remote: fatal: pack exceeds maximum allowed size

    背景:是需要新建一个仓库,把老的仓库里面的git提交啥的都迁移过来。但是呢,总是失败,提醒大致意思就是提交的commit和tag太大了不行。 目录 方法一:命令迁移 方法二:脚本迁移 方法三:镜像 方法一:命令迁移 基本方法: 但是过程中会报错:  提示 方法二:脚本迁

    2024年02月07日
    浏览(45)
  • Maximum upload size exceeded; nested exception is java.lang.IllegalStateException解决办法

    一、问题描述 Springboot文件上传时报错:org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException: The field fileUrl exceeds its maximum permitted size of 10485760 bytes. 完整报错

    2024年02月11日
    浏览(28)
  • 【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

    本项目是个Springboot 项目,功能是要做一个文件上传,在测试时发现报错,上传的是一个 word 文件,大小是 1.25MB,报错内容如下: Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes. 详细报错内容如下图

    2024年03月15日
    浏览(31)
  • 算法 in Golang:Recursion(递归)

    while 没找到: if 当前项 is 宝石: return 宝石 else if 当前项 is 套娃: 打开这个套娃 if 当前项 is 宝石: return 宝石 else if 当前项 is 套娃: 打开这个套娃 if 当前项 is 宝石: ... ... 打开套娃 找到的是宝石,结束 得到的是一个套娃(重复操作,再次打开套娃,进行判断...) 递归

    2024年02月08日
    浏览(32)
  • git push超过100MB大文件失败(remote: fatal: pack exceeds maximum allowed size)

    push代码的时候,有时会出现如下问题 remote: fatal: pack exceeds maximum allowed size error: failed to push some refs to ‘git.n.xiaomi.com:fuzheng1/nl2sql.git’ 解决方案: 将本地 http.postBuffer 数值调整到GitHub服务对应的单次上传大小配置 查看是否配置成功

    2024年02月06日
    浏览(40)
  • uni-app、小程序项目,在分包后访问地址无法跳转,出现异常:RangeError: Maximum call stack size exceeded

    使用uni-app开发小程序,由于微信小程序对代码包体积有大小限制,故分包处理,同时也做了分包预加载 分包后,一个点击事件,同一个跳转地址,在浏览器下正常跳转,在微信开发者工具内出现如下报错 出现异常: RangeError: Maximum call stack size exceeded 如图: 原因:很有可能

    2024年02月12日
    浏览(28)
  • Spark SQL报错: Task failed while writing rows.

    今天运行 Spark 任务时报了一个错误,如下所示: ORC 仅在 HiveContext 中受支持,但这里使用 SQLContext。 SQLContext 存在一些问题,尝试使用 HiveContext。 使用以下配置来解决: native 和 hive 二选一, native 是基于 ORC1.4,表示使用 Spark SQL 提供的本地ORC实现方式。 hive 是基于 Hive 的

    2024年02月14日
    浏览(27)
  • ImportError: ERROR: recursion is detected during loading of “cv2“ binary extensions. Check OpenCV in

    1. import cv2错误 ImportError: ERROR: recursion is detected during loading of “cv2” binary extensions. Check OpenCV installation. 2. 解决 cv2版本太高,需要降低cv2版本 2.1 在anaconda环境下使用conda list查看当前cv2的版本为4.6.0.66,如下图: 2.2 使用pip uninstall opencv-python==4.6.0.66(指定卸载的当前cv2版本号)

    2024年02月12日
    浏览(56)
  • Unity UI与粒子 层级问题Camera depth Sorting Layer Order in Layer RenderQueue

    Unity游戏开发中,模型、界面、特效等,需要规划好 layer 的概念,涉及到摄像机(Camera)、画布(Canvas)、Shader等相关内容。 在 Unity 中,渲染顺序是由多个因素共同决定的,大致分为三层优先级: Camera depth、Sorting Layer/Order in Layer 和 RenderQueue 。 一般游戏项目,会创建至少两

    2024年02月08日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包