-1

New attempt, same question:

I am just looking for a way to keep java.util.logging-logs to NOT loop at the end of the amount of file-sizes and file-number assigned to them!

Additional Information: In my case, this happens because the software, due to unexpected circumstances triggered by the customer, drops into an infinite loop on a section of code that writes to the log. This leads to the infinite loop overwriting the part of the log that shows how the software got into this state in the first place.
Yes, I am aware that the loop is the underlying issue, I would still like to keep the log from looping, and instead to stop logging.

Layna
  • 457
  • 5
  • 19
  • Wouldn't it make more sense to fix the infinite loop instead? And how small is that log file when it immediately starts to overwrite? – Tom Jul 17 '17 at 09:06
  • We want to, we want to fix so very badly! And the size really depends... but with a infinite loop running somewhere deep in the bones of the software, happily looping on and on, the end of the file is eventually reached... often before anyone fully realises something went wrong or even considers shutting down the software! – Layna Jul 17 '17 at 09:20
  • Possible duplicate of [How to set maximum number of rolls and maximum log size for tomcat?](https://stackoverflow.com/questions/8342336/how-to-set-maximum-number-of-rolls-and-maximum-log-size-for-tomcat) – Tom Jul 17 '17 at 09:25
  • And it overwrites the log file with unhelpful information? So you might want to reconsider your logging strategies in the future (you should be able to know why something happened, even when a part of the log is cut of) About the dupe: it explains how to set up file rotation, which you should use here. And use proper sizes, so nothing gets overwritten anymore. Btw: you should always use file rotation, not only here with that issue. Also think about using libs like log4j or logback where you also can zip the old logs. – Tom Jul 17 '17 at 09:28
  • 1
    The loop tells us what part is looping... but sadly hides how the software was pushed to that point. – Layna Jul 17 '17 at 09:38
  • Possible duplicate of [How to regulate the amount of printouts generated by a logging instruction produces over time?](https://stackoverflow.com/questions/986548/how-to-regulate-the-amount-of-printouts-generated-by-a-logging-instruction-produ) – jmehrens Jul 17 '17 at 13:16
  • Ok, I have tried to make this more clear. Really, all I was trying to ask is "How do I keep the log from looping", and I added the extra-information to avoid any suspicions of an XY-Problem. I am basically asking how to influence the logging-behavior. If this is off-Topic, someone please tell me why. – Layna Jul 17 '17 at 13:59

1 Answers1

1

Create and install a custom java.util.logging.Filter to detect the infinite loop and have it either stop the messages, stop the loop, or stop the system. That will allow you to track down the root cause and fix the problem.

Here is some example code to get you started:

import java.util.logging.Filter;
import java.util.logging.LogRecord;

public class LoopFilter implements Filter {

    private long counter;
    private final long LIMIT = Short.MAX_VALUE; //@todo Change this.

    @Override
    public synchronized boolean isLoggable(LogRecord record) {
        if (isLoopRecord(record)) {
            if (counter++ >= LIMIT) {
                return false;
                //throw new Error("Loop abort"); //Harsh.
                //System.exit(0);  //Really harsh.
                //Runtime.getRuntime().halt(0); //Really, really harsh.
            }
        }
        return true;
    }

    private boolean isLoopRecord(LogRecord record) {
        return "Some loop message".equals(record.getMessage());
    }
} 

Then install this filter on the logger that is generating the message or messages you see in the log file.

Another option would be to raise logger level for the just offending logger. That is assuming it doesn't silence the the logger that contains the details on why this loop is occurring.

jmehrens
  • 10,580
  • 1
  • 38
  • 47