【发布时间】:2014-09-07 08:33:07
【问题描述】:
是否可以将命令和视图记录到单独的日志文件中,同时使用从命令和视图调用的通用函数?将记录器作为函数参数传递肯定不是一个好的设计。
settings.py
'loggers': {
'Web': {
'handlers': ['logfile_website'], # -> views.log
'Commands': {
'handlers': ['logfile_commands'], # -> commands.log
}
views.py -> 转到 views.log
log = logging.getLogger('Web')
def index(request):
log.info('In view')
my_common.common_function(1)
command.py -> 转到 commands.log
log = logging.getLogger('Commands')
class Command(BaseCommand):
def handle(self, *args, **options):
log.info('In command')
my_common.common_function(2)
my_common.py -> 根据调用者前往需要的地方
def common_function(param):
log = How to get logger based on current call stack?
log.info('In common function')
【问题讨论】: