Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a hyperlink to a project file in console output

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. enter image description here

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
like image 661
mmaynar1 Avatar asked Jun 19 '15 00:06

mmaynar1


People also ask

How do you create a hyperlink to a file?

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.

How do you add a link to console log?

console. log('example.com'); console. log('<a href="http://www.example.com">Link</a>');


2 Answers

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.")
like image 156
Javaru Avatar answered Oct 02 '22 21:10

Javaru


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() + ")");
like image 27
Ivanov O. Avatar answered Oct 02 '22 20:10

Ivanov O.