【问题标题】:Django logging with multiple handlers for commands and views, with shared functions带有多个命令和视图处理程序的 Django 日志记录,具有共享功能
【发布时间】: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')

【问题讨论】:

    标签: python django logging


    【解决方案1】:

    您可以使用单个处理程序,该处理程序使用基于环境的文件名。例如,在 POSIX 上,

    LOG_FILENAME = os.environ.get('LOGFILE', 'views.log')
    
    # then use the filename in your configuration
    

    然后使用

    运行命令
    LOGFILE=commands.log python manage.py syncdb
    

    我在这里使用syncdb 作为命令的一个示例。

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多