简体   繁体   English

Django中的python登录

[英]python logging in django

I am using the basic python logger in django and it seems to be workng well. 我在django中使用基本的python记录器,似乎工作得很好。 I have the logging setup in my setting.py as; 我在我的setting.py中有日志记录设置;

logging.baseConfig(level = logging.NOTSET,    
                   format='a format',    
                   datemt=' a datefmt',    
                   filename='path to log',    
                   filemode = 'a')    
logging.getLogger('').setLevel(logging.NOTSET)    

My question is with regard to propagating exceptions. 我的问题是关于传播例外。 In my code if I have a try/except clause and catch the exception so I can log it, what is the best way to then propagate that error so that I can redirect to my 500 page. 在我的代码中,如果我有一个try / except子句并捕获异常,以便可以对其进行日志记录,那么传播该错误以便重定向到我的500页的最佳方法是什么。 I have been using 我一直在用

try:
    do stuff
except Exception, e:
    logging.error(e)
    raise

but I find that this causes the exeption to be logged twice. 但是我发现这导致该异常记录两次。 Is there another way to do this or am I doing something wrong? 还有另一种方法可以执行此操作,还是我做错了什么?

Regards 问候
Andrew 安德鲁

There's no need to catch the exception just so you can log it. 有没有需要捕获异常所以您可以登录它。 You can log it and handle it, or else let it bubble up to some higher level which will log it and handle it. 您可以记录并处理它,或者让它冒泡到更高的水平,它将记录并处理它。 If you want to log exceptions which occur in some view, which you don't want to handle, then you can install some exception middleware which logs the exception and either returns a custom response which you determine, or None (to return whatever response Django would normally return). 如果您想记录某些视图中不想处理的异常 ,则可以安装一些异常中间件 ,该中间件记录异常并返回您确定的自定义响应,或者返回None (返回Django响应的任何响应)通常会返回)。

There's an example of extensible exception middleware here , which doesn't actually use logging but whose log_exception() method you could subclass to log the exception, or just use that snippet as a guide to provide your own exception middleware - it's basically just a class with a method called process_exception : 有可扩展的异常中间件的例子在这里 ,它实际上并没有使用记录,但其log_exception()方法,你可以继承来记录异常,或只使用该代码段为指导,以提供自己的异常中间件-它基本上只是一个类使用一个名为process_exception的方法:

class MyExceptionMiddleware:

    def process_exception(self, request, exception):
        #Do your logging here

Also, note that loggers have an exception() method which works like error() but includes traceback information in the log. 另外,请注意,记录器具有exception()方法,该方法的作用类似于error()但在日志中包含回溯信息。

There's a recipe: http://code.activestate.com/recipes/466332/ 有一个食谱: http : //code.activestate.com/recipes/466332/

In any somewhat complex application, you likely want to log and handle most exceptions. 在任何复杂的应用程序中,您可能希望记录并处理大多数异常。 The recipe shows a way to separate logging from handling, so that it is not necessary to explicitly invoke the logging mechanism in each try-except clause. 配方显示了一种将日志记录与处理分开的方法,因此不必在每个try-except子句中显式调用日志记录机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM