Editing
Loggifier.unkrig.de / Getting Started
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
I presume that you know what <code>java.util.logging</code> is and why you want to use logging at all (e.g. in favor of an interactive debugger). So let's start with a very simple program: '''import''' java.util.logging.Logger; '''public class''' Main { '''private static final''' Logger LOGGER = Logger.getLogger("Main.USER"); '''public static void''' main(String[] args) { System.out.println("HELLO WORLD"); LOGGER.fine("About to terminate"); } } By default, <code>java.util.logging</code> is disabled, so we don't get any logging output: '''$ javac Main.java''' '''$ java Main''' HELLO WORLD '''$''' To enable logging, we can either edit <code>$JAVA_HOME/jre/lib/logging.properties</code>, or create a file # File 'my_logging.properties'. .level = FINEST handlers = java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level = FINEST java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter.format = [%2$20s] %4$s: %5$s%n , and run our program with the <code>java.util.logging.config.file</code> system property set: '''$ java -Djava.util.logging.config.file=my_logging.properties Main''' HELLO WORLD <span style="color:red">[ Main main] FINE: About to terminate</span> '''$''' Now let the magic begin! There are different [[#Tools|tools]] to loggify code; here we would use the [[#Java Agent|Java agent]]. Get <code>de.unkrig.loggifier.jar</code> and and run: '''$ java -javaagent:de.unkrig.loggifier.jar -Djava.util.logging.config.file=my_logging.properties Main''' <span style="color:red">[ Main <clinit>()] FINER: (Main.java:6) ENTRY args=()</span> <span style="color:red">[ Main <clinit>()] FINER: (Main.java:6) RETURN</span> <span style="color:red">[ Main main(String[])] FINER: (Main.java:9) ENTRY args=(args=String[0])</span> HELLO WORLD <span style="color:red">[ Main main] FINE: About to terminate</span> <span style="color:red">[ Main main(String[])] FINER: (Main.java:12) RETURN</span> '''$''' By default, LOGGIFIER injects a 'reasonable' amount of logging: You can see how the class initializer of the <code>Main</code> class is executed, then how the <code>main()</code> method is entered and how it returns. Notice how the hand-written logging ("About to terminate") seamlessly mixes with the automatically injected logging. Effectively, we can now remove the hand-written logging code from our class, because the automatically injected logging code now does the job: '''public class''' Main { '''public static void''' main(String[] args) { System.out.println("HELLO WORLD"); } } Now let's run it again: '''$ java -javaagent:de.unkrig.loggifier.jar -Djava.util.logging.config.file=my_logging.properties Main''' <span style="color:red">[ Main <clinit>()] FINER: (Main.java:6) ENTRY args=()</span> <span style="color:red">[ Main <clinit>()] FINER: (Main.java:6) RETURN</span> <span style="color:red">[ Main main(String[])] FINER: (Main.java:9) ENTRY args=(args=String[0])</span> HELLO WORLD <span style="color:red">[ Main main(String[])] FINER: (Main.java:12) RETURN</span> '''$''' For demonstration, we now set the amount of logging to the highest possible level. Loggification is controlled by 'loggification rules', and there are several ways to declare them: We can pass them on the command line like '''$ java -javaagent:de.unkrig.loggifier.jar<span style="color:blue">=ALL=FINE</span> -Djava.util.logging.config.file=my_logging.properties Main''' or add a <code>@Loggify</code> annotation to any class, constructor or method declaration: '''import''' de.unkrig.loggifier.runtime.annotation.Loggify; @Loggify("ALL=FINE") '''public class''' Main { // ... Then compile and run again: '''$ javac Main.java -cp de.unkrig.loggifier-runtime.jar''' '''$ java -javaagent:de.unkrig.loggifier.jar -Djava.util.logging.config.file=my_logging.properties Main''' <span style="color:red">[ Main <clinit>()] FINE: (Main.java:1) CLINIT Main</span> <span style="color:red">[ Main main(String[])] FINE: (Main.java:12) ENTRY args=(args=String[0])</span> <span style="color:red">[ Main main(String[])] FINE: (Main.java:12) GET System.out => java.io.PrintStream@7020b3a3</span> <span style="color:red">[ Main main(String[])] FINE: (Main.java:12) CONST "HELLO WORLD"</span> <span style="color:red">[ Main main(String[])] FINE: (Main.java:12) INVOKE PrintStream.println(String): target=java.io.PrintStream@7020b3a3, args=("HELLO WORLD")</span> HELLO WORLD <span style="color:red">[ Main main(String[])] FINE: (Main.java:12) RESULT PrintStream.println(String)</span> <span style="color:red">[ Main main(String[])] FINE: (Main.java:15) RETURN</span> '''$''' (Notice that JAVAC now needs "de.unkrig.loggifier-runtime.jar" on the class path, because "@Loggify" lives there.) Now you can watch how the <code>System.out</code> field is read and the string constant <code>"HELLO WORLD"</code> is used - if you're interested.
Summary:
Please note that all contributions to unkrig.de may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Unkrig.de:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help
Tools
What links here
Related changes
Special pages
Page information