Django में Logging की मूल बातें
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.
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.
# 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 नहीं।
उदाहरण: 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.
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. #}
- Debugging के लिए logging module के बजाय
print()statements उपयोग करना, जो server एक visible terminal से run न होने पर गायब हो जाता है। - हर चीज़ को same level पर log करना, जिससे routine info को real errors से अलग बताना असंभव हो जाता है।
- कभी एक logging handler configure न करना, इसलिए log messages production में silently कहीं नहीं जाते।
- Django की logging
settings.pyमेंLOGGINGdictionary के 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 देता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: