https://github.com/tsundberg/debug-switch
How to enable or disable debug logging in a Java program
https://github.com/tsundberg/debug-switch
Last synced: 4 months ago
JSON representation
How to enable or disable debug logging in a Java program
- Host: GitHub
- URL: https://github.com/tsundberg/debug-switch
- Owner: tsundberg
- Created: 2017-10-15T16:48:12.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-15T17:35:08.000Z (over 7 years ago)
- Last Synced: 2025-01-05T13:42:24.370Z (5 months ago)
- Language: Java
- Size: 52.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Run a Java program with and without debug information
If you log to a console using System.out, how can you enable and disable the logging?
An approach is to create a log method that checks a system property and set the property at startup to enable logging and not set it when no logging is wanted.
The log method may look like this:
private void log(String text) {
String debug = System.getProperty("debug");
if (debug != null) {
System.out.println(text);
}
}Calling this method may be done like this:
public void checkBalance() {
// do stuff
log("My debug text");
// more stuff
}Want to try it out and fiddle with the code? Clone this project and build as described below.
If you run this program and set the property `debug` like this `java -Ddebug` then the debug information will be printed. If you don't set it, no debug info will be printed.
## Build:
./gradlew stage
## Run:
java -jar ./build/libs/debug-switch-all.jar
## Run with debug logging
java -Ddebug -jar ./build/libs/debug-switch-all.jar