如何从我的自定义Django命令获取日志输出到Apache日志中?
在settings.py中,我将记录器设置为登录到控制台,因为这是我找到的让Django应用将日志写入Apache日志的唯一方法。但是,如果我随后在Django命令中使用应用程序记录器并从命令行(例如python manage.py my_command
)运行该命令,则日志记录设置将写入实际控制台,而不是Apache日志。
我可以对日志记录进行不同的配置,甚至以不同的方式调用Django命令,以使命令日志记录写入Apache日志吗?
settings.py日志记录片段:
'handlers' : {
'console':{ #this logs to the Apache log when called from within the Django app e.g. from within views.py
'level':'INFO',
'class':'logging.StreamHandler',
'formatter':'standard',
}
}
....
'loggers' : {
'app_logger': {
'handlers': ['console'],
'level':'DEBUG',
}
}
my_command.py代码段:
....
class Command(BaseCommand):
....
def handle(self, *args, **options):
# do some useful work here
log = logging.getLogger("app_logger")
# when called from the command line, this statement is logged back to the console, not the Apache log
log.info("something happened")