Coder Social home page Coder Social logo

debug_logger's Introduction

debug_logger

debug_logger is a powerful debug helper based on the debug informations in the standard output. It can easily manage the output of the debug information, which will help us a lot when debugging.CHINESE DOCUMENTATION HERE

How to start

Initialize

Firstly, we should initialize the config of the DebugHelper at the beginning of the entry point, like this:

import com.hansbug.debug.DebugHelper;

public abstract class Main {
    public static void main(String[] args) {
        DebugHelper.setSettingsFromArguments(args);
    }
}

After this, we can use the DebugHelper.

Let's debug

The usage of DebugHelper in the program is very easy. You can add this into your program anywhere you like to output the debug information.

DebugHelper.debugPrintln(2, "My debug information");

When you use the --debug 2(or more than 2) in the command line, this will create a line of output(in the standard output).

[DEBUG-2][xxxx.java:233 TestClass.testMethod] My debug information

xxxx.java:233 TestClass.testMethod represents the position where you put the debugPrintln, in this case it means the 233rd line of the xxxx.java, in the method testMethod of the class TestClass.

When you use the --debug 1, there will be nothing output because we the debug level of this piece of debug information is 2, while what we need is only 1.

Command line configuration

  • -D <level>, --debug <level> define the debug level. The maximum of the debug level is 5 and the minimum is 1.
  • --debug_package_name <package_name> define the limit of the package name(full name, just like com.hansbug.debug)
  • --debug_file_name <file_name> define the limit of the file name(short file name, just like Main.java, DebugHelper.java)
  • --debug_class_name <class_name> define the limit of the class name(short class name, including inner classes, like TestClassA, TestInnerClassA(instead of TestClassA.TestInnerClassA))
  • --debug_method_name <method_name> define the limit of the method name(just like toString, <init>(constructor method))
  • --debug_include_children define whether record the log in subroutines that called by the method in the limit above.
  • --debug_show_thread define whether show the name of threads which execute the debugPrintln.

ATTENTION

  • If --debug(or -D) not detected, debug mode is DISABLED and file logger will not be processed.
  • If debug level is invalid, there will be exception thrown out.
  • All the limit above is regular-expression-supported!
  • Remember to initialize the configurations at the beginning!!!

Example

You can try to run the Main.main in the test directory to see and try the example.

The command line arguments should be added before running the demo.

In the Main.main in the test directory, when we run it without any arguments, its output should like this:

x: 1, y: -1
x: 3, y: 6
[thread_2] 0
[thread_1] 0
[thread_2] 1
[thread_1] 1
[thread_2] 2
[thread_1] 2
[thread_2] 3
[thread_2] 4
[thread_1] 3
[thread_1] 4
[thread_1] 5
[thread_1] 6
[thread_1] 7
[thread_1] 8

if the arguments --debug 1 is added, its standard output will be:

[DEBUG-1][Main.java:62 Main.testDebugHelper] ksdhjf
x: 1, y: -1
x: 3, y: 6
[DEBUG-1][TestThread.java:18 TestThread.run] [Thread - "thread_1"] start
[DEBUG-1][TestThread.java:18 TestThread.run] [Thread - "thread_2"] start
[thread_2] 0
[thread_1] 0
[thread_2] 1
[thread_1] 1
[thread_2] 2
[thread_1] 2
[thread_2] 3
[thread_2] 4
[thread_1] 3
[DEBUG-1][TestThread.java:31 TestThread.run] [Thread - "thread_2"] end
[thread_1] 4
[thread_1] 5
[thread_1] 6
[thread_1] 7
[thread_1] 8
[DEBUG-1][TestThread.java:31 TestThread.run] [Thread - "thread_1"] end

if the arguments --debug 5 is added, its standard output will be:

[DEBUG-1][Main.java:62 Main.testDebugHelper] ksdhjf
[DEBUG-2][TestClassA.java:12 TestInnerClassA.<init>] TestInnerClassA initialize, x: 0, y: 0
[DEBUG-2][TestClassA.java:20 TestClassA.<init>] TestClassA initialize, x: 0, y: 0
[DEBUG-2][TestClassA.java:31 TestClassA.toString] TestClassA getString, x: 1, y: -1
x: 1, y: -1
[DEBUG-2][TestClassA.java:12 TestInnerClassA.<init>] TestInnerClassA initialize, x: 2, y: 7
[DEBUG-2][TestClassA.java:20 TestClassA.<init>] TestClassA initialize, x: 2, y: 7
[DEBUG-2][TestClassA.java:31 TestClassA.toString] TestClassA getString, x: 3, y: 6
x: 3, y: 6
[DEBUG-1][TestThread.java:18 TestThread.run] [Thread - "thread_1"] start
[DEBUG-1][TestThread.java:18 TestThread.run] [Thread - "thread_2"] start
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 0 and wait for 503 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 0 and wait for 367 ms
[thread_2] 0
[thread_1] 0
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 367 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 1 and wait for 367 ms
[thread_2] 1
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 1 and wait for 503 ms
[thread_1] 1
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 368 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 2 and wait for 367 ms
[thread_2] 2
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 503 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 2 and wait for 503 ms
[thread_1] 2
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 368 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 3 and wait for 367 ms
[thread_2] 3
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 368 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 4 and wait for 367 ms
[thread_2] 4
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 503 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 3 and wait for 503 ms
[thread_1] 3
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 368 ms
[DEBUG-1][TestThread.java:31 TestThread.run] [Thread - "thread_2"] end
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 4 and wait for 503 ms
[thread_1] 4
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 503 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 5 and wait for 503 ms
[thread_1] 5
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 6 and wait for 503 ms
[thread_1] 6
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 7 and wait for 503 ms
[thread_1] 7
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 503 ms
[DEBUG-2][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 8 and wait for 503 ms
[thread_1] 8
[DEBUG-3][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-1][TestThread.java:31 TestThread.run] [Thread - "thread_1"] end

if the arguments --debug 5 --debug_package_name "test_package_A" is added, it will be:

[DEBUG-2][TestClassA.java:12 TestInnerClassA.<init>] TestInnerClassA initialize, x: 0, y: 0
[DEBUG-2][TestClassA.java:20 TestClassA.<init>] TestClassA initialize, x: 0, y: 0
[DEBUG-2][TestClassA.java:31 TestClassA.toString] TestClassA getString, x: 1, y: -1
x: 1, y: -1
[DEBUG-2][TestClassA.java:12 TestInnerClassA.<init>] TestInnerClassA initialize, x: 2, y: 7
[DEBUG-2][TestClassA.java:20 TestClassA.<init>] TestClassA initialize, x: 2, y: 7
[DEBUG-2][TestClassA.java:31 TestClassA.toString] TestClassA getString, x: 3, y: 6
x: 3, y: 6
[thread_2] 0
[thread_1] 0
[thread_2] 1
[thread_1] 1
[thread_2] 2
[thread_1] 2
[thread_2] 3
[thread_2] 4
[thread_1] 3
[thread_1] 4
[thread_1] 5
[thread_1] 6
[thread_1] 7
[thread_1] 8

if the arguments --debug 5 --debug_package_name "test_package_B" --debug_show_thread is added, it will be:

x: 1, y: -1
x: 3, y: 6
[DEBUG-1][Thread-1][TestThread.java:18 TestThread.run] [Thread - "thread_2"] start
[DEBUG-1][Thread-0][TestThread.java:18 TestThread.run] [Thread - "thread_1"] start
[DEBUG-2][Thread-1][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 0 and wait for 367 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 0 and wait for 503 ms
[thread_2] 0
[thread_1] 0
[DEBUG-3][Thread-1][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 368 ms
[DEBUG-2][Thread-1][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 1 and wait for 367 ms
[thread_2] 1
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 503 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 1 and wait for 503 ms
[thread_1] 1
[DEBUG-3][Thread-1][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 368 ms
[DEBUG-2][Thread-1][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 2 and wait for 367 ms
[thread_2] 2
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 2 and wait for 503 ms
[thread_1] 2
[DEBUG-3][Thread-1][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 367 ms
[DEBUG-2][Thread-1][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 3 and wait for 367 ms
[thread_2] 3
[DEBUG-3][Thread-1][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 367 ms
[DEBUG-2][Thread-1][TestThread.java:21 TestThread.run] [Thread - "thread_2"] execute - 4 and wait for 367 ms
[thread_2] 4
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 3 and wait for 503 ms
[thread_1] 3
[DEBUG-3][Thread-1][TestThread.java:26 TestThread.run] [Thread - "thread_2"] came back after 368 ms
[DEBUG-1][Thread-1][TestThread.java:31 TestThread.run] [Thread - "thread_2"] end
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 4 and wait for 503 ms
[thread_1] 4
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 503 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 5 and wait for 503 ms
[thread_1] 5
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 504 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 6 and wait for 503 ms
[thread_1] 6
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 503 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 7 and wait for 503 ms
[thread_1] 7
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 503 ms
[DEBUG-2][Thread-0][TestThread.java:21 TestThread.run] [Thread - "thread_1"] execute - 8 and wait for 503 ms
[thread_1] 8
[DEBUG-3][Thread-0][TestThread.java:26 TestThread.run] [Thread - "thread_1"] came back after 505 ms
[DEBUG-1][Thread-0][TestThread.java:31 TestThread.run] [Thread - "thread_1"] end

Javadoc

(not yet created :-( )

debug_logger's People

Contributors

buaa0110 avatar hansbug avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.