Skip to content

Extending the logger

In addition to the configurable logger options, DogLog also allows you to extend the logger class to add your own custom behavior. This is a great way to simplify common logging flows your team has, and reduce duplicated code in your robot projects.

This guide will walk you through the steps to create a custom logger class that allows you to add your own custom behavior. As an example, we will add in a new log() method to help log motor values.

  1. Add DogLog to your project if you haven't already:

  2. Create a new file for your extended logger class:

    • Directorysrc
      • Directorymain
        • Directorydeploy/
        • Directoryjava/
          • Directoryfrc/
            • Directoryrobot/
              • CustomLogger.java Create this file
              • Main.java
              • Robot.java
              • RobotContainer.java
  3. In the newly created file, paste the following code:

    src/main/java/frc/robot/CustomLogger.java
    package frc.robot;
    import com.ctre.phoenix6.hardware.TalonFX;
    import dev.doglog.DogLog;
    public class CustomLogger extends DogLog {
    public static void log(String key, TalonFX motor) {
    log(key + "/StatorCurrent", motor.getStatorCurrent().getValue());
    log(key + "/Position", motor.getPosition().getValue());
    log(key + "/Velocity", motor.getVelocity().getValue());
    }
    }
  4. Replace places where you use DogLog with CustomLogger.

    package frc.robot;
    import com.ctre.phoenix6.hardware.TalonFX;
    import dev.doglog.DogLog;
    public class ExampleClass {
    private final TalonFX motor = new TalonFX(1);
    public void logData() {
    DogLog.log("ExampleMotor/StatorCurrent", motor.getStatorCurrent().getValue());
    DogLog.log("ExampleMotor/Position", motor.getPosition().getValue());
    DogLog.log("ExampleMotor/Velocity", motor.getVelocity().getValue());
    CustomLogger.log("ExampleMotor", motor);
    }
    }

If you want to track how long a command takes to run, you can extend the logger with a helper that wraps a command with a timer.

  1. Create a CustomLogger class if you don't already have one, following the steps above.

  2. Create a new file for the command wrapper:

    • Directorysrc
      • Directorymain
        • Directorydeploy/
        • Directoryjava/
          • Directoryfrc/
            • Directoryrobot/
              • CustomLogger.java
              • TimedCommand.java Create this file
              • Main.java
              • Robot.java
              • RobotContainer.java
  3. Paste in the wrapper for your command framework version, then add a matching time() method to your CustomLogger:

    This wraps a Commands v3 command and logs its total execution time in seconds.

    src/main/java/frc/robot/TimedCommand.java
    package frc.robot;
    import dev.doglog.DogLog;
    import java.util.Set;
    import org.wpilib.command3.Command;
    import org.wpilib.command3.Coroutine;
    import org.wpilib.command3.Mechanism;
    import org.wpilib.hardware.hal.HALUtil;
    public class TimedCommand implements Command {
    private final Command command;
    private final String key;
    public TimedCommand(Command command, String key) {
    this.command = command;
    this.key = key;
    }
    @Override
    public String name() {
    return "Timed" + command.name();
    }
    @Override
    public int priority() {
    return command.priority();
    }
    @Override
    public Set<Mechanism> requirements() {
    return command.requirements();
    }
    @Override
    public void run(Coroutine coroutine) {
    var startTime = HALUtil.getMonotonicTime();
    coroutine.await(command);
    var elapsed = (HALUtil.getMonotonicTime() - startTime) / 1_000_000_000.0;
    DogLog.log(key, elapsed);
    }
    }

    Then add a time() method to your CustomLogger so you can wrap commands with it. The highlighted lines are what you need to add:

    src/main/java/frc/robot/CustomLogger.java
    package frc.robot;
    import dev.doglog.DogLog;
    import org.wpilib.command3.Command;
    public class CustomLogger extends DogLog {
    public static Command time(String key, Command command) {
    return new TimedCommand(command, key);
    }
    }
  4. Wrap your commands with the new time() method:

    src/main/java/frc/robot/RobotContainer.java
    public Command getAutonomousCommand() {
    return CustomLogger.time(
    "Autos/AutoCommand", Commands.waitTime(Units.Seconds.of(0.75)).withName("MyAutoCommand"));
    }