您的位置:首页 > 汽车 > 时评 > 高清vga视频线_html网页怎么做_免费网站推广优化_广东做seo的公司

高清vga视频线_html网页怎么做_免费网站推广优化_广东做seo的公司

2024/10/22 4:48:07 来源:https://blog.csdn.net/weixin_44617651/article/details/142780691  浏览:    关键词:高清vga视频线_html网页怎么做_免费网站推广优化_广东做seo的公司
高清vga视频线_html网页怎么做_免费网站推广优化_广东做seo的公司

在 Django 1.2 中,标准日志模块有时会出现意想不到的行为,例如日志消息未按预期记录、日志级别未正确应用或日志格式错乱等。这些问题可能源于日志配置不当、日志模块被多次初始化、或日志模块被其他包覆盖等原因。下面是一些常见问题的排查方法和解决方案。

在这里插入图片描述

1、问题背景

在 Django 1.2 中,使用标准日志模块记录信息时遇到了一个奇怪的问题。有时候它可以正常工作,而有时候它却无法记录信息。

2、解决方案

为了解决这个问题,我们采取了以下步骤:

  1. 首先,我们检查了代码结构。代码结构如下:
/mysite/ (Django root)my_logging.py (logging configuration)settings.pyviews.py (global views)data_objects.py (objects only containing data, similar to POJO)uploader/ (application)views.py (uploader views) --> This is where I have problems
  1. 接着,我们检查了 my_logging.py 的代码:
import logging
import logging.handlers
from django.conf import settingsis_initialized = Falsedef init_logger():"""Initializes the logging for the application. Configure the rootlogger and creates the handlers following the settings. This function shouldnot be used directly from outside the module and called only once."""# Create the loggerserver_logger = logging.getLogger()server_logger.setLevel(logging.DEBUG)# Set the logging format for filesfiles_formatter = logging.Formatter(settings.LOGGING_FORMAT_FILE)# Rotating file handler for errorserror_handler = logging.handlers.RotatingFileHandler(settings.LOGGING_ERROR_FILE,maxBytes=settings.LOGGING_ERROR_FILE_SIZE,backupCount=settings.LOGGING_ERROR_FILE_COUNT,)error_handler.setLevel(logging.WARNING)error_handler.setFormatter(files_formatter)# Rotating file handler for infoinfo_handler = logging.handlers.RotatingFileHandler(settings.LOGGING_INFO_FILE,maxBytes=settings.LOGGING_INFO_FILE_SIZE,backupCount=settings.LOGGING_INFO_FILE_COUNT,)info_handler.setLevel(logging.INFO)info_handler.setFormatter(files_formatter)# Add the handlers to the loggerserver_logger.addHandler(info_handler)server_logger.addHandler(error_handler)# Init once at first import
if not is_initialized:init_logger()is_initialized = True
  1. 然后,我们检查了 uploader/views.py 中的部分代码:
#...
import mysite.my_logging
import logging
#... 
# The messages in the following view are written correctly :
@login_required
def delete(request, file_id):"""Delete the file corresponding to the given ID and confirm the deletion tothe user.@param request: the HTTP request object@type request: django.http.HttpRequest@return: django.http.HttpResponse - the response to the client (html)"""# Get the file object form the database and raise a 404 if not foundf = get_object_or_404(VideoFile, pk=file_id)# TODO: check if the deletion is successful# Get the video directorydir_path = os.path.dirname(f.file.path)# Delete the filef.delete()try:# Delete the video directory recursivelyshutil.rmtree(dir_path)logging.info("File \"%(file)s\" and its directory have been deleted by %(username)s",{'file': f.title,'username': request.user.username})messages.success(request, _('The video file "%s" has been successfully deleted.') % f.title)except OSError:logging.warning("File \"%(id)d\" directory cannot be completely deleted. Some files may still be there.",{'id': f.id,})messages.warning(request, _("The video file \"%s\" has been successfully deleted, but not its directory. There should not be any problem but useless disk usage.") % f.title)return HttpResponseRedirect(reverse('mysite.uploader.views.list'))
#...
# The messages in the following view are NOT written at all:
@csrf_exempt
def get_thumblist(request,file_id):"""This view can be called only by POST and with the id of a videofile ready for the scene editor.@param request: the HTTP request object. Must have POST as method.@type request: django.http.HttpRequest@return: django.http.HttpResponse - the response to the client (json)"""#TODO: Security, TESTlogging.info("Demand of metadata for file %(id)d received.",{'id': file_id,})if request.method == 'POST':if file_id:# Get the video file object form the database and raise a 404 if not foundvid = get_object_or_404(VideoFile, pk=file_id)# ...try:# ... file operationsexcept IOError:logging.error("Error when trying to read index file for file %(id)d !",{'id': file_id,})except TypeError:logging.error("Error when trying to parse index file JSON for file %(id)d !",{'id': file_id,})# ...logging.info("Returning metadata for file %(id)d.",{'id': file_id,})return HttpResponse(json,content_type="application/json")else:logging.warning("File %(id)d is not ready",{'id': file_id,})return HttpResponseBadRequest('file_not_ready')else:logging.warning("bad POST parameters")return HttpResponseBadRequest('bad_parameters')else:logging.warning("The GET method is not allowed")return HttpResponseNotAllowed(['POST'])
  1. 最后,我们检查了 settings.py 中的部分代码:
# ---------------------------------------
# Logging settings
# ---------------------------------------#: Minimum level for logging messages. If logging.NOTSET, logging is disabled
LOGGING_MIN_LEVEL = logging.DEBUG#: Error logging file path. Can be relative to the root of the project or absolute.
LOGGING_ERROR_FILE = os.path.join(DIRNAME,"log/error.log")#: Size (in bytes) of the error files
LOGGING_ERROR_FILE_SIZE = 10485760 # 10 MiB#: Number of backup error logging files
LOGGING_ERROR_FILE_COUNT = 5#: Info logging file path. Can be relative to the root of the project or absolute.
LOGGING_INFO_FILE = os.path.join(DIRNAME,"log/info.log")#: Size (in bytes) of the info files
LOGGING_INFO_FILE_SIZE = 10485760 # 10 MiB#: Number of backup error info files
LOGGING_INFO_FILE_COUNT = 5#: Format for the log files
LOGGING_FORMAT_FILE = "%(asctime)s:%(name)s:%(levelname)s:%(message)s"
  1. 通过对以上代码的检查,我们发现问题出现在 uploader/views.py 中的 get_thumblist 函数中。该函数中使用 logging.info('Demand of metadata for file %(id)d received.') 语句记录信息,但由于没有使用 logger 对象,导致信息没有被记录下来。

  2. 为了解决这个问题,我们将 get_thumblist 函数中的 logging.info('Demand of metadata for file %(id)d received.') 语句改为 logger.info('Demand of metadata for file %(id)d received.'),其中 logger 是一个 logging.getLogger() 函数返回的日志对象。

  3. 修改后的代码如下:

#...
import mysite.my_logging
import logging
logger = logging.getLogger('MySite.views')
#... 
# The messages in the following view are written correctly :
@login_required
def delete(request, file_id):"""Delete the file corresponding to the given ID and confirm the deletion tothe user.@param request: the HTTP request object@type request: django.http.HttpRequest@return: django.http.HttpResponse - the response to the client (html)"""# Get the file object form the database and raise a 404 if not foundf = get_object_or_404(VideoFile, pk=file_id)# TODO: check if the deletion is successful# Get the video directorydir_path = os.path.dirname(f.file

以上方法可以帮助解决 Django 1.2 中标准日志模块的异常行为问题。通过合理配置和调整日志模块,可以确保日志记录功能稳定、可靠地运行。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com