25

This is my settings module:

LOGGING = {
  'version': 1,
  'disable_existing_loggers': False,
  'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': '/django-python/django/testapp/testapp.log',
    },
  },
  'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
  },
}

and this is my code in a view file:

import logging
logger = logging.getLogger(__name__)
logger.info("this is an error message!!")

I am getting the previous logs from various modules but not the above log entry "this is an error message".

solarissmoke
  • 30,039
  • 14
  • 71
  • 73
Teja Reddy
  • 561
  • 1
  • 4
  • 12

4 Answers4

37

Your logging configuration only captures logs within the django namespace.

This line:

logger = logging.getLogger(__name__)

... tells the logger to use your module's name as the namespace for these logs (docs). If your module is called mymodule, then you can catch these logs by adding something like this to your logging configuration:

'loggers': {
   'django' : {...},
   'mymodule': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
},
solarissmoke
  • 30,039
  • 14
  • 71
  • 73
  • 5
    Just to add a little here, if you're placing this logging code in a view in your users module, `__name__` will output `users.view`. So using solarissmoke's example above you would change `mymodule` to `users.view` – Braden Holt May 21 '18 at 18:29
  • 1
    "modules" confused me, it is basically the app name (several apps can be part of a django project) that needs to be used, so within settings.py I had to use appname.views instead of "mymodule" – Greg Holst Jun 06 '19 at 16:23
11

you should add logger configuration due to your application name - something like

    'your_app_name': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },

By now you have declared only logger for django default messages (like system errors)


Notice that the level of logger messages is important so when you are using

    logger.info("this is an error message!!")

method to print out message your logger's level should be INFO or more strict

m.antkowicz
  • 13,268
  • 18
  • 37
10

You could add the "catch all" logger to the loggers section:

'loggers': {
    '': {
        'handlers': ['file'],
        'level': 'DEBUG',
    }
}

It will catch all the log messages which are not caught by Django's default loggers.

andrey
  • 588
  • 1
  • 8
  • 14
2
import logging
logger = logging.getLogger(__name__)

after add:

logging.basicConfig(
    level = logging.DEBUG,
    format = '%(name)s %(levelname)s %(message)s',
)

or just add settings.py :

import logging

    logging.basicConfig(
        level = logging.DEBUG,
        format = '%(name)s %(levelname)s %(message)s',
    )

we may change format to:

format = '"%(levelname)s:%(name)s:%(message)s"  ',

or

format = '%(name)s %(asctime)s %(levelname)s %(message)s',
Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
Sérgio
  • 6,966
  • 1
  • 48
  • 53