An utility to collect logs.
In the last weeks I've found myself using logrotate to organize log files of some scripts and tools... It's easy and flexible, but I find it a bit cumbersome: create a configuration file, create a cron entry... so I decided to code my own tool (mainly because I wanted to practice a bit with Go). loco capures standard out and redirect it to a file, rotating it at a specified interval.
In its simplest form you can use loco like this:
$ some-command | loco collect /path/to/log/fileloco will rotate the log file every day. If you want to use a different interval you can:
-
Create a configuration for a log file. For instance, if you want the log file to be rotated every week:
$ loco config -i 1w /path/to/log/file.log
A custom suffix can be specified using the
-sparameter:$ loco config -i 1w -s %Y%m%d /path/to/log/file.log
The following values will be replaced at runtime:
%%: literal%%c: number of times the file has been rotated%Y: year (four digits)%m: month (01 to 12)%d: day (01 to 31)%H: hour (00 to 23)%M: minute (00 to 59)%S: second (00 to 59)
Default value is
%c -
Change the defaults; if you want to set all the log files rotate, by default, every 3 days using a timestamp suffix:
$ loco defaults -i 3d -s %Y%m%d%H%M%S
-
Set the
LOCO_INTERVALenvironment variable to a valid interval -
Set the
LOCO_SUFFIXenvironment variable to a suffix
Valid intervals have the form \d+[mhdwM]
mstands for minutehstands for hourdstands for dayswstands for weeksMstands for months
To create or edit configurations:
$ loco config -i <interval> /path/to/file.logTo list active configurations:
$ loco listTo remove a configuration (not the log files):
$ loco remove /path/to/file.logTo show the defaults:
$ loco defaultsTo set defaults:
$ loco defaults -i <interval>$ loco collect /path/to/file.logThe -t or --tee makes loco work as the tee command: output is send to both log file and stdout.
loco uses the excellent kingpin library to parse command line and options. In order to have command completion you can add:
eval "$(loco --completion-script-bash)"to your .bashrc file or
eval "$(loco --completion-script-zsh)"to .zshrc.
Custom suffix- Save rotate history in state?
- Max rotations
- Clone configurations
- Autocompletion hints
- Post rotate actions:
- Gzip
- Move/copy to another directory
- Move/copy to a remote destination (S3 bucket, ...)