|
|
|
|
|
by solid_fuel
28 days ago
|
|
> An alternative, using `rc.d` to run daemons was just so hard to get logs working. I actually just did this for a web application on FreeBSD 15. It wasn't too bad, but I used `daemon` to send stdout & stderr into syslog and then I just added a syslog config to send that to it's own file. Roughly, I did this: # /usr/local/etc/rc.d/app:
# PROVIDE: app
# KEYWORD: shutdown
. /etc/rc.subr
name="app"
rcvar="app_enable"
load_rc_config $name
: ${app_enable:="NO"}
: ${app_user:="www"}
: ${app_database_url:=""}
app_command="/usr/local/bin/app"
cpidfile="/var/run/${name}/${name}.pid"
pidfile="/var/run/${name}/${name}d.pid"
logfile="/var/log/${name}.log"
command=/usr/sbin/daemon
command_args="-P ${pidfile} -p ${cpidfile} -S -t ${name}-super -T ${name} ${app_command} start"
start_precmd="${name}_prestart"
app_prestart()
{
# ... Set environment variables here with EXPORT, using values from rc.conf
export DATABASE_URL=${app_database_url}
# This is also a good place to use install(1) to create
# config files and log directories if they don't exist
}
run_rc_command "$1"
Note that -S to `daemon` instructs it to use syslog, and the -T sets the syslog tag which controls how messages are routed. See for more information on daemon: https://man.freebsd.org/cgi/man.cgi?query=daemon&apropos=0&s...After sending the log messages to syslog, it's a simple matter of routing them to the desired destination. That's easy enough to do by creating a file as follows: # /usr/local/etc/syslog.d/app.conf:
!app
*.* /var/log/app.log
in this, the !app indicates this rule is for all items tagged "app", then the *.* matches "all facilities" and "all levels". The last bit indicates the file to route those messages to. After that, it pretty much runs itself. You can start and stop the service with `service app start` and `service app stop` and all log messages get forwarded to `/var/log/app.log`. You do need to make sure the log file exists and has appropriate permissions (usually 600 or 644, owned by root & the wheel group).You can additionally set up log rotation with: # /usr/local/etc/newsyslog.conf.d/app.conf:
/var/log/blog.log 640 7 * @T00 Z
Oh and I believe you do need to reload syslog with `service syslogd reload` after this. |
|