Within two minutes, the IP 203.0.113.7 fails to log in to the admin account six times. Your log (the record of what happens in your app) records every page view (each page someone opens), but not a single failed login. So nobody knows, and no alert (an automatic warning) fires.
To make login attempts like these visible, you need security logging: recording the events that matter for security (who did what, when, from where, and whether it worked). The records are written in a form a program can search, so attacks get noticed and can be traced. Picture the visitor book at a security post. A book that only says "a visitor came" helps no one. A book that records the name, the time, the door, and "refused entry" lets the guard notice when the same person tries six doors in two minutes. But a visitor's safe PIN is never written in that book, because the book itself can be read by others. In your app, the visitor book is the security log. Each line in the book is one event (something that happened and was recorded), and the safe PIN is a password or token that must never be recorded.
What gets recorded
OWASP has a guide called the Logging Cheat Sheet, which lists the kinds of events that must be recorded. The main examples are below. The text in brackets is the event name this lesson uses.
- Failed logins (
auth.login_failed): the first sign of brute force or credential stuffing (trying email and password pairs leaked from other sites). - Denied access (
authz.denied): a user trying a page or data that isn't theirs, for example a normal user opening/admin/users. - Successful logins, logouts, password resets, password changes.
- Input that fails validation and requests that hit a rate limit.
- Admin actions: changing a user's role, deleting a user, changing configuration.
An ordinary page view isn't a security event. It belongs in the web server's access log, not the security log.
A brute force alert counts failed logins in the log. With none recorded, the six attempts from 203.0.113.7 at the start of this lesson pass unnoticed, and the alert never fires. In the 2025 edition of the OWASP Top 10, this problem has its own number: A09 Security Logging and Alerting Failures. In the 2021 edition it was A09 Security Logging and Monitoring Failures.
One structured log line
A structured log is written as JSON: one event per line, with the same fields (named pieces of data) on every line. So a program can filter "every auth.login_failed from this IP" without guessing at sentences. In Laravel, log lines go to a channel (a destination for log lines, with its own settings). This is a channel just for security, in JSON format:
// config/logging.php, inside 'channels'
'security' => [
'driver' => 'daily',
'path' => storage_path('logs/security.log'),
'level' => 'info',
'days' => 90,
'formatter' => Monolog\Formatter\JsonFormatter::class,
],
Then the login code calls that channel when a login fails:
// When a login fails
Log::channel('security')->warning('auth.login_failed', [
'email' => $request->input('email'),
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
'reason' => 'invalid_password',
]);
channel('security')writes to a separate file, one per day ('driver' => 'daily'):security.logplus the date, for examplesecurity-2026-10-08.log. Files are kept 90 days (days).JsonFormatterturns each event into one JSON line.warningis the level: how important a log line is (for example info, warning or error). The message,'auth.login_failed', is an event name that never changes. The details go in the context array (the second array).
The line written in the Lab for the last failed login:
{"message":"auth.login_failed","context":{"email":"[email protected]","ip":"203.0.113.7","user_agent":"Mozilla/5.0 (X11; Linux x86_64)","reason":"invalid_password"},"level":300,"level_name":"WARNING","channel":"production","datetime":"2026-10-08T14:03:47.000000+07:00","extra":{}}
What must never go in a log
Logs are read by many people and many tools: developers, the ops team (the team that runs the servers), and third-party log services. Whatever goes into a log is open to all of them. Never record:
- Passwords, wrong ones included. A wrong password is often one letter away from the real one.
- Session IDs, tokens, API keys: anyone who reads them can use them to get in.
- Card numbers and sensitive personal data. If you need it, keep a masked version like
**** 4242.
The first version of the login code in the Lab wrote this:
Log::channel('security')->warning('auth.login_failed', [
'email' => $request->input('email'),
'password' => $request->input('password'), // NEVER
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
'session_id' => $request->session()->getId(), // NEVER
'reason' => 'invalid_password',
]);
- The
passwordline writes the typed password to a file, as plain text. - The
session_idline writes the session ID. Someone reading the log could use it to pretend to be that visitor. - Remove those two lines. Don't log
$request->all()(everything in the request) either, because it includes the password.
From logs to alerts
Logs only help if something reads them. A SIEM (Security Information and Event Management) is a system that collects logs from many sources (the app, servers, firewalls, the WAF), looks for patterns, and sends alerts. Examples are Splunk, Elastic Security and IBM QRadar. The ELK Stack or Grafana with Loki are also common for collecting and watching logs.
The brute force alert rule in the Lab: 5 or more auth.login_failed from the same IP within 5 minutes. Once failed logins are recorded, the fifth attempt from 203.0.113.7, at 14:03:30, fires the alert. That limit of 5 attempts is the threshold. A threshold that's too low makes alerts fire often, from ordinary typos. A threshold that's too high lets slow attacks through.
⚠️ Logs that stay silent, leak, or get wiped
- Not recording failed logins and denied access. Without those two events, brute force and attempts at forbidden access go unseen.
- Recording passwords, tokens or session IDs. The log becomes a source of leaks. Pick the fields one by one, never
$request->all(). - Logs as free sentences. "Login failed for admin from 203.0.113.7" is hard for a program to count. Use JSON with a fixed event name.
- Logs only on the same server. An attacker who gets into the server can delete their tracks. Send logs somewhere separate that is append-only (can only be added to), and keep them at least 90 days.
TL;DR: Security logging records security events (things that happened) as a structured log: JSON, one event per line. A must: failed logins (auth.login_failed) and denied access (authz.denied), plus successful logins, password resets, failed validation and admin actions. Page views (each page someone opens) belong in the access log. Never record passwords, session IDs, tokens or full card numbers; pick the fields (the data you record) one by one. In Laravel, use a security channel (a log destination) with JsonFormatter and days 90 (kept for 90 days). Logs go to a SIEM (Security Information and Event Management), a system that looks for patterns in logs and sends alerts (warnings). For example: 5 failed logins from one IP within 5 minutes. Without logging and monitoring (watching the logs), attacks pass silently (OWASP A09).
⚡ Record the events that matter
Meaning: Security logging means recording the events (things that happened) that matter for security. Failed logins (auth.login_failed) and denied access (authz.denied) must be recorded, plus successful logins, password resets, and admin actions. If failed logins aren't recorded, brute force passes unnoticed.
⚡ One JSON line per event
Write:
Log::channel('security')->warning('auth.login_failed', [
'email' => $request->input('email'),
'ip' => $request->ip(),
]);
Meaning: Log::channel('security') writes to a channel (a log destination) just for security, and warning is the level (how important the line is). 'auth.login_failed' is an event name that never changes, and the details go in the context array (the second array). JsonFormatter in that channel's settings makes each event one JSON line a program can filter easily.
⚡ What must never go in a log
Guess first: what's wrong with Log::info("Login attempt", ['email' => $email, 'password' => $password]);?
The password gets recorded in the log, as plain text. Even a wrong password is often one letter away from the real one.
Meaning: Session IDs, tokens, and API keys must stay out of the log too, because anyone who reads them can use them to get in. Pick the fields (the data you record) one by one, and never record $request->all() (everything in the request, the password included).
⚡ From logs to alerts
Meaning: A SIEM (Security Information and Event Management) collects logs from many sources and fires an alert (a warning) when it finds an attack pattern. For example: 5 or more auth.login_failed from the same IP within 5 minutes. Send logs somewhere separate that is append-only (can only be added to), so an attacker who gets into the server can't delete their tracks.