2020-02-28 11:16:45 -07:00
---
title: log (Caddyfile directive)
---
# log
Enables and configures HTTP request logging (also known as access logs).
2020-11-25 10:46:58 -07:00
The `log` directive applies to the host/port of the site block it appears in, not any other part of the site address (e.g. path).
2021-08-31 13:39:15 -04:00
- [Syntax ](#syntax )
- [Output modules ](#output-modules )
- [stderr ](#stderr )
- [stdout ](#stdout )
- [discard ](#discard )
- [file ](#file )
- [net ](#net )
- [Format modules ](#format-modules )
- [console ](#console )
- [json ](#json )
- [single_field ](#single-field )
- [filter ](#filter )
- [delete ](#delete )
- [replace ](#replace )
- [ip_mask ](#ip-mask )
- [Examples ](#examples )
2020-02-28 11:16:45 -07:00
## Syntax
2020-05-17 16:32:12 -04:00
```caddy-d
2020-02-28 11:16:45 -07:00
log {
output < writer_module > ...
format < encoder_module > ...
level < level >
}
```
2021-01-16 01:26:22 +08:00
- **output** configures where to write the logs. See [Output modules ](#output-modules ) below. Default: `stderr`
2020-09-29 10:17:27 -05:00
- **format** describes how to encode, or format, the logs. See [Format modules ](#format-modules ) below. Default: `console` if `stdout` is detected to be a terminal, `json` otherwise.
2021-08-31 13:39:15 -04:00
- **level** is the minimum entry level to log. Default: `INFO` . Note that access logs currently only emit `INFO` and `ERROR` level logs.
2020-02-28 11:16:45 -07:00
### Output modules
The **output** subdirective lets you customize where logs get written. It appears within a `log` block.
#### stderr
Standard error (console, default).
2020-05-17 16:32:12 -04:00
```caddy-d
2020-02-28 11:16:45 -07:00
output stderr
```
#### stdout
Standard output (console).
2020-05-17 16:32:12 -04:00
```caddy-d
2020-03-26 10:14:38 +07:00
output stdout
2020-02-28 11:16:45 -07:00
```
#### discard
No output.
2020-05-17 16:32:12 -04:00
```caddy-d
2020-02-28 11:16:45 -07:00
output discard
```
#### file
A file. By default, log files are rotated ("rolled") to prevent disk space exhaustion.
2020-05-17 16:32:12 -04:00
```caddy-d
2020-02-28 11:16:45 -07:00
output file < filename > {
roll_disabled
roll_size < size >
roll_keep < num >
roll_keep_for < days >
}
```
- **< filename> ** is the path to the log file.
- **roll_disabled** disables log rolling. This can lead to disk space depletion, so only use this if your log files are maintained some other way.
2020-05-07 12:46:49 -07:00
- **roll_size** is the size at which to roll the log file. The current implementation supports megabyte resolution; fractional values are rounded up to the next whole megabyte. For example, `1.1MiB` is rounded up to `2MiB` . Default: `100MiB`
2020-02-28 11:16:45 -07:00
- **roll_keep** is how many log files to keep before deleting the oldest ones. Default: `10`
2020-05-07 12:46:49 -07:00
- **roll_keep_for** is how long to keep rolled files as a [duration string ](/docs/conventions#durations ). The current implementation supports day resolution; fractional values are rounded up to the next whole day. For example, `36h` (1.5 days) is rounded up to `48h` (2 days). Default: `2160h` (90 days)
2020-02-28 11:16:45 -07:00
#### net
2021-05-25 17:25:25 -06:00
A network socket. If the socket goes down, it will dump logs to stderr while it attempts to reconnect.
2020-02-28 11:16:45 -07:00
2020-05-17 16:32:12 -04:00
```caddy-d
2021-05-25 17:25:25 -06:00
output net < address > {
dial_timeout < duration >
}
2020-02-28 11:16:45 -07:00
```
- **< address> ** is the [address ](/docs/conventions#network-addresses ) to write logs to.
2021-05-25 17:25:25 -06:00
- **< dial_timeout> ** is how long to wait for a successful connection to the log socket. Log emissions may be blocked for up to this long if the socket goes down.
2020-02-28 11:16:45 -07:00
### Format modules
The **format** subdirective lets you customize how logs get encoded (formatted). It appears within a `log` block.
In addition to the syntax for each individual encoder, these common properties can be set on most encoders:
2020-05-17 16:32:12 -04:00
```caddy-d
format < encoder_module > {
2020-02-28 11:16:45 -07:00
message_key < key >
level_key < key >
time_key < key >
name_key < key >
caller_key < key >
stacktrace_key < key >
line_ending < char >
time_format < format >
level_format < format >
}
```
2020-07-29 21:31:13 +08:00
- **message_key** The key for the message field of the log entry. Default: `msg`
2020-02-28 11:16:45 -07:00
- **level_key** The key for the level field of the log entry. Default: `level`
2020-07-29 21:31:13 +08:00
- **time_key** The key for the time field of the log entry. Default: `ts`
2020-02-28 11:16:45 -07:00
- **name_key** The key for the name field of the log entry (i.e. the name of the logger itself). Default: `name`
- **caller_key** The key for the caller field of the log entry.
- **stacktrace_key** The key for the stacktrace field of the log entry.
- **line_ending** The line endings to use.
- **time_format** The format for timestamps.
- **level_format** The format for levels.
#### console
The console encoder formats the log entry for human readability while preserving some structure.
2020-05-17 16:32:12 -04:00
```caddy-d
2020-04-05 01:13:48 +03:00
format console
2020-02-28 11:16:45 -07:00
```
#### json
Formats each log entry as a JSON object.
2020-05-17 16:32:12 -04:00
```caddy-d
2020-04-05 01:13:48 +03:00
format json
2020-02-28 11:16:45 -07:00
```
#### single_field
2021-08-31 13:39:15 -04:00
< span class = "warning" > ⚠️ This format is deprecated, and will be removed in a future version.< / span >
2020-02-28 11:16:45 -07:00
Writes only a single field from the structure log entry. Useful if one of the fields has all the information you need.
2020-05-17 16:32:12 -04:00
```caddy-d
2020-04-05 01:13:48 +03:00
format single_field < field_name >
2020-02-28 11:16:45 -07:00
```
- **< field_name> ** is the name of the field whose value to use as the log entry.
2020-10-01 14:27:14 -04:00
#### filter
2020-02-28 11:16:45 -07:00
2020-10-01 14:27:14 -04:00
Wraps another encoder module, allowing per-field filtering.
2020-02-28 11:16:45 -07:00
2020-10-01 14:27:14 -04:00
```caddy-d
format filter {
wrap < encode_module > ...
fields {
< field > < filter > ...
}
}
```
Nested fields can be referenced by representing a layer of nesting with `>` . In other words, for an object like `{"a":{"b":0}}` , the inner field can be referenced as `a>b` .
The following fields are fundamental to the log and cannot be filtered because they are added by the underlying logging library as special cases: `ts` , `level` , `logger` , and `msg` .
These are the available filters:
##### delete
Marks a field to be skipped from being encoded.
```caddy-d
< field > delete
```
2021-04-08 14:29:15 -04:00
##### replace
Marks a field to be replaced with the provided string at encoding time.
```caddy-d
< field > replace < replacement >
```
2020-10-01 14:27:14 -04:00
##### ip_mask
Masks IP addresses in the field using a CIDR mask, i.e. the number of bytes from the IP to retain, starting from the left side. There is separate configuration for IPv4 and IPv6 addresses.
```caddy-d
< field > ip_mask {
ipv4 < cidr >
ipv6 < cidr >
}
```
2020-02-28 11:16:45 -07:00
## Examples
Enable access logging (to the console):
2020-05-17 16:32:12 -04:00
```caddy-d
2020-02-28 11:16:45 -07:00
log
```
2021-08-31 13:39:15 -04:00
2020-02-28 11:16:45 -07:00
Write logs to a file (with log rolling, which is enabled by default):
2020-05-17 16:32:12 -04:00
```caddy-d
2020-02-28 11:16:45 -07:00
log {
output file /var/log/access.log
}
```
2021-08-31 13:39:15 -04:00
2020-02-28 11:16:45 -07:00
Customize log rolling:
2020-05-17 16:32:12 -04:00
```caddy-d
2020-02-28 11:16:45 -07:00
log {
output file /var/log/access.log {
roll_size 1gb
roll_keep 5
2020-04-28 12:13:25 -04:00
roll_keep_for 720h
2020-02-28 11:16:45 -07:00
}
}
```
2021-08-31 13:39:15 -04:00
Use Common Log Format (CLF):
< span class = "warning" > ⚠️ The `single_field` format is deprecated and will be removed in a future version. To encode logs in common log format, please use the [`format-encoder` ](https://github.com/caddyserver/format-encoder ) plugin.</ span >
2020-02-28 11:16:45 -07:00
2020-05-17 16:32:12 -04:00
```caddy-d
2020-02-28 11:16:45 -07:00
log {
format single_field common_log
}
```
2020-10-01 14:27:14 -04:00
2021-08-31 13:39:15 -04:00
2020-10-01 14:27:14 -04:00
Delete the Authorization request header from the logs:
```caddy-d
log {
format filter {
wrap console
fields {
request>headers>Authorization delete
}
}
}
```
2021-08-31 13:39:15 -04:00
2020-11-02 18:03:28 -05:00
Mask the remote address from the request, keeping the first 16 bytes (i.e. 255.255.0.0) for IPv4 addresses, and the first 64 bytes from IPv6 addresses, and also deletes the `common_log` field which would normally contain an unmasked IP address:
2020-10-01 14:27:14 -04:00
```caddy-d
log {
format filter {
wrap console
fields {
2020-11-02 18:03:28 -05:00
common_log delete
2020-10-01 14:27:14 -04:00
request>remote_addr ip_mask {
ipv4 24
ipv6 32
}
}
}
}
2021-01-16 01:26:22 +08:00
```