SkillAgentSearch skills...

Debugtrace

A java library to output logs for debugging. It is available in Java 8 or later.

Install / Use

/learn @masatokokubo/Debugtrace
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

DebugTrace-java

Japanese

DebugTrace-java is a library that outputs trace logs when debugging Java programs.<br> By embedding DebugTrace.enter() and DebugTrace.leave() at the start and end of methods, you can output the execution status of the Java program under development to the log.

|DebugTrace-java version|Java version to support |:----------------------|:---------------------- |DebugTrace-java 4.x.x |Java 17 and later |DebugTrace-java 3.x.x |Java 8 and later

1. Features

  • Automatically outputs invoker's class name, method name, source file and line number.
  • Automatically indents the log with nesting methods and objects.
  • Automatically output logs when changing threads.
  • Uses reflection to output the contents of classes that do not implement the toString method.
  • You can customize the output content in DebugTrace.properties.
  • There are no dependent libraries at run time. (Required if you use the following logging library)
  • You can use the following logging library.
    • Console (stdout and stderr)
    • https://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html[JDK Logger]
    • http://logging.apache.org/log4j/1.2/[Log4j]
    • https://logging.apache.org/log4j/2.x/[Log4j2]
    • http://www.slf4j.org/[SLF4J]
    • Direct file output

2. How to use

Insert the following for the debuggee and related methods.

  • DebugTrace.enter();
  • DebugTrace.leave();
  • DebugTrace.print("value", value);

(1) If the method does not throw an exception and does not return in the middle.

public void foo() {
    DebugTrace.enter(); // TODO: Debug
    ...
    DebugTrace.print("value", value); // TODO: Debug
    ...
    DebugTrace.leave(); // TODO: Debug
}

(2) If the method does not throw an exception, but there are returns in the middle.

public void foo() {
    try {DebugTrace.enter(); // TODO: Debug
    ...
    DebugTrace.print("value", value); // TODO: Debug
    ...
    if (...)
        return;
    ...
    } finally {DebugTrace.leave();} // TODO: Debug
}

(3) If the method throws an exception.

public void foo() throws Exception {
    try {DebugTrace.enter(); // TODO: Debug
    ...
    DebugTrace.print("value", value); // TODO: Debug
    ...
    if (...)
        throw new Exception();
    ...
    } catch (Exception e) {DebugTrace.print("e", e); throw e; // TODO: Debug
    } finally {DebugTrace.leave();} // TODO: Debug
}

The following is an example of Java source used DebugTrace-java methods and the log of when it has been executed.

[source,java] .Example1.java

package example;

import java.util.HashMap;
import java.util.Map;

import org.debugtrace.DebugTrace;

public class Example1 {
    private static final Map<Long, Long> fibonacciMap = new HashMap<>();
    static {
        fibonacciMap.put(0L, 0L);
        fibonacciMap.put(1L, 1L);
    }

    public static void main(String[] args) {
        DebugTrace.enter(); // TODO: Debug
        try {
            if (args.length <= 0)
                throw new IllegalArgumentException("args.length = " + args.length);
            long n = Long.parseLong(args[0]);
            long fibonacci = fibonacci(n);
            System.out.println("fibonacci(" + n + ") = " + fibonacci);
        } catch (Exception e) {
            DebugTrace.print("e", e); // TODO: Debug
        }
        DebugTrace.leave(); // TODO: Debug
    }

    public static long fibonacci(long n) {
        DebugTrace.enter(); // TODO: Debug
        if (n < 0)
            throw new IllegalArgumentException("n (" + n + ") is negative.");
        long fibonacci = 0;
        if (fibonacciMap.containsKey(n)) {
            fibonacci = fibonacciMap.get(n);
            DebugTrace.print("mapped fibonacci(" + n + ")", fibonacci); // TODO: Debug
        } else {
            fibonacci = fibonacci(n - 2) + fibonacci(n - 1);
            DebugTrace.print("fibonacci(" + n + ")", fibonacci); // TODO: Debug
            if (fibonacci < 0)
                throw new RuntimeException("Overflow occurred in fibonacci(" + n + ") calculation.");
            fibonacciMap.put(n, fibonacci);
        }
        DebugTrace.leave(); // TODO: Debug
        return fibonacci;
    }
}

.debugtrace.log

2025-07-19 02:34:06.969-07:00 DebugTrace 4.1.2 on Amazon.com Inc. OpenJDK Runtime Environment 17.0.15+6-LTS
2025-07-19 02:34:06.979-07:00   property name: DebugTrace.properties
2025-07-19 02:34:06.987-07:00   logger: org.debugtrace.logger.File (character set: UTF-8, line separator: \n, file: Z:\logs\debugtrace.log)
2025-07-19 02:34:06.989-07:00   time zone: America/Los_Angeles
2025-07-19 02:34:06.996-07:00 
2025-07-19 02:34:07.000-07:00 <i>_____________________________ main _____________________________</i>
2025-07-19 02:34:07.002-07:00 
2025-07-19 02:34:07.004-07:00 Enter example.Example2.main (Example2.java:18) <- (:0)
2025-07-19 02:34:07.007-07:00 | Enter example.Example2.fibonacci (Example2.java:33) <- (Example2.java:23)
2025-07-19 02:34:07.009-07:00 | | Enter example.Example2.fibonacci (Example2.java:33) <- (Example2.java:41)
2025-07-19 02:34:07.038-07:00 | | | mapped fibonacci(1) = (long)1 (Example2.java:39)
2025-07-19 02:34:07.041-07:00 | | Leave example.Example2.fibonacci (Example2.java:48) duration: 00:00:00.029
2025-07-19 02:34:07.043-07:00 | | 
2025-07-19 02:34:07.045-07:00 | | Enter example.Example2.fibonacci (Example2.java:33) <- (Example2.java:41)
2025-07-19 02:34:07.047-07:00 | | | Enter example.Example2.fibonacci (Example2.java:33) <- (Example2.java:41)
2025-07-19 02:34:07.049-07:00 | | | | mapped fibonacci(0) = (long)0 (Example2.java:39)
2025-07-19 02:34:07.051-07:00 | | | Leave example.Example2.fibonacci (Example2.java:48) duration: 00:00:00.001
2025-07-19 02:34:07.061-07:00 | | | 
2025-07-19 02:34:07.066-07:00 | | | Enter example.Example2.fibonacci (Example2.java:33) <- (Example2.java:41)
2025-07-19 02:34:07.068-07:00 | | | | mapped fibonacci(1) = (long)1 (Example2.java:39)
2025-07-19 02:34:07.070-07:00 | | | Leave example.Example2.fibonacci (Example2.java:48) duration: 00:00:00.002
2025-07-19 02:34:07.072-07:00 | | | fibonacci(2) = (long)1 (Example2.java:42)
2025-07-19 02:34:07.074-07:00 | | Leave example.Example2.fibonacci (Example2.java:48) duration: 00:00:00.027
2025-07-19 02:34:07.076-07:00 | | fibonacci(3) = (long)2 (Example2.java:42)
2025-07-19 02:34:07.083-07:00 | Leave example.Example2.fibonacci (Example2.java:48) duration: 00:00:00.074
2025-07-19 02:34:07.088-07:00 | 
2025-07-19 02:34:07.097-07:00 | fibonacciMap = (HashMap)[
2025-07-19 02:34:07.104-07:00 |   (Long)0: (Long)0, (Long)1: (Long)1, (Long)2: (Long)1, (Long)3: (Long)2
2025-07-19 02:34:07.106-07:00 | ] (Example2.java:26)
2025-07-19 02:34:07.113-07:00 | 
2025-07-19 02:34:07.115-07:00 Leave example.Example2.main (Example2.java:29) duration: 00:00:00.108

3. Method List

This library has the following methods. These are all static methods of <a href=http://masatokokubo.github.io/debugtrace/javadoc/org/debugtrace/DebugTrace.html>org.debugtrace.DebugTrace</a> class.

<table> <caption>Method List</caption> <tr> <th>Method Name</th><th>Arguments</th><th>Return Value</th><th>Description</th> </tr> <tr> <td><code>enter</code></td> <td><i>None</i></td> <td><i>None</i></td> <td>Outputs method start to log.</td> </tr> <tr> <td><code>leave</code></td> <td><i>None</i></td> <td><i>None</i></td> <td>Outputs method end to log.</td> </tr> <tr> <td><code>print</code></td> <td><code>message</code>: a message</td> <td>the <code>message</code></td> <td>Outputs the message to log.</td> </tr> <tr> <td><code>print</code></td> <td><code>messageSupplier</code>: a supplier of message</td> <td> tht message getted from the messageSupplier</td> <td>Gets a message from the supplier and output it to log.</td> </tr> <tr> <td><code>print</code></td> <td> <code>name</code>: the value name<br> <code>value</code>: the value </td> <td>the <code>value</code></td> <td> Outputs to the log in the form of<br> <code>"Name = Value"</code><br> <code>value</code> type is one of the following.<br> <code>boolean</code>, <code>char</code></code>,<br> <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>,<br> <code>float</code>, <code>double</code>, <code>T</code> </td> </tr> <tr> <td><code>print</code></td> <td> <code>name</code>: the value name<br> <code>value</code>: the value<br> <code>logOptions</code>: <a href="http://masatokokubo.github.io/debugtrace/javadoc/org/debugtrace/LogOptions.html">LogOptions</a><br> The following fields can be specified in <code>logOptions</code>.<br> <code>minimumOutputSize</code>,<br> <code>minimumOutputLength</code>,<br> <code>collectionLimit</code>,<br> <code>byteArrayLimit</code>,<br> <code>stringLimit</code>,<br> <code>reflectionNestLimit</code><br> Or the following can be specified.<br> <code>LogOptions.outputSize</code><br> <code>LogOptions.outputLength</code> </td> <td>the <code>value</code></td> <td>Same as above.</td> </tr> <tr> <td><code>print</code></td> <td> <code>name</code>: the value name<br> <code>valueSupplier</code>: the supplier of the value </td> <td> the value getted from the <code>valueSupplier</code></td> <td> Gets a value from the <code>valueSupplier</code> and outputs to the log in the form of<br> <code><value name> = <value></code><br> <code>valueSupplier</code> type is one of the following.<br> <code>BooleanSupplier</code>,<br> <code>IntSupplier`, `LongSupplier</code><br> <code>Supplier&lt;T&gt;</code> </td> </tr> <tr> <td><code>print</code></td> <td> <code>name</code>: the value name<br> <code>valueSupplier</code>: the supplier of the value<br>
View on GitHub
GitHub Stars11
CategoryDevelopment
Updated8mo ago
Forks0

Languages

Java

Security Score

82/100

Audited on Jul 21, 2025

No findings