Use syslog to manage caddy std{out,err} on FreeBSD (#2652)

* Use syslog to manage caddy std{out,err} on FreeBSD

There is no good way to rotate the logfile created by the previous
FreeBSD rc.d script (it's the result of redirecting std{out,err} and
is held open by the shell).

This solves the problem by sending caddy's std{out,err} stream to
syslog, using the daemon command's builtin functionality.

It replaces the old `caddy_logfile` rc.conf variable with
`caddy_syslog_facility` (which defaults to 'local7') and
`caddy_syslog_level` (which defaults to 'notice').

By default, these messages will end up in /var/log/messages but can
be redirected as documented in the script's comments.

* Add info about rotating log with newsyslog

If you create a caddy specific logfile in `/var/log`, you should
rotate it.

This adds a bit of info to the dist/init/freebsd/README.md about
rotating that log file with newsyslog.
This commit is contained in:
George Hartzell 2019-07-18 12:58:40 -07:00 committed by Matt Holt
parent 120811e7f7
commit 580f7677ad
2 changed files with 54 additions and 8 deletions

44
dist/init/freebsd/README.md vendored Normal file
View File

@ -0,0 +1,44 @@
# Logging the caddy process's output:
Caddy's FreeBSD `rc.d` script uses `daemon` to run `caddy`; by default
it sends the process's standard output and error to syslog with the
`caddy` tag, the `local7` facility and the `notice` level.
The stock FreeBSD `/etc/syslog.conf` has a line near the top that
captures nearly anything logged at the `notice` level or higher and
sends it to `/var/log/messages`. That line will send the caddy
process's output to `/var/log/messages`.
The simplest way to send `caddy` output to a separate file is:
- Arrange to log the messages at a lower level so that they slip past
that early rule, e.g. add an `/etc/rc.conf` entry like
``` shell
caddy_syslog_level="info"
```
- Add a rule that catches them, e.g. by creating a
`/usr/local/etc/syslog.d/caddy.conf` file that contains:
```
# Capture all messages tagged with "caddy" and send them to /var/log/caddy.log
!caddy
*.* /var/log/caddy.log
```
Heads up, if you specify a file that does not already exist, you'll
need to create it.
- Rotate `/var/log/caddy.log` with `newsyslog` by creating a
`/usr/local/etc/newsyslog.conf/caddy.conf` file that contains:
```
# See newsyslog.conf(5) for details. Logs written by syslog,
# no need for a pidfile or signal, the defaults workg.
# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]
/var/log/caddy.log www:www 664 7 * @T00 J
```
There are many other ways to do it, read the `syslogd.conf` and
`newsyslog.conf` man pages for additional information.

View File

@ -29,8 +29,13 @@
# caddy_group (str): Set to "wheel" by default.
# Defines the group that caddy files will be attached to
#
# caddy_logfile (str) Set to "/var/log/caddy.log" by default.
# Defines where the process log file is written, this is not a web access log
# caddy_syslog_facility (str) Set to "local7" by default.
# Defines the syslog facility used to log output from the caddy process.
# This is NOT the web access log.
#
# caddy_syslog_level (str) Set to "notice" by default.
# Defines the syslog level used to log output from the caddy process.
# This is NOT the web access log.
#
# caddy_env (str) Set to "" by default.
# This allows environment variable to be set that may be required, for example when using "DNS Challenge" account credentials are required.
@ -49,7 +54,8 @@ load_rc_config ${name}
: ${caddy_bin_path="/usr/local/bin/caddy"}
: ${caddy_cpu="99%"} # was a bug for me that caused a crash within jails
: ${caddy_config_path="/usr/local/www/Caddyfile"}
: ${caddy_logfile="/var/log/caddy.log"}
: ${caddy_syslog_facility="local7"}
: ${caddy_syslog_level="notice"}
: ${caddy_user="root"}
: ${caddy_group="wheel"}
@ -62,7 +68,7 @@ fi
pidfile="/var/run/${name}.pid"
procname="${caddy_bin_path}" #enabled builtin pid checking for start / stop
command="/usr/sbin/daemon"
command_args="-p ${pidfile} /usr/bin/env ${caddy_env} ${procname} -cpu ${caddy_cpu} -log stdout -conf ${caddy_config_path} -agree -email ${caddy_cert_email} < /dev/null >> ${caddy_logfile} 2>&1"
command_args="-p ${pidfile} -T ${name} -l ${caddy_syslog_facility} -s ${caddy_syslog_level} /usr/bin/env ${caddy_env} ${procname} -cpu ${caddy_cpu} -log stdout -conf ${caddy_config_path} -agree -email ${caddy_cert_email} < /dev/null"
start_precmd="caddy_startprecmd"
@ -71,10 +77,6 @@ caddy_startprecmd()
if [ ! -e "${pidfile}" ]; then
install -o "${caddy_user}" -g "${caddy_group}" "/dev/null" "${pidfile}"
fi
if [ ! -e "${caddy_logfile}" ]; then
install -o "${caddy_user}" -g "${caddy_group}" "/dev/null" "${caddy_logfile}"
fi
}
required_files="${caddy_config_path}"