My URCap uses the standard library logging API (java.util.logging
) to log debug messages and other such things. I can see log messages by opening the PolyScope terminal with Ctrl
+ Alt
+ F9
. However, some of my log messages are not showing because messages from PolyScope and other URCaps has shoved them off the screen (and there is no way to scroll in this terminal).
I want logging that writes to a file so I can filter and view the messages I am looking for. The only previous threads I could find on this are URCaps Logging file and URCap log details. The former does not apply to my circumstance and the latter is lacking a lot of details.
It looks like PolyScope is already configured to log to files. I can see all the messages from PolyScope in ~/polyscope.log
. However, my URCap’s messages are all missing from this log file. I think that I need to drop java.util.logging
and switch to Log4j, since that’s what PolyScope now uses, but I don’t know how to obtain a logger instance from the APIs exposed to my URCap.
Can someone please provide an up-to-date explanation on logging with example code?
I mean this is probably more simplistic than you’re looking for, but I’ve just used a basic FileWriter to write a text file onto the controller. Then I can just SSH into that, or run a CAT command on that from the teach pendent to read stuff.
FileWriter myWriter = new FileWriter("/root/myLogFile.txt", true)";
myWriter.write("Here's a line to write to the file.");
myWriter.close();
I then just made a static method in a class that got instantiated in the activator that takes a string as input and just substituted that string into the .write() method.
This lead me in the right direction and I came up with the example below:
public class Activator implements BundleActivator {
private Logger logger;
@Override
public void start(BundleContext bundleContext) throws Exception {
String name = bundleContext.getBundle().getSymbolicName();
this.logger = Logger.getLogger(name);
// The parent directory may be deleted if the robot is fully
// shutdown, so use a different file location if the log needs
// to persist between restarts.
FileHandler handler = new FileHandler(bundleContext.getDataFile("latest.log").getAbsolutePath());
SimpleFormatter formatter = new SimpleFormatter();
handler.setFormatter(formatter);
this.logger.addHandler(handler);
// ...
}
// ...
}