Logging

Although we recommend using a dedicated logging framework — even the JDK logging. However, sometimes that’s overkill and you just want to output a handful of messages but want a bit more control than what System.out or System.err provide.

Strongback includes a very simple and lightweight application logging framework that you can use in your robot code. There are not many features, but it does allow you to easily generate error, warning, informational, debug, and trace messages. It also lets you easily control which level of log messages you want to capture, and which you want to discard. All captured messages are written to System.out. Like we said, it’s a very simple framework.

Using the logger

You can use the Strongback logger in any of your classes:

Strongback.logger().warn("Something may have gone wrong!");

or

try {
    ...
} catch ( Throwable t ) {
    Strongback.logger().error(t,"Whoa, what happened?!");
}

You can also pass a context to the logger(…​) method, too. Strongback’s default logger doesn’t use this, but if you hook up Strongback to use a different logging framework you might find it useful. Here’s an example that obtains the logger for the MyCommand.class logging context:

Strongback.logger(MyCommand.class).debug("MyCommand executed once and completed");

Control the log level

Like many other logging frameworks, Strongback supports multiple levels of messages: ERROR, WARN, INFO, DEBUG, and TRACE. These are ordered, so enabling INFO level messages will include ERROR and WARN messages as well, but will discard DEBUG and TRACE.

By default Strongback captures INFO level and above, but you can change that when you configure Strongback in robotInit():

public class SimpleTankDriveRobot extends IterativeRobot {
    ....
    @Override
    public void robotInit() {
        Strongback.configure()
                  .useSystemLogger(Logger.Level.DEBUG)
                  .initialize();
        ...
    }
    ...
}

Using a custom Logger

If you want to send your log messages somewhere other than System.out, you can always create your own implementation of the org.strongback.Logger interface, and register it when initializing Strongback:

public class SimpleTankDriveRobot extends IterativeRobot {
    ....
    @Override
    public void robotInit() {
        Logger myLogger = ...
        Strongback.configure()
                  .useCustomLogger(context->myLogger)
                  .initialize();
        ...
    }
    ...
}

Remember that you now have control over which log messages are captured, since all messages will be sent to your Logger implementation.

If you look carefully, the useCustomLogger method takes a function that returns a Logger given a context parameter. The above lambda always uses myLogger, but you can make it as complicated as you want.

results matching ""

    No results matching ""