logrotate dateformat seems not supporting %H:%M:%S

gwecho huang picture gwecho huang · Jun 30, 2015 · Viewed 21.6k times · Source

I am newer to logrotate. when the configure comes to the property "dateformat",it seems that logrotate doesn't support strftime "%H" . here is the config:

{
    daily
    rotate  2
    size 3M
    missingok
    notifempty
    dateext
    dateformat -%Y%m%d_%H:%M:%S
    ...
}

the rotated file format tend to look like : uwsgi_dev.log-20150630_%H:%M:%S, but I want the exact "hour minutes and seconds".

thanks

Answer

Benedikt Köppel picture Benedikt Köppel · Dec 27, 2015

Support for %H was added in version 3.9.0. In earlier versions, logrotate did not support strftime "%H:

dateformat format_string: Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d and %s specifiers are allowed.

From the logrotate man page http://linux.die.net/man/8/logrotate

However, you can use %s in the dateformat string, which is the number of seconds since 1970-01-01. You can set dateformat -%Y%m%d-%s. This will produce unique filenames every time the log is rotated, so you can rotate the file multiple times a day. Unfortunately, the %s part will not be easy to read, but you can easily convert it back into a readable date with perl -e "print scalar(localtime(1451214849))".

On some systems, the date program allows to do such conversion easily with date -d @1451214849 (e.g. GNU date). On most systems (including e.g. Solaris date), you may have luck with syntax like date -d "1970-01-01 + 1451214849 sec". Note that Busybox date supports only the @ trick but not complex expressions of the second example.