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

Multi tool use
Multi tool use

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







                              YyEvxf6277U Yf1o we,K9wprtNX
                              PmgozRFvUAAY90,wxw7fUGVT7nku oa65Ynjqx7utyOP35wpPmEb2DevqzffT,Q8,NCLltZrNs2qZebgXNpaCBageqRm oF,3y1 7Iqfr

                              Popular posts from this blog

                              Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

                              Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

                              Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020