0

I have a logrotate configuration which is given below.

/var/log/Test.log {
    size 50k
    missingok
    rotate 10
    notifempty
    create 0640 root root
}

The file is rotating successfully once it reached the size of 50kb in the below format

Test.log.1
Test.log.2
Test.log.3

What I can do if I want to rotate files in the below format

Test_1.log
Test_2.log
Test_3.log
user3441151
  • 1,880
  • 6
  • 35
  • 79
  • Did you try the `extension` directive? As I understand it, with `extension log`, you'd end up with `Test.1.log`. I don't think your exact format is possible directly, but maybe with a `postrotate` script? – Benjamin W. Aug 03 '22 at 11:39
  • @BenjaminW. I don't have much knowledge about logrotate can you please guide me on what should I do for 'extension log'. – user3441151 Aug 03 '22 at 12:13
  • I know only what I read in the [manual](https://man7.org/linux/man-pages/man8/logrotate.8.html#CONFIGURATION_FILE_DIRECTIVES) about that directive. – Benjamin W. Aug 03 '22 at 13:05

2 Answers2

0

Try this one,it should help you.

/var/log/Test.log {
...

postrotate 
    /usr/bin/mv Test.log.1 Test_1.log
    ... 
endscript
}
rezshar
  • 570
  • 1
  • 6
  • 20
0

OK, so the problem here is that logrotate uses these filename extensions to keep track of its rotations. So if you have x.log, it inherently needs a predictable, sequential extension to keep track of what is what; i.e. x.log.1 is the first rotation, x.log.2 is the second, etc.

By renaming the file like that, you're moving the extension to the middle of the file and subsequently leaving each file with the same extension: .log. So as far as logrotate is concerned, it will think those are all unrelated files, not rotations of a previous log.

E.g., if you rename Test.log.1 to Test_1.log, and leave that file in the same folder, logrotate won't know that it's a rotation of Test.log and create a new Test.log.1 upon next rotation. And so on, and so on.

So the short answer is no, there is no logrotate directive that will rename a file like you want. You will have to create this functionality yourself using a postrotate script, and you will also have to (at some point) move these files out of the var/log/ directory once they're renamed, using either said postrotate script or the olddir directive.

The dateext directive might also be useful, as in my opinion date extensions are typically more user-friendly than numerical ones.

parttimeturtle
  • 1,125
  • 7
  • 22