Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 580
gwecho huang Avatar asked Jun 30 '15 08:06

gwecho huang


People also ask

Can logrotate run hourly?

Logrotate does not support hourly schedule, but this is an easy task to accomplish. Create separate directory to store hourly logrotate configuration files. Create main logrotate configuration file that will read configuration files from designated directory.

How do I check logrotate configuration?

You can check the settings of logrotate , usually in /etc/logrotate. conf . Modern distros have a specific logrotate configuration file in the /etc/logrotate. d directory.

What is Maxage in logrotate?

Default Value. maxage days. Sets the number of days that a log file is kept. At the end of this period, the log file is deleted.

How do I add a file to logrotate?

Add an entry for your log fileAt the end of logrotate. conf, add the full path to your log file followed by open and close curly brackets. There are many options you can add like the frequency to rotate "daily/weekly/monthly" and the number of rotations to keep "rotate 2/rotate 3".


1 Answers

Support for %H was added in version 3.9.0, for %M and %S - 3.9.2 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.

like image 65
Benedikt Köppel Avatar answered Sep 16 '22 12:09

Benedikt Köppel