English 中文(简体)
粉碎/Django:在serv子下 log鱼的标识,在 log子下存档ache
原标题:Python/Django: log to console under runserver, log to file under Apache

当I m管理我的Django在manage.pyprserver<下运行时,我怎么能向哥尔(如)发出追踪信息,但当I m管理阿帕奇的 app时,这些电文已发送到一个记录档案?

我审查了Django 伐木,尽管我对先进用途的灵活性和可预见性印象深刻,但我仍对如何处理我的简单使用案例感到困惑。

最佳回答

打印文本在磁带上时将张贴在httpd s误差记录上。 您可以直接使用<条码>,也可以使用<条码>。

print >>sys.stderr,  Goodbye, cruel world! 
问题回答

您可在<代码>、py文档中核对伐木。

例如:

if DEBUG:
    # will output to your console
    logging.basicConfig(
        level = logging.DEBUG,
        format =  %(asctime)s %(levelname)s %(message)s ,
    )
else:
    # will output to logging file
    logging.basicConfig(
        level = logging.DEBUG,
        format =  %(asctime)s %(levelname)s %(message)s ,
        filename =  /my_log_file.log ,
        filemode =  a 
    )

然而,这取决于设立德国马克,也许你不想担心它是如何建立的。 见。 Edit:上述例子来自Django 1.1项目,Django的伐木组合自该版本以来有所变化。

我这样做:

伐木。 会议:

[loggers]
keys=root,applog
[handlers]
keys=rotateFileHandler,rotateConsoleHandler

[formatters]
keys=applog_format,console_format

[formatter_applog_format]
format=%(asctime)s-[%(levelname)-8s]:%(message)s

[formatter_console_format]
format=%(asctime)s-%(filename)s%(lineno)d[%(levelname)s]:%(message)s

[logger_root]
level=DEBUG
handlers=rotateFileHandler,rotateConsoleHandler

[logger_applog]
level=DEBUG
handlers=rotateFileHandler
qualname=simple_example

[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=applog_format
args=( applog.log ,  a , 10000, 9)

[handler_rotateConsoleHandler]
class=StreamHandler
level=DEBUG
formatter=console_format
args=(sys.stdout,)

测试应用:

import logging
import logging.config

def main():
    logging.config.fileConfig( logging.conf )
    logger = logging.getLogger( applog )

    logger.debug( debug message )
    logger.info( info message )
    logger.warn( warn message )
    logger.error( error message )
    logger.critical( critical message )
    #logging.shutdown()

if __name__ ==  __main__ :
    main()

您可以很容易地使用<代码>tagalog (https://github.com/dorkates/tagalog)。

例如,尽管标准肺部单元写上了以对应方式开启的立案物体,但应用引擎模块(https://github.com/dorkized/tagalog/blob/master/tagalog_appengine.py)却凌驾于这一行为之上,而是使用。 INFO。

为了将这种行为纳入一个上诉引擎项目,我们只能:

import tagalog.tagalog_appengine as tagalog
tagalog.log( whatever message , [ whatever , tags ])

您可以扩大模块范围,不遇到任何困难就超标。

这在我的当地非常好。 py, Save the messing up the regular Hawaii:

from .settings import *

LOGGING[ handlers ][ console ] = {
     level :  DEBUG ,
     class :  logging.StreamHandler ,
     formatter :  verbose 
}
LOGGING[ loggers ][ foo.bar ] = {
     handlers : [ console ],
     propagate : False,
     level :  DEBUG ,
}




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签