Skip to content

Instantly share code, notes, and snippets.

@renaudfv
Created February 22, 2023 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renaudfv/8683660061e06fbe14848b67b1c8e379 to your computer and use it in GitHub Desktop.
Save renaudfv/8683660061e06fbe14848b67b1c8e379 to your computer and use it in GitHub Desktop.
Processing logger using java util logging lib
import java.util.logging.*;
import java.util.Date;
// Main logger
Logger LOGGER = Logger.getLogger("");
MyCustomClass customInstance;
void setupLogging() {
try {
// Logg to file, max size 1048576bits, rotate every 5 files, append to existing file
Handler handler = new FileHandler(sketchPath("output_%g.log"), 1048576, 5, true);
// Format time input
handler.setFormatter(new SimpleFormatter() {
// [DD-MM-YYYY HH:MM:SS GMT-TIMEZONE] <LEVEL> Logline
private static final String format = "[%1$td-%1$tm-%1$tY %1$tT GMT%1tz] <%2$-4s> %3$s %n";
@Override
public synchronized String format(LogRecord lr) {
return String.format(format,
new Date(lr.getMillis()),
lr.getLevel().getLocalizedName(),
lr.getMessage()
);
}
}
);
LOGGER.addHandler(handler);
}
catch (Exception e) {
LOGGER.warning("Error setting up logging " + e);
}
}
void setup() {
setupLogging();
customInstance = new MyCustomClass();
// Use the main logger to do program level logging
LOGGER.log(Level.INFO, "Setup complete");
}
void draw() {
background(0);
customInstance.draw();
if(frameCount % 100 == 0) {
LOGGER.info("Log every 100 frames");
}
}
class MyCustomClass {
int x;
int y;
// Class level logger
Logger logger = Logger.getLogger(MyCustomClass.class.getName());
MyCustomClass() {
this.x = 0;
this.y = 0;
this.logger.info("MyCustomClass constructor has been called");
}
void draw() {
this.update();
square(this.x, this.y, 10);
if(frameCount % 200 == 0) {
this.logger.warning("Useless class level log warning every 200 frame");
}
}
void update() {
this.x = (int) random(0, 100);
this.y = (int) random(0, 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment