Log Rotation क्या है
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 करने के लिए कहता है।
कितनी 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
/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). #}
- एक custom per-VirtualHost log path add करना बिना इसे logrotate के configuration में भी add किए, ताकि यह unbounded बढ़े जबकि default logs ठीक rotate होते रहें।
- postrotate reload भूल जाना, Apache को एक ऐसी file में लिखते हुए छोड़ना जो पहले से rename और compress हो चुकी है।
- "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: