Refactor and drop '-n' option for rotation
The '-n' option is commonly used for remote servers, so let's consolidate the log rotation under the '-r' flag instead, with the same syntax as previously established in syslog.conf Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
parent
76b6981467
commit
bdb1d45f90
52
man/logger.1
52
man/logger.1
@ -34,11 +34,10 @@
|
|||||||
.Nm
|
.Nm
|
||||||
.Op Fl hsv
|
.Op Fl hsv
|
||||||
.Op Fl f Ar FILE
|
.Op Fl f Ar FILE
|
||||||
.Op Fl n Ar SIZE
|
|
||||||
.Op Fl p Ar PRIO
|
.Op Fl p Ar PRIO
|
||||||
.Op Fl r Ar NUM
|
.Op Fl r Ar SIZE:NUM
|
||||||
.Op Fl t Ar TAG
|
.Op Fl t Ar TAG
|
||||||
.Qq Ar message
|
.Op Ar MESSAGE
|
||||||
.Sh DESCRIPTIOMN
|
.Sh DESCRIPTIOMN
|
||||||
.Nm
|
.Nm
|
||||||
can be used to send messages to the system log from a UNIX shell, or
|
can be used to send messages to the system log from a UNIX shell, or
|
||||||
@ -47,27 +46,59 @@ script.
|
|||||||
This program follows the usual UNIX command line syntax:
|
This program follows the usual UNIX command line syntax:
|
||||||
.Bl -tag -width Ds
|
.Bl -tag -width Ds
|
||||||
.It Fl f Ar FILE
|
.It Fl f Ar FILE
|
||||||
File to write log messages to, instead of syslog.
|
Log file to write messages to, instead of syslog daemon.
|
||||||
.It Fl h
|
.It Fl h
|
||||||
Show program help.
|
Show program help.
|
||||||
.It Fl n Ar SIZE
|
|
||||||
Number of bytes before rotating when logging to a file, default: 200 kB.
|
|
||||||
.It Fl p Ar PRIO
|
.It Fl p Ar PRIO
|
||||||
Priority, numeric or facility.level pair.
|
Priority, numeric or
|
||||||
.It Fl r Ar NUM
|
.Ar facility.level
|
||||||
Number of rotated files to keep when logging to a file, default: 5.
|
pair.
|
||||||
|
.It Fl r Ar SIZE:NUM
|
||||||
|
Controls log file rotation.
|
||||||
|
.Ar SIZE
|
||||||
|
denotes number of bytes before rotating, default: 200 kB.
|
||||||
|
.Ar NUM
|
||||||
|
denotes number of rotated files to keep when logging to a file, default:
|
||||||
|
5.
|
||||||
.It Fl s
|
.It Fl s
|
||||||
Log to stderr as well as the system log.
|
Log to stderr as well as the system log.
|
||||||
.It Fl t Ar TAG
|
.It Fl t Ar TAG
|
||||||
Log using the specified tag, default: username.
|
Log using the specified tag, default: username.
|
||||||
.It Fl v
|
.It Fl v
|
||||||
Show program version.
|
Show program version.
|
||||||
|
.It Ar MESSAGE
|
||||||
|
Log message to write. Remember to use single/double qoutes if calling
|
||||||
|
.Nm
|
||||||
|
from a shell prompt due to expansion the shell does.
|
||||||
.El
|
.El
|
||||||
.Sh EXAMPLES
|
.Sh EXAMPLES
|
||||||
.Bd -unfilled -offset left
|
.Bd -unfilled -offset left
|
||||||
logger -t dropbear -p auth.notice "Successful login for user 'admin' from 1.2.3.4"
|
logger -t dropbear -p auth.notice "Successful login for user 'admin' from 1.2.3.4"
|
||||||
logger -t udhcpc -f /tmp/script.log "New lease 1.2.3.200 obtained for interface eth0"
|
logger -t udhcpc -f /tmp/script.log "New lease 1.2.3.200 obtained for interface eth0"
|
||||||
.Ed
|
.Ed
|
||||||
|
.Sh FILES
|
||||||
|
.Bl -tag -width /var/run/log -compact
|
||||||
|
.It Ar FILE
|
||||||
|
If a log file is enabled, using
|
||||||
|
.Fl f Ar FILE ,
|
||||||
|
then this file is opened and written to by
|
||||||
|
.Nm .
|
||||||
|
With log file rotation,
|
||||||
|
.Fl r Ar SIZE:NUM ,
|
||||||
|
this means
|
||||||
|
.Nm
|
||||||
|
also creates
|
||||||
|
.Pa FILE.1 FILE.2 FILE.3.gz
|
||||||
|
etc.
|
||||||
|
.It Pa /var/run/log
|
||||||
|
Socket to connect to for communicating with
|
||||||
|
.Xr syslogd 8 .
|
||||||
|
In older implementations
|
||||||
|
.Pa /dev/log
|
||||||
|
was used. This means this version of
|
||||||
|
.Nm
|
||||||
|
is not compatible with older releases, or other (local) syslog servers.
|
||||||
|
.El
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr syslog 3
|
.Xr syslog 3
|
||||||
.Xr syslogd 8
|
.Xr syslogd 8
|
||||||
@ -83,5 +114,6 @@ and be extended upon in the sysklogd project.
|
|||||||
.Sh STANDARDS
|
.Sh STANDARDS
|
||||||
The
|
The
|
||||||
.Nm
|
.Nm
|
||||||
command is expected to be IEEE Std 1003.2 ("POSIX.2") compatible.
|
command is expected to be IEEE Std 1003.2 ("POSIX.2") compatible, with
|
||||||
|
extensions for RFC5424 and log file rotation.
|
||||||
.Sh AVAILABILITY
|
.Sh AVAILABILITY
|
||||||
|
65
src/logger.c
65
src/logger.c
@ -39,6 +39,7 @@
|
|||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include "compat.h"
|
||||||
|
|
||||||
static const char version_info[] = PACKAGE_NAME " v" PACKAGE_VERSION;
|
static const char version_info[] = PACKAGE_NAME " v" PACKAGE_VERSION;
|
||||||
|
|
||||||
@ -123,6 +124,19 @@ static int checksz(FILE *fp, off_t sz)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int logit(int level, char *buf, size_t len)
|
||||||
|
{
|
||||||
|
if (buf[0]) {
|
||||||
|
syslog(level, "%s", buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((fgets(buf, len, stdin)))
|
||||||
|
syslog(level, "%s", buf);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int flogit(char *logfile, int num, off_t sz, char *buf, size_t len)
|
static int flogit(char *logfile, int num, off_t sz, char *buf, size_t len)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
@ -155,19 +169,6 @@ reopen:
|
|||||||
return fclose(fp);
|
return fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int logit(int level, char *buf, size_t len)
|
|
||||||
{
|
|
||||||
if (buf[0]) {
|
|
||||||
syslog(level, "%s", buf);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((fgets(buf, len, stdin)))
|
|
||||||
syslog(level, "%s", buf);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int parse_prio(char *arg, int *f, int *l)
|
static int parse_prio(char *arg, int *f, int *l)
|
||||||
{
|
{
|
||||||
char *ptr;
|
char *ptr;
|
||||||
@ -202,14 +203,14 @@ static int usage(int code)
|
|||||||
"\n"
|
"\n"
|
||||||
"Write MESSAGE (or stdin) to syslog, or file (with logrotate)\n"
|
"Write MESSAGE (or stdin) to syslog, or file (with logrotate)\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -? This help text\n"
|
" -p PRIO Log message priority (numeric or facility.level pair)\n"
|
||||||
" -p PRIO Priority (numeric or facility.level pair)\n"
|
|
||||||
" -t TAG Log using the specified tag (defaults to user name)\n"
|
" -t TAG Log using the specified tag (defaults to user name)\n"
|
||||||
" -s Log to stderr as well as the system log\n"
|
" -s Log to stderr as well as the system log\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -f FILE File to write log messages to, instead of syslog\n"
|
" -f FILE Log file to write messages to, instead of syslog daemon\n"
|
||||||
" -n SIZE Number of bytes before rotating, default: 200 kB\n"
|
" -r S:R Log file rotation, default: 200 kB max \e[4ms\e[0mize, 5 \e[4mr\e[0motations\n"
|
||||||
" -r NUM Number of rotated files to keep, default: 5\n"
|
"\n"
|
||||||
|
" -? This help text\n"
|
||||||
" -v Show program version\n"
|
" -v Show program version\n"
|
||||||
"\n"
|
"\n"
|
||||||
"This version of logger is distributed as part of sysklogd.\n"
|
"This version of logger is distributed as part of sysklogd.\n"
|
||||||
@ -218,6 +219,26 @@ static int usage(int code)
|
|||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void parse_rotation(char *optarg, off_t *size, int *num)
|
||||||
|
{
|
||||||
|
char buf[100];
|
||||||
|
char *c;
|
||||||
|
int sz = 0, cnt = 0;
|
||||||
|
|
||||||
|
strlcpy(buf, optarg, sizeof(buf));
|
||||||
|
c = strchr(buf, ':');
|
||||||
|
if (c) {
|
||||||
|
*c++ = 0;
|
||||||
|
cnt = atoi(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
sz = strtobytes(buf);
|
||||||
|
if (sz > 0)
|
||||||
|
*size = sz;
|
||||||
|
if (cnt)
|
||||||
|
*num = cnt;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int c, rc, num = 5;
|
int c, rc, num = 5;
|
||||||
@ -228,23 +249,19 @@ int main(int argc, char *argv[])
|
|||||||
char *ident = NULL, *logfile = NULL;
|
char *ident = NULL, *logfile = NULL;
|
||||||
char buf[512] = "";
|
char buf[512] = "";
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "?f:n:p:r:st:v")) != EOF) {
|
while ((c = getopt(argc, argv, "?f:p:r:st:v")) != EOF) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'f':
|
case 'f':
|
||||||
logfile = optarg;
|
logfile = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'n':
|
|
||||||
size = atoi(optarg);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'p':
|
case 'p':
|
||||||
if (parse_prio(optarg, &facility, &level))
|
if (parse_prio(optarg, &facility, &level))
|
||||||
return usage(1);
|
return usage(1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
num = atoi(optarg);
|
parse_rotation(optarg, &size, &num);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 's':
|
case 's':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user