简体   繁体   中英

django logging - can't print logs to file

I'm trying to setup Logging in my Django project... my first attempt, supposedly very simple, has been a failure so far.

This is my LOGGING in settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
            'style': '{',
        },
        'simple': {
            'format': '{levelname} {message}',
            'style': '{',
        },
    },
    'filters': {
        # 'special': {
        #     '()': 'project.logging.SpecialFilter',
        #     'foo': 'bar',
        # },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            # 'filters': ['special']
        },
        'file':{
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': './logs/log_file1.log',
            'formatter': 'verbose',
        },
        'apps_handler':{
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': './logs/apps_logs.log',
            'formatter': 'verbose',
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        **'Equipment': {
            'handlers': ['console', 'apps_handler'],
            'level': 'INFO',
            # 'filters': ['special']
        }**
    }
}

As suggested here: Django: logging only for my project's apps I'm trying to isolate my apps' logs in a separated file, and since my app is named Equipment I made the Equipment logger

As a first try in my Equipment app in the views.py file I tried this:

**import logging

logger = logging.getLogger(__name__)**

@login_required
def dashboard(request):
    types = Type.objects.all().order_by('name')
    form = SubTypeForm(request.POST or None)
    mezzi_list = SubType.objects.filter(type__name__icontains='mezzi').order_by('name')
    form.fields['subType'].queryset = mezzi_list

    **logging.info('hello darling: %s' % mezzi_list)**

    context = {
        'types': types,
        'form': form
    }
    if request.method == 'POST':
        if form.is_valid():
            subType = form.cleaned_data['subType']
            type = subType.type
            return HttpResponseRedirect(reverse('equipment:item_create', args=(type.id, subType.id)))
        else:
            return render(request, 'equipment/equipment_dashboard.html', context)

    return render(request, 'equipment/equipment_dashboard.html', context)

A very simple call to info log with a variable passed in, just to test the configuration everytime I access the dashboard view.

Problem. The file is always empty? what am I missing here?

Thank you very much for any help

Bad spelling mistake. it's:

logger.info('some message')

and not:

logging.info('some message')

By the way I've posted my final solution for making a log settings on an app level: https://stackoverflow.com/questions/16876045/django-logging-only-for-my-projects-apps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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