← Back to Apache Course | Chapter 6: Logging | Lesson 3 of 4

Custom Log Formats बनाना

Apache का log format fixed नहीं है -- LogFormat आपको exactly define करने देता है कि हर log line में कौन से fields किस order में दिखें, built-in formats के बिल्कुल भी capture न करने वाली चीज़ों समेत, जैसे response time या कोई specific request header।

LogFormat Directive

LogFormat "format-string" nickname percent-escape codes से बना एक named format define करता है, जिसे CustomLog फिर उस nickname से reference करता है। Apache दो built-in ship करता है: common (एक minimal पुराना format) और combined (referrer और user agent add करता है), दोनों already default config में define हैं।

आम Format Codes

%h client का IP address है, %t timestamp, %r request की पहली line, %>s final HTTP status code, %b bytes में response size, %{Referer}i और %{User-Agent}i नाम से specific request headers pull करते हैं, और %D microseconds में request serve करने में लगा समय देता है।

एक Custom Format क्यों बनाएं

built-in combined format में response time बिल्कुल शामिल नहीं है -- %D add करना सबसे common customizations में से एक है, क्योंकि यह आपको अलग application-level profiling की ज़रूरत के बिना सीधे log से slow requests ढूंढने देता है। एक specific header, जैसे load balancer से pass through की गई API key या request ID, log करना अपना खुद का format define करने की एक और common वजह है।

Machine Parsing के लिए Format

अगर logs ELK stack, Splunk, या किसी custom parser जैसे system में feed होते हैं, तो एक structured format -- यहाँ तक कि LogFormat's codes से पूरी तरह बना JSON जैसा कुछ भी -- अक्सर regular expressions से space-and-quote-delimited default format parse करने से reliably ingest करना ज़्यादा आसान होता है।

Note: अपने access log format में जल्दी %D (response time) add करें -- इसे log करने में कुछ cost नहीं होती और यह अक्सर users के complain करने से पहले किसी slow endpoint को spot करने के लिए सबसे useful field होता है।

उदाहरण: A custom format that includes response time

apacheconf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" timed

CustomLog /var/log/apache2/example.com-access.log timed
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# 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. एक LogFormat define करना लेकिन CustomLog में इसका nickname reference करना भूल जाना, ताकि site पहले से active जो भी format था वही इस्तेमाल करती रहे।
  2. combined जैसे built-in nickname को अलग fields से redefine करना, जो कहीं और मौजूद हर CustomLog directive के लिए format को चुपचाप बदल देता है जो original meaning मान रही थी।
  3. एक hand-rolled delimited format चुनना जब एक structured format (जैसे JSON) downstream में इसे actually पढ़ने वाले किसी भी tool में कहीं ज़्यादा reliably parse होता।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

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.