4

I need to use time and date as an extension of rotated logs. Right now I'm using dateext, but the problem is that I can't achieve rotation more than once on same date/day and I have to do rotation on hourly basis

Here is the logorotate configuration that I created:

/someDirectory/logs/*.log {
    nocompress
    notifempty
    copytruncate
    size 100M
    dateext
    olddir someDirectory/logs/archived
    rotate 10
}

What am I missing?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Muneeb Nasir
  • 2,414
  • 4
  • 31
  • 54

2 Answers2

3

You can use postrotate script like:

   someDirectory/logs/*.log /someDirectory/logs/*.log {
   nocompress
   notifempty
   copytruncate
   size 100M
   dateext
   olddir someDirectory/logs/archived
   rotate 10
   sharedscripts
   postrotate
        day=$(date +%Y%m%d)
        daytime=$(date +%Y%m%d-%H:%M:%S)
        mv somelog-$day /var/log/somelog-$daytime
   endscript
  }
Rupesh
  • 1,636
  • 13
  • 18
  • Thank you for your help. Can you please tell me how can I get log fileName in postrotate – Muneeb Nasir Feb 27 '15 at 13:38
  • What is ur log file name & path – Rupesh Feb 27 '15 at 13:42
  • @MuneebNasir Can you update your original post with the solution with getting the log filename? It would make this SO post much more helpful and applicable as often there are many different types of log files in a given location. Thanks! – Skylar Graika Apr 02 '18 at 14:53
  • For me it is not quite clear from the answer, what should I replace "somelog" with? The full path to the log like `/var/log/myservice.log` or just `myservice.log` because the name of the log to rotate is already stated on top of the script in my case. Will `somelog-$day` be created as a result of dateext? The answer is not self-explaining for those who is not that familiar to logrotate – rightaway717 Jan 24 '19 at 08:28
  • Considering the option that `logrotate` provides, I think that this `postrotate` statement is not necessary. As Joachim Wagner mentioned, `dateformat` (along with `dateext)` does this job. – ivanleoncz Jun 16 '20 at 00:53
2

Reading logrotate dateformat seems not supporting %H:%M:%S logrotate support for %H:%M:%S has arrived (though not yet on my favourite Linux distribution).

If this is not supported yet on your system and you only require some extra information to distinguish the hourly archive files you can use

dateformat -%Y%m%d-%s

or sequentially numbered logs pending clarification in the other answer how to make somelog a variable.

Joachim Wagner
  • 860
  • 7
  • 16