How to combine logrotate with Log4j “rolling” file-appender?I want my logs sent to my mail with logrotateLogrotate Successful, original file goes back to original sizeHow to compress and clean logs with logrotate but not rotate themHow do I tell logrotate what files to rotate with cron?Logrotate: rotate all log files on specific size and rotate hourlyLogrotate doesn't work from cron on CentOSlogrotate cron job not rotating certain logsError with gunzip during logrotateDaily logrotate error creating unique temp file: Permission deniedLogs are not deleted and compressed in elasticsearch nodes

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

To string or not to string

Do VLANs within a subnet need to have their own subnet for router on a stick?

TGV timetables / schedules?

Writing rule stating superpower from different root cause is bad writing

What do you call a Matrix-like slowdown and camera movement effect?

How to format long polynomial?

How can bays and straits be determined in a procedurally generated map?

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

What defenses are there against being summoned by the Gate spell?

What's the point of deactivating Num Lock on login screens?

Why are electrically insulating heatsinks so rare? Is it just cost?

Do I have a twin with permutated remainders?

What is the offset in a seaplane's hull?

How does one intimidate enemies without having the capacity for violence?

Why do falling prices hurt debtors?

How old can references or sources in a thesis be?

Problem of parity - Can we draw a closed path made up of 20 line segments...

Fencing style for blades that can attack from a distance

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

Can I ask the recruiters in my resume to put the reason why I am rejected?

Font hinting is lost in Chrome-like browsers (for some languages )

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?



How to combine logrotate with Log4j “rolling” file-appender?


I want my logs sent to my mail with logrotateLogrotate Successful, original file goes back to original sizeHow to compress and clean logs with logrotate but not rotate themHow do I tell logrotate what files to rotate with cron?Logrotate: rotate all log files on specific size and rotate hourlyLogrotate doesn't work from cron on CentOSlogrotate cron job not rotating certain logsError with gunzip during logrotateDaily logrotate error creating unique temp file: Permission deniedLogs are not deleted and compressed in elasticsearch nodes






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















We have a Java-application, that uses log4j and rotates its own logs daily:



  • The currently-used log-file is /var/log/foo/foo.log

  • It is moved into /var/log/foo/foo.log.YYYY-MM-dd when the day changes

We'd rather not change its configuration...



However, we do want to:



  1. Compress the file once it has been renamed

  2. Only retain a certain number of them

Though it wouldn't be difficult to write a cron-job to do both, we'd rather stay in the framework of logrotate...



I create the following /etc/logrotate.d/foo:



/var/log/foo/foo.log.* 
daily
rotate 2
compress
delaycompress
missingok
notifempty



but it does not do anything:



reading config file /etc/logrotate.d/foo

Handling 1 logs

rotating pattern: /var/log/foo/foo.log.* after 1 days (2 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/foo/foo.log.2017-06-28
log does not need rotating
considering log /var/log/foo/foo.log.2017-06-29
log does not need rotating
considering log /var/log/foo/foo.log.2017-06-30
log does not need rotating
considering log /var/log/foo/foo.log.2017-07-01
log does not need rotating
considering log /var/log/foo/foo.log.2017-07-02
log does not need rotating
considering log /var/log/foo/foo.log.2017-07-03
log does not need rotating
considering log /var/log/foo/foo.log.2017-07-04
log does not need rotating


How do I make it compress the files and delete the oldest ones, when their total number exceeds 2?










share|improve this question






























    3















    We have a Java-application, that uses log4j and rotates its own logs daily:



    • The currently-used log-file is /var/log/foo/foo.log

    • It is moved into /var/log/foo/foo.log.YYYY-MM-dd when the day changes

    We'd rather not change its configuration...



    However, we do want to:



    1. Compress the file once it has been renamed

    2. Only retain a certain number of them

    Though it wouldn't be difficult to write a cron-job to do both, we'd rather stay in the framework of logrotate...



    I create the following /etc/logrotate.d/foo:



    /var/log/foo/foo.log.* 
    daily
    rotate 2
    compress
    delaycompress
    missingok
    notifempty



    but it does not do anything:



    reading config file /etc/logrotate.d/foo

    Handling 1 logs

    rotating pattern: /var/log/foo/foo.log.* after 1 days (2 rotations)
    empty log files are not rotated, old logs are removed
    considering log /var/log/foo/foo.log.2017-06-28
    log does not need rotating
    considering log /var/log/foo/foo.log.2017-06-29
    log does not need rotating
    considering log /var/log/foo/foo.log.2017-06-30
    log does not need rotating
    considering log /var/log/foo/foo.log.2017-07-01
    log does not need rotating
    considering log /var/log/foo/foo.log.2017-07-02
    log does not need rotating
    considering log /var/log/foo/foo.log.2017-07-03
    log does not need rotating
    considering log /var/log/foo/foo.log.2017-07-04
    log does not need rotating


    How do I make it compress the files and delete the oldest ones, when their total number exceeds 2?










    share|improve this question


























      3












      3








      3








      We have a Java-application, that uses log4j and rotates its own logs daily:



      • The currently-used log-file is /var/log/foo/foo.log

      • It is moved into /var/log/foo/foo.log.YYYY-MM-dd when the day changes

      We'd rather not change its configuration...



      However, we do want to:



      1. Compress the file once it has been renamed

      2. Only retain a certain number of them

      Though it wouldn't be difficult to write a cron-job to do both, we'd rather stay in the framework of logrotate...



      I create the following /etc/logrotate.d/foo:



      /var/log/foo/foo.log.* 
      daily
      rotate 2
      compress
      delaycompress
      missingok
      notifempty



      but it does not do anything:



      reading config file /etc/logrotate.d/foo

      Handling 1 logs

      rotating pattern: /var/log/foo/foo.log.* after 1 days (2 rotations)
      empty log files are not rotated, old logs are removed
      considering log /var/log/foo/foo.log.2017-06-28
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-06-29
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-06-30
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-07-01
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-07-02
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-07-03
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-07-04
      log does not need rotating


      How do I make it compress the files and delete the oldest ones, when their total number exceeds 2?










      share|improve this question
















      We have a Java-application, that uses log4j and rotates its own logs daily:



      • The currently-used log-file is /var/log/foo/foo.log

      • It is moved into /var/log/foo/foo.log.YYYY-MM-dd when the day changes

      We'd rather not change its configuration...



      However, we do want to:



      1. Compress the file once it has been renamed

      2. Only retain a certain number of them

      Though it wouldn't be difficult to write a cron-job to do both, we'd rather stay in the framework of logrotate...



      I create the following /etc/logrotate.d/foo:



      /var/log/foo/foo.log.* 
      daily
      rotate 2
      compress
      delaycompress
      missingok
      notifempty



      but it does not do anything:



      reading config file /etc/logrotate.d/foo

      Handling 1 logs

      rotating pattern: /var/log/foo/foo.log.* after 1 days (2 rotations)
      empty log files are not rotated, old logs are removed
      considering log /var/log/foo/foo.log.2017-06-28
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-06-29
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-06-30
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-07-01
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-07-02
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-07-03
      log does not need rotating
      considering log /var/log/foo/foo.log.2017-07-04
      log does not need rotating


      How do I make it compress the files and delete the oldest ones, when their total number exceeds 2?







      logrotate log4j






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 25 '18 at 11:19









      kasperd

      26.8k1252104




      26.8k1252104










      asked Jul 5 '17 at 20:05









      Mikhail T.Mikhail T.

      1,4491431




      1,4491431




















          3 Answers
          3






          active

          oldest

          votes


















          1














          I really don't think you are going be able to do what you want with logrotate without some external commands. Also, the pattern /var/log/foo/foo.log.* would treat each individual file as if it was a separate log to be rotated, not as a set of rotated files.




          man logrotate



          Please use wildcards with caution. If you specify *, logrotate will rotate all files, including previously rotated ones.




          Anyway, maybe something like this? Just set your rotate condition that will never be reached since log4j is already handling the rotation partly. Then put the rest in a prerotate script?



          /var/log/foo/foo.log 
          size 100G # something big that will never be reached/rotated since log4j does this.
          missingok
          notifempty
          # delete the other logs over 7 days old
          prerotate
          find /var/log/foo/ -name 'foo.log.*' -mtime +7 -delete
          nice gzip /var/log/foo/foo.log.*
          endscript






          share|improve this answer

























          • Thanks. This just seems like a way to execute arbitrary command -- there is nothing logrotate-ish about it :) I may as well just do a cron-job instead...

            – Mikhail T.
            Jul 5 '17 at 20:51











          • Yeah, like I said, I don't think you are going to find what you want.

            – Zoredache
            Jul 5 '17 at 21:52











          • Setting an unreachable rotate option doesn't work, logrotate script options (e.g. prerotate) are only run if at least one file is rotated (see man logrotate). I came up with a solution and posted it below.

            – Ryan Fisher
            Apr 3 at 19:09


















          1














          I needed to accomplish this behavior because the app teams wanted to maintain control of log and filename format as well as rotation schedule with log4j. If you read man logrotate you'll notice that all the pre/post/lastaction scripts only execute when at least one log was rotated. Thus, setting something unreachable like size 999G does not work.



          Instead, what I did was set the copy and extension options so that:



          1. The original log file was never touched (it's managed by log4j)

          2. The logs rotated by logrotate have a different extension that I can filter on

          /var/log/<service>/<service>.log 
          copy # don't touch the orig. log file, it's managed by log4j
          extension .log # End rotated files in ".log" so we can find and delete them
          rotate 1
          hourly
          missingok

          prerotate
          find /var/log/<service>/ -iname "*service.log.*" ! -iname "*.gz" -exec gzip ; &&
          aws s3 sync /var/log/<service>/ s3://<bucket>/<environment>/<ec2_id>/<service>/ --exclude "*" --include "*<service>*.gz" &&
          find /var/log/<service>/ -iname "*<service>.log.*" -mtime +7 -delete
          endscript

          lastaction
          find /var/log/<service>/ -iname "*<service>.*.log" -delete
          endscript



          Yes, I could just make a cron job. My reasoning for using logrotate is that I keep all of the log rotation config management in a single Ansible role and in the future, I'll be making the case to disable log4j rotation and use logrotate instead which will be a simple change.






          share|improve this answer
































            0














            The Log4j Rolling File Appenders and the logrotate are not compatible. The log4j rotation will interfere with logrotate.



            First you need to find the log4j configuration file for the application and replace the Rolling File Appenders with simple File Appenders. This will stop the log4j from rotating the files. Then you can use the logrotate to rotate the log files as you wanted.



            If you can't modify the application's log4j configuration you can create your own one and specify this in the classpath when the application is stared.



            -Dlog4j.configurationFile=path/to/log4j2.xml


            Ref: https://logging.apache.org/log4j/2.0/faq.html#config_location






            share|improve this answer























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "2"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f859561%2fhow-to-combine-logrotate-with-log4j-rolling-file-appender%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              I really don't think you are going be able to do what you want with logrotate without some external commands. Also, the pattern /var/log/foo/foo.log.* would treat each individual file as if it was a separate log to be rotated, not as a set of rotated files.




              man logrotate



              Please use wildcards with caution. If you specify *, logrotate will rotate all files, including previously rotated ones.




              Anyway, maybe something like this? Just set your rotate condition that will never be reached since log4j is already handling the rotation partly. Then put the rest in a prerotate script?



              /var/log/foo/foo.log 
              size 100G # something big that will never be reached/rotated since log4j does this.
              missingok
              notifempty
              # delete the other logs over 7 days old
              prerotate
              find /var/log/foo/ -name 'foo.log.*' -mtime +7 -delete
              nice gzip /var/log/foo/foo.log.*
              endscript






              share|improve this answer

























              • Thanks. This just seems like a way to execute arbitrary command -- there is nothing logrotate-ish about it :) I may as well just do a cron-job instead...

                – Mikhail T.
                Jul 5 '17 at 20:51











              • Yeah, like I said, I don't think you are going to find what you want.

                – Zoredache
                Jul 5 '17 at 21:52











              • Setting an unreachable rotate option doesn't work, logrotate script options (e.g. prerotate) are only run if at least one file is rotated (see man logrotate). I came up with a solution and posted it below.

                – Ryan Fisher
                Apr 3 at 19:09















              1














              I really don't think you are going be able to do what you want with logrotate without some external commands. Also, the pattern /var/log/foo/foo.log.* would treat each individual file as if it was a separate log to be rotated, not as a set of rotated files.




              man logrotate



              Please use wildcards with caution. If you specify *, logrotate will rotate all files, including previously rotated ones.




              Anyway, maybe something like this? Just set your rotate condition that will never be reached since log4j is already handling the rotation partly. Then put the rest in a prerotate script?



              /var/log/foo/foo.log 
              size 100G # something big that will never be reached/rotated since log4j does this.
              missingok
              notifempty
              # delete the other logs over 7 days old
              prerotate
              find /var/log/foo/ -name 'foo.log.*' -mtime +7 -delete
              nice gzip /var/log/foo/foo.log.*
              endscript






              share|improve this answer

























              • Thanks. This just seems like a way to execute arbitrary command -- there is nothing logrotate-ish about it :) I may as well just do a cron-job instead...

                – Mikhail T.
                Jul 5 '17 at 20:51











              • Yeah, like I said, I don't think you are going to find what you want.

                – Zoredache
                Jul 5 '17 at 21:52











              • Setting an unreachable rotate option doesn't work, logrotate script options (e.g. prerotate) are only run if at least one file is rotated (see man logrotate). I came up with a solution and posted it below.

                – Ryan Fisher
                Apr 3 at 19:09













              1












              1








              1







              I really don't think you are going be able to do what you want with logrotate without some external commands. Also, the pattern /var/log/foo/foo.log.* would treat each individual file as if it was a separate log to be rotated, not as a set of rotated files.




              man logrotate



              Please use wildcards with caution. If you specify *, logrotate will rotate all files, including previously rotated ones.




              Anyway, maybe something like this? Just set your rotate condition that will never be reached since log4j is already handling the rotation partly. Then put the rest in a prerotate script?



              /var/log/foo/foo.log 
              size 100G # something big that will never be reached/rotated since log4j does this.
              missingok
              notifempty
              # delete the other logs over 7 days old
              prerotate
              find /var/log/foo/ -name 'foo.log.*' -mtime +7 -delete
              nice gzip /var/log/foo/foo.log.*
              endscript






              share|improve this answer















              I really don't think you are going be able to do what you want with logrotate without some external commands. Also, the pattern /var/log/foo/foo.log.* would treat each individual file as if it was a separate log to be rotated, not as a set of rotated files.




              man logrotate



              Please use wildcards with caution. If you specify *, logrotate will rotate all files, including previously rotated ones.




              Anyway, maybe something like this? Just set your rotate condition that will never be reached since log4j is already handling the rotation partly. Then put the rest in a prerotate script?



              /var/log/foo/foo.log 
              size 100G # something big that will never be reached/rotated since log4j does this.
              missingok
              notifempty
              # delete the other logs over 7 days old
              prerotate
              find /var/log/foo/ -name 'foo.log.*' -mtime +7 -delete
              nice gzip /var/log/foo/foo.log.*
              endscript







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 3 at 19:02









              Ryan Fisher

              1387




              1387










              answered Jul 5 '17 at 20:47









              ZoredacheZoredache

              112k30231379




              112k30231379












              • Thanks. This just seems like a way to execute arbitrary command -- there is nothing logrotate-ish about it :) I may as well just do a cron-job instead...

                – Mikhail T.
                Jul 5 '17 at 20:51











              • Yeah, like I said, I don't think you are going to find what you want.

                – Zoredache
                Jul 5 '17 at 21:52











              • Setting an unreachable rotate option doesn't work, logrotate script options (e.g. prerotate) are only run if at least one file is rotated (see man logrotate). I came up with a solution and posted it below.

                – Ryan Fisher
                Apr 3 at 19:09

















              • Thanks. This just seems like a way to execute arbitrary command -- there is nothing logrotate-ish about it :) I may as well just do a cron-job instead...

                – Mikhail T.
                Jul 5 '17 at 20:51











              • Yeah, like I said, I don't think you are going to find what you want.

                – Zoredache
                Jul 5 '17 at 21:52











              • Setting an unreachable rotate option doesn't work, logrotate script options (e.g. prerotate) are only run if at least one file is rotated (see man logrotate). I came up with a solution and posted it below.

                – Ryan Fisher
                Apr 3 at 19:09
















              Thanks. This just seems like a way to execute arbitrary command -- there is nothing logrotate-ish about it :) I may as well just do a cron-job instead...

              – Mikhail T.
              Jul 5 '17 at 20:51





              Thanks. This just seems like a way to execute arbitrary command -- there is nothing logrotate-ish about it :) I may as well just do a cron-job instead...

              – Mikhail T.
              Jul 5 '17 at 20:51













              Yeah, like I said, I don't think you are going to find what you want.

              – Zoredache
              Jul 5 '17 at 21:52





              Yeah, like I said, I don't think you are going to find what you want.

              – Zoredache
              Jul 5 '17 at 21:52













              Setting an unreachable rotate option doesn't work, logrotate script options (e.g. prerotate) are only run if at least one file is rotated (see man logrotate). I came up with a solution and posted it below.

              – Ryan Fisher
              Apr 3 at 19:09





              Setting an unreachable rotate option doesn't work, logrotate script options (e.g. prerotate) are only run if at least one file is rotated (see man logrotate). I came up with a solution and posted it below.

              – Ryan Fisher
              Apr 3 at 19:09













              1














              I needed to accomplish this behavior because the app teams wanted to maintain control of log and filename format as well as rotation schedule with log4j. If you read man logrotate you'll notice that all the pre/post/lastaction scripts only execute when at least one log was rotated. Thus, setting something unreachable like size 999G does not work.



              Instead, what I did was set the copy and extension options so that:



              1. The original log file was never touched (it's managed by log4j)

              2. The logs rotated by logrotate have a different extension that I can filter on

              /var/log/<service>/<service>.log 
              copy # don't touch the orig. log file, it's managed by log4j
              extension .log # End rotated files in ".log" so we can find and delete them
              rotate 1
              hourly
              missingok

              prerotate
              find /var/log/<service>/ -iname "*service.log.*" ! -iname "*.gz" -exec gzip ; &&
              aws s3 sync /var/log/<service>/ s3://<bucket>/<environment>/<ec2_id>/<service>/ --exclude "*" --include "*<service>*.gz" &&
              find /var/log/<service>/ -iname "*<service>.log.*" -mtime +7 -delete
              endscript

              lastaction
              find /var/log/<service>/ -iname "*<service>.*.log" -delete
              endscript



              Yes, I could just make a cron job. My reasoning for using logrotate is that I keep all of the log rotation config management in a single Ansible role and in the future, I'll be making the case to disable log4j rotation and use logrotate instead which will be a simple change.






              share|improve this answer





























                1














                I needed to accomplish this behavior because the app teams wanted to maintain control of log and filename format as well as rotation schedule with log4j. If you read man logrotate you'll notice that all the pre/post/lastaction scripts only execute when at least one log was rotated. Thus, setting something unreachable like size 999G does not work.



                Instead, what I did was set the copy and extension options so that:



                1. The original log file was never touched (it's managed by log4j)

                2. The logs rotated by logrotate have a different extension that I can filter on

                /var/log/<service>/<service>.log 
                copy # don't touch the orig. log file, it's managed by log4j
                extension .log # End rotated files in ".log" so we can find and delete them
                rotate 1
                hourly
                missingok

                prerotate
                find /var/log/<service>/ -iname "*service.log.*" ! -iname "*.gz" -exec gzip ; &&
                aws s3 sync /var/log/<service>/ s3://<bucket>/<environment>/<ec2_id>/<service>/ --exclude "*" --include "*<service>*.gz" &&
                find /var/log/<service>/ -iname "*<service>.log.*" -mtime +7 -delete
                endscript

                lastaction
                find /var/log/<service>/ -iname "*<service>.*.log" -delete
                endscript



                Yes, I could just make a cron job. My reasoning for using logrotate is that I keep all of the log rotation config management in a single Ansible role and in the future, I'll be making the case to disable log4j rotation and use logrotate instead which will be a simple change.






                share|improve this answer



























                  1












                  1








                  1







                  I needed to accomplish this behavior because the app teams wanted to maintain control of log and filename format as well as rotation schedule with log4j. If you read man logrotate you'll notice that all the pre/post/lastaction scripts only execute when at least one log was rotated. Thus, setting something unreachable like size 999G does not work.



                  Instead, what I did was set the copy and extension options so that:



                  1. The original log file was never touched (it's managed by log4j)

                  2. The logs rotated by logrotate have a different extension that I can filter on

                  /var/log/<service>/<service>.log 
                  copy # don't touch the orig. log file, it's managed by log4j
                  extension .log # End rotated files in ".log" so we can find and delete them
                  rotate 1
                  hourly
                  missingok

                  prerotate
                  find /var/log/<service>/ -iname "*service.log.*" ! -iname "*.gz" -exec gzip ; &&
                  aws s3 sync /var/log/<service>/ s3://<bucket>/<environment>/<ec2_id>/<service>/ --exclude "*" --include "*<service>*.gz" &&
                  find /var/log/<service>/ -iname "*<service>.log.*" -mtime +7 -delete
                  endscript

                  lastaction
                  find /var/log/<service>/ -iname "*<service>.*.log" -delete
                  endscript



                  Yes, I could just make a cron job. My reasoning for using logrotate is that I keep all of the log rotation config management in a single Ansible role and in the future, I'll be making the case to disable log4j rotation and use logrotate instead which will be a simple change.






                  share|improve this answer















                  I needed to accomplish this behavior because the app teams wanted to maintain control of log and filename format as well as rotation schedule with log4j. If you read man logrotate you'll notice that all the pre/post/lastaction scripts only execute when at least one log was rotated. Thus, setting something unreachable like size 999G does not work.



                  Instead, what I did was set the copy and extension options so that:



                  1. The original log file was never touched (it's managed by log4j)

                  2. The logs rotated by logrotate have a different extension that I can filter on

                  /var/log/<service>/<service>.log 
                  copy # don't touch the orig. log file, it's managed by log4j
                  extension .log # End rotated files in ".log" so we can find and delete them
                  rotate 1
                  hourly
                  missingok

                  prerotate
                  find /var/log/<service>/ -iname "*service.log.*" ! -iname "*.gz" -exec gzip ; &&
                  aws s3 sync /var/log/<service>/ s3://<bucket>/<environment>/<ec2_id>/<service>/ --exclude "*" --include "*<service>*.gz" &&
                  find /var/log/<service>/ -iname "*<service>.log.*" -mtime +7 -delete
                  endscript

                  lastaction
                  find /var/log/<service>/ -iname "*<service>.*.log" -delete
                  endscript



                  Yes, I could just make a cron job. My reasoning for using logrotate is that I keep all of the log rotation config management in a single Ansible role and in the future, I'll be making the case to disable log4j rotation and use logrotate instead which will be a simple change.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago

























                  answered Apr 3 at 18:44









                  Ryan FisherRyan Fisher

                  1387




                  1387





















                      0














                      The Log4j Rolling File Appenders and the logrotate are not compatible. The log4j rotation will interfere with logrotate.



                      First you need to find the log4j configuration file for the application and replace the Rolling File Appenders with simple File Appenders. This will stop the log4j from rotating the files. Then you can use the logrotate to rotate the log files as you wanted.



                      If you can't modify the application's log4j configuration you can create your own one and specify this in the classpath when the application is stared.



                      -Dlog4j.configurationFile=path/to/log4j2.xml


                      Ref: https://logging.apache.org/log4j/2.0/faq.html#config_location






                      share|improve this answer



























                        0














                        The Log4j Rolling File Appenders and the logrotate are not compatible. The log4j rotation will interfere with logrotate.



                        First you need to find the log4j configuration file for the application and replace the Rolling File Appenders with simple File Appenders. This will stop the log4j from rotating the files. Then you can use the logrotate to rotate the log files as you wanted.



                        If you can't modify the application's log4j configuration you can create your own one and specify this in the classpath when the application is stared.



                        -Dlog4j.configurationFile=path/to/log4j2.xml


                        Ref: https://logging.apache.org/log4j/2.0/faq.html#config_location






                        share|improve this answer

























                          0












                          0








                          0







                          The Log4j Rolling File Appenders and the logrotate are not compatible. The log4j rotation will interfere with logrotate.



                          First you need to find the log4j configuration file for the application and replace the Rolling File Appenders with simple File Appenders. This will stop the log4j from rotating the files. Then you can use the logrotate to rotate the log files as you wanted.



                          If you can't modify the application's log4j configuration you can create your own one and specify this in the classpath when the application is stared.



                          -Dlog4j.configurationFile=path/to/log4j2.xml


                          Ref: https://logging.apache.org/log4j/2.0/faq.html#config_location






                          share|improve this answer













                          The Log4j Rolling File Appenders and the logrotate are not compatible. The log4j rotation will interfere with logrotate.



                          First you need to find the log4j configuration file for the application and replace the Rolling File Appenders with simple File Appenders. This will stop the log4j from rotating the files. Then you can use the logrotate to rotate the log files as you wanted.



                          If you can't modify the application's log4j configuration you can create your own one and specify this in the classpath when the application is stared.



                          -Dlog4j.configurationFile=path/to/log4j2.xml


                          Ref: https://logging.apache.org/log4j/2.0/faq.html#config_location







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 11 at 9:56









                          Oz.Oz.

                          1




                          1



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Server Fault!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f859561%2fhow-to-combine-logrotate-with-log4j-rolling-file-appender%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

                              Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

                              What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company