Lesson 7 / الدرس 7
Logs you can actually answer questions with / سجلات تستطيع الإجابة بها فعلًا
The PHP course covered what to log. This is about the log as an object on a server: where it lives, why it eventually fills the disk and stops the site, and what makes the difference between a file you grep and a file you give up on.
غطّت دورة PHP ما يُسجَّل. وهذا عن السجل بوصفه شيئًا على خادم: أين يعيش، ولماذا يملأ القرص في النهاية فيوقف الموقع، وما الفرق بين ملف تبحث فيه وملف تستسلم أمامه.
A server has more than one log and they answer different questions. The access log says what was requested; the error log says what went wrong; your application log says what your code decided. Most investigations need two of the three, and the skill is knowing which.
The disk fills, and then nothing works
A log grows forever unless something stops it. A full disk is one of the worst failures a server has, because everything breaks at once and none of it says "disk full" — the database refuses writes, sessions cannot be saved, uploads fail, and the log that would tell you why cannot be written to either.
# /etc/logrotate.d/mysite — rotation is not optional, it is the
# difference between a log and a slow outage.
/home/site/storage/logs/*.log {
daily
rotate 14 # keep two weeks, then delete
compress # old ones shrink by roughly 90%
missingok # do not fail if there is nothing yet
notifempty
create 0640 site site
}
# And check, occasionally, before it matters:
$ df -h # how full is the disk
$ du -sh /home/site/storage/* # what is using it
Reading one when you need an answer
# What is failing right now, as it happens
$ tail -f storage/logs/php.log
# The commonest errors, most frequent first — this is the one that
# turns a wall of text into a list of things to fix
$ grep -o 'PHP [A-Za-z ]*error[^:]*' php.log | sort | uniq -c | sort -rn | head
# Everything that happened around a specific moment
$ grep '2026-08-18 14:2' php.log
# Which URLs are returning 500, and how often
$ awk '$9 == 500 {print $7}' access.log | sort | uniq -c | sort -rn
# Is one address doing something unusual
$ awk '{print $1}' access.log | sort | uniq -c | sort -rn | head
sort | uniq -c | sort -rn pattern is the one worth memorising — it turns any log into "the most common thing, then the next", which is almost always the question you actually had. sort | uniq -c | sort -rn هو ما يستحق الحفظ — فهو يحوّل أي سجل إلى «أشيع شيء، ثم التالي»، وهو السؤال الذي كان لديك فعلًا في معظم الأحيان.grep إيجادها: سطر واحد لكل حدث، وطابع زمني أولًا، وبادئة ثابتة تستطيع البحث عنها. فالرسالة المقسومة على أربعة أسطر وفي وسطها سطر فارغ غير مرئية لكل أداة في كتلة الكود السابقة.Check yourself / اختبر نفسك
1. Why is a full disk one of the worst server failures?
The database refuses writes, sessions cannot save, uploads fail, and the error log cannot be written to either. Rotation is what prevents it, and it is one config file.
2. How long should logs be kept?
It is a decision with two sides, and the second one is a legal question as much as a technical one. Two weeks is a common answer, not an automatic one.
3. What does sort | uniq -c | sort -rn give you from a log?
It is the terminal course's pipes doing real work, and it answers the question you usually had: what is happening most, and what next.
Score / النتيجة: 0 / 3