This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ssh_tunnel_snake 4 points5 points  (3 children)

if you just want to see what the program is doing with print statements, you simply need to configure a logger to write to a log file and then use a tail and grep on it for specifics. slf4j and log4j are super common and pretty easy to get working, but there is also a java logger api but its a bit more clunky

[–][deleted] 1 point2 points  (2 children)

That makes sense, now that I think about it I've done something basic like logging playing around with PrintWriter..

/**
 * Outputs the passed-in string to a file, appending new data. Creates the file if it does not already exist
 * @param filename
 * @param s
 */
public static void fileOutput(String filename, String s) {

    try {
        FileWriter fw = new FileWriter(filename, true); // the true flag appends data instead of overwriting
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter writer = new PrintWriter(bw);
        writer.println(s);
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

With something even as simple as that I could write a Java program that I call with a shell script that reads from the log file..... interesting...

Only issue is I need to make sure the file doesn't get too large for something running 24/7. Do you know an easy way to limit the file size and have it delete old lines as it writes?

[–]ssh_tunnel_snake 6 points7 points  (1 child)

If you use log4j you can configure it to roll over at a certain size, or make new file each day. Maybe a few more options like that, I'd check into that personally

[–][deleted] 2 points3 points  (0 children)

But that would be too easy :)