# View logs in journalctl

We can monitor logs of a specific service using journalctl in real time, filter by time, or check past logs. Here are some useful commands:

**1\. View logs of a specific service**

```plaintext
journalctl -u <service-name>
```

Example for nginx:

```plaintext
journalctl -u nginx
```

**2\. Monitor logs in real-time (follow mode)**

```plaintext
journalctl -u <service-name> -f
```

Example:

```plaintext
journalctl -u nginx -f
```

This is similar to tail -f and will continuously display new log entries as they appear.

**3\. View logs since a specific time**

```plaintext
journalctl -u <service-name> --since "2024-02-01 12:00:00"
```

Or for a relative time (e.g., last 2 hours):

```plaintext
journalctl -u <service-name> --since "2 hours ago"
```

**4\. View logs until a specific time**

```plaintext
journalctl -u <service-name> --until "2024-02-07 18:00:00"
```

**5\. Filter logs between a time range**

```plaintext
journalctl -u <service-name> --since "2024-02-01" --until "2024-02-07"
```

**6\. Show the most recent logs (e.g., last 50 entries)**

```plaintext
journalctl -u <service-name> -n 50
```

**7\. View logs for the current boot**

```plaintext
journalctl -u <service-name> -b
```

Or for the previous boot:

```plaintext
journalctl -u <service-name> -b -1
```

**8\. Filter logs by priority level**

```plaintext
journalctl -u <service-name> -p <priority>
```

Priority levels:

• 0 (emerg)

• 1 (alert)

• 2 (crit)

• 3 (err)

• 4 (warning)

• 5 (notice)

• 6 (info)

• 7 (debug)

Example to view only errors and critical issues:

```plaintext
journalctl -u nginx -p err
```

**9\. Search for a specific keyword in logs**

```plaintext
journalctl -u <service-name> | grep "ERROR"
```

**10\. Show logs in JSON format (for structured logging)**

```plaintext
journalctl -u <service-name> -o json-pretty
```
