# Logging and monitoring for your app

Once your app is live, you want to know if something is going wrong before a customer calls. This requires three things: **logs** (what happened), **error messages** (something went wrong) and **monitoring

## 1. Logs in Laravel

By default, Laravel writes to `storage/logs/laravel.log`. To log something yourself:

```php
use Illuminate\Support\Facades\Log;

Log::info('Factuur verstuurd', ['factuur_id' => $factuur->id]);
Log::warning('Betaling vertraagd', ['order' => $order->id]);
Log::error('PDF maken mislukt', ['exception' => $e]);
```

Tips:

- Provide context as an array, rather than pasting it into the text. That way, you can search it (**structured logging**).
- **Never** log passwords, tokens or full credit card or national insurance number details.
- Set `LOG_CHANNEL=daily` in production so that a new file is created every day and old files are deleted.

## 2. Error messages with Sentry

With a service such as **Sentry**, you’ll receive an email or notification containing the full stack trace as soon as an error occurs in production.

```bash
composer require sentry/sentry-laravel
php artisan sentry:publish --dsn=https://jouw-dsn@sentry.io/123
```

Any uncaught errors are then automatically reported. The **sentry-php-sdk** skill helps your assistant with the entire installation process.

## 3. Monitoring uptime

Set up an external service to check your site every minute. You’ll receive a notification if it doesn’t respond. By default, Laravel has a health route at `/up` that you can use for this.

## 4. Metrics: how healthy is your app?

For larger apps, you can collect **metrics** (time-series data) using tools such as Prometheus and Grafana, or your cloud provider’s monitoring service. The four **golden signals**:

| Signal | Question |
|---|---|
| Latency | How quickly does the app respond? |
| Traffic | How many requests are coming in? |
| Errors | How many go wrong? |
| Saturation | How busy are the CPU, memory and database? |

When looking at speed, focus on **percentiles**, not the average. **p95 = 300 ms

## 5. SLA, SLO and SLI in plain language

- **SLI** (indicator): what you measure, e.g. ‘percentage of error-free requests’.
- **SLO** (target): what you aim to achieve internally, e.g. “99.9% error-free per month”.
- **SLA** (agreement): what you contractually promise the customer, usually lower than your SLO.
- **Error budget:** the amount of time that can be lost. 99.9 per cent per month equates to approximately 43 minutes of downtime.

For a small app, a simple objective is enough: “the site is accessible 99.5 per cent of the time and any faults are investigated within one working day”.

## 6. If things go wrong

1. Confirm the issue (uptime alert, Sentry, logs).
2. First restore the system (revert to the previous version), then identify the cause.
3. Write down briefly what happened and what you’re going to change to make sure it doesn’t happen again. The ‘**post-mortem writing**’ skill can help with this.

Key metrics: **MTTD** (how quickly do you notice the issue), **MTTR** (how quickly is it resolved).

## Need help?

Monitoring is an essential part of a well-delivered app. I take care of this at **Custom Software and Web Applications**. Would you like me to look into recurring errors? Take a look at **AI Code Review**.

## Skills

- [Sentry PHP SDK] (/nl/skills/sentry-php-sdk)
- [Prometheus Configuration] (/nl/skills/prometheus-configuration)
- [Grafana Dashboards] (/nl/skills/grafana-dashboards)
- [Distributed Tracing] (/nl/skills/distributed-tracing)
- [SLO Implementation] (/nl/skills/slo-implementation)
- [AWS CloudWatch Investigation Skill] (/nl/skills/aws-cloudwatch-investigation)
- [Incident Runbook Templates] (/nl/skills/incident-runbook-templates)
- [Postmortem Writing] (/nl/skills/postmortem-writing)

## Related services

- [Maatwerk software en webapplicaties] (/nl/services#service-03)
- [Beoordeling van AI-code] (/nl/services#service-05)

