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

Log Rotation क्या है

Log files तब तक हमेशा बढ़ती रहती हैं जब तक कुछ इन्हें manage न करे -- log rotation periodically पुराने logs compress करता है, एक certain age से पुराने delete करता है, और Apache को एक fresh file में लिखना start करा देता है, ताकि logs चुपचाप disk न भर दें।

logrotate

Linux पर, standard tool logrotate है, एक general-purpose utility (Apache-specific नहीं) जिसे ज़्यादातर distros पहले से cron/systemd timer से daily चलाते हैं। Apache का package आमतौर पर /etc/logrotate.d/apache2 (या httpd) पर एक ready-made config install करता है जो इसके default logs automatically rotate करता है -- defaults के लिए कोई setup नहीं चाहिए, हालांकि custom per-site log paths को इसमें manually add करना होता है।

एक logrotate Config

एक typical block log file का path specify करता है, कितनी बार rotate करना है इसके लिए daily/weekly, कितनी पुरानी copies रखनी हैं इसके लिए rotate 14, पुरानी copies gzip करने के लिए compress, और file अभी exist न करने या उसमें कुछ नया न होने पर errors से बचने के लिए missingok/notifempty।

Apache को नई File के बारे में बताना

सिर्फ log file rename करना काफी नहीं है -- Apache original file handle खुली रखता है और अब rename हुई (compress होने वाली) file में हमेशा के लिए लिखता रहेगा। postrotate script जो systemctl reload apache2 चलाता है (या Apache को एक graceful restart signal भेजता है) इसे rotation के बाद अपनी log files fresh reopen करने के लिए कहता है।

Warning: postrotate reload step भूल जाने का मतलब है Apache अपने file handle से rename/compress हुई पुरानी file में लिखता रहता है, इसलिए नई log file खाली रहती है जबकि disk usage actually improve नहीं होती।

कितनी History रखें

कितनी देर logs रखने हैं यह site की ज़रूरतों पर depend करता है -- किसी recent issue को debug करने के लिए आमतौर पर बस कुछ हफ्ते चाहिए होते हैं, लेकिन compliance या security requirements कभी-कभी महीनों की retention mandate करती हैं। Compressed logs raw वाली की space का बस एक हिस्सा लेते हैं, जो एक लंबी history रखना पहली नज़र में लगने से कहीं सस्ता बना देता है।

उदाहरण: A logrotate config for a custom per-site log

apacheconf
/var/log/apache2/example.com-*.log {
    daily
    rotate 14
    compress
    missingok
    notifempty
    postrotate
        systemctl reload apache2 > /dev/null 2>/dev/null || true
    endscript
}
{# 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. एक custom per-VirtualHost log path add करना बिना इसे logrotate के configuration में भी add किए, ताकि यह unbounded बढ़े जबकि default logs ठीक rotate होते रहें।
  2. postrotate reload भूल जाना, Apache को एक ऐसी file में लिखते हुए छोड़ना जो पहले से rename और compress हो चुकी है।
  3. "just in case" actually ज़रूरत से कहीं ज़्यादा retention set करना, disk space इस्तेमाल करते हुए जो बाद में disk भरने पर एक unrelated outage cause करता है।
चैप्टर सारांश
  • Access log हर request record करता है (कौन, क्या, कब, response code); error log जो गलत हुआ वह record करता है (startup failures, permission errors, PHP fatals) -- साथ मिलकर, ये लगभग किसी भी Apache problem के लिए पहली जगह हैं जहाँ देखना है।
  • LogFormat आपको exactly customize करने देता है कि क्या capture होता है (जैसे %D से response time), और logrotate logs को हमेशा के लिए बढ़ने से रोकता है -- लेकिन सिर्फ तब जब custom log paths इसमें add हों और हर rotation के बाद Apache reload हो।
🔒

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.