← Back to Django Course | Chapter 15: Security, Caching & Deployment | Lesson 9 of 9

Django में Logging की मूल बातें

Logging एक ship की logbook जैसी है -- यह quietly लिख देती है कि क्या हुआ, ताकि अगर बाद में कुछ गलत हो, तो आप पीछे देखकर exactly समझ सकें कि इसकी वजह क्या थी।
Syntax
markup
import logging

logger = logging.getLogger(__name__)
logger.info('message')
logger.warning('message')
logger.error('message')

एक View में एक Logger पाना

हर module को नाम से अपना logger मिलता है, आमतौर पर __name__ पर set किया गया। इस पर .info() या .error() जैसे methods call करना उस severity level पर एक message record करता है।

उदाहरण: Getting a Logger in a View

Each module gets its own logger by name, usually set to __name__. Calling methods like .info() or .error() on it records a message at that severity level.

markup
import logging

logger = logging.getLogger(__name__)

def checkout_view(request):
    logger.info('Checkout started for user %s', request.user.id)
    return render(request, 'checkout.html')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

settings.py में LOGGING Configure करना

LOGGING setting handlers (messages कहां जाते हैं) और loggers (कौन से messages किस minimum level पर कहां send होते हैं) define करती है। Development के दौरान useful output पाने के लिए एक basic console handler काफी है।

उदाहरण: Configuring LOGGING in settings.py

The LOGGING setting defines handlers (where messages go) and loggers (which messages get sent where, at what minimum level). A basic console handler is enough to get useful output during development.

markup
# settings.py
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {'console': {'class': 'logging.StreamHandler'}},
    'root': {'handlers': ['console'], 'level': 'INFO'},
}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

सही Log Level चुनना

DEBUG development के दौरान detailed diagnostic info के लिए है, INFO routine events के लिए, WARNING किसी unexpected लेकिन non-fatal चीज़ के लिए, और ERROR एक failure के लिए जिसे attention चाहिए। सही level चुनना logs को useful रखता है, noisy नहीं।

Note: ERROR और उससे ऊपर उन चीज़ों के लिए रखें जिन्हें असल में किसी के attention की ज़रूरत है।

उदाहरण: Choosing the Right Log Level

DEBUG is for detailed diagnostic info during development, INFO for routine events, WARNING for something unexpected but non-fatal, and ERROR for a failure that needs attention. Picking the right level keeps logs useful instead of noisy.

markup
import logging

logger = logging.getLogger(__name__)

def process_payment(order):
    logger.debug('Processing payment for order %s', order.id)
    if order.total <= 0:
        logger.warning('Order %s has a non-positive total', order.id)
        return False
    logger.info('Payment succeeded for order %s', order.id)
    return True
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Debugging के लिए logging module के बजाय print() statements उपयोग करना, जो server एक visible terminal से run न होने पर गायब हो जाता है।
  2. हर चीज़ को same level पर log करना, जिससे routine info को real errors से अलग बताना असंभव हो जाता है।
  3. कभी एक logging handler configure न करना, इसलिए log messages production में silently कहीं नहीं जाते।
चैप्टर सारांश
  • Django की logging settings.py में LOGGING dictionary के through configured है, Python के standard logging module पर built।
  • Log messages में levels होते हैं (DEBUG, INFO, WARNING, ERROR, CRITICAL) जो उनकी severity बताते हैं।
  • Handlers decide करते हैं कि log messages कहां जाएं, जैसे console या एक file।
  • print() के बजाय logging उपयोग करना आपको levels, timestamps, और messages कहां end होते हैं इसका control देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.