Is there a way for me to write out a link to the console output that when clicked on directs to a project file in Intellij?
For example, this happens when a run-time exception occurs. I see the stack trace and I can click on a link in the console that directs me to where the problem was. Here I can click on DatabaseConfiguration.java and I will be redirected to that file in Intellij.
What I want to do is output a link to a readme.txt file that is written to the console when main starts up. When clicked on it opens up readme.txt in Intellij.
I am also using log4j and directing output to the console, which may affect the solution. Here is my conversion pattern:
log4j.appender.FILE.layout.conversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] %m%n
Create a hyperlink to a file on your computerSelect the text or picture that you want to display as a hyperlink. Press Ctrl+K. Under Link to, do one of the following: To link to an existing file, click Existing File or Web Page under Link to, and then find the file in the Look in list or the Current Folder list.
console. log('example.com'); console. log('<a href="http://www.example.com">Link</a>');
IDEA will create a link in the console for any text that matches following pattern:
(${FileName}.${FileExtention}:${lineNum})
or as a Regex:
\([\w \.\-]+\.[\w]*:[\d]+\)
For example: (ErrorNotes.txt:10)
Note that you need to include the parenthesis. For an actual class, you could use the following in log4j pattern: (%F:%L) For example: To reference another class, or file, you would have to output the actual file name, extension, and line number, inside parenthesis, yourself since log4j can only access that info for the current class. For example:
logger.error("A FluxCapacitorExcpetion #88 has occurred. See (Error Details.txt:542) for more details.")
I found this solution for me:
System.out.println("Printing stack trace:");
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
for (int i = 1; i < elements.length; i++) {
StackTraceElement s = elements[i];
System.out.println("\tat " + s.getClassName() + "." + s.getMethodName()
+ "(" + s.getFileName() + ":" + s.getLineNumber() + ")");
}
And the main result is here (only link to row of code - without a full stack trace):
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
StackTraceElement s = elements[elements.length-1];
System.out.println("\tat " + s.getClassName() + "." + s.getMethodName()
+ "(" + s.getFileName() + ":" + s.getLineNumber() + ")");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With