Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement the "System.out.println(ClassName::MethodName <then my message>)" of Eclipse in Netbeans?

Tags:

java

netbeans

I would like to know if there is the same feature as in eclipse to automatically generate and print the System.out.println(ClassName::MethodName <then my message>) functionality (which will print the class name and method name for debugging in the console) in Netbeans also.

For example, in Eclipse Editor, Typing

syst + Ctrl+ Space

will auto generate a System.out.println(ClassName::MethodName ) type output in the console.

Is such a method available in Netbeans?

As of now, I have only two methods here in Netbeans:

sout + Tab

(System.out.println()) and

soutv + Tab

(System.out.println(prints the variable used just above the line)) automatically.

Let me rephrase, instead of myMethod1, I want to get the enclosing method name.

Eg. :

public class X {

  public void myMethod1(int a) {
    System.out.println(X::myMethod1()); // This should be produced when I type the Code-Template abbreviation (example: syst) and press tab (or corresponding key).
  }
}

public class Y {

  public void myMethod2(int b) {
    System.out.println(Y::myMethod2()); // This should be produced when I type the Code-Template abbreviation (example: syst) and press tab (or corresponding key).
  } 
}  

Update:

With the following code template:

syst = System.out.println("${classVar editable="false" currClassName default="getClass()"}");

I am able to print the classname, but still no clue for the Method name.

like image 696
Navaneeth Sen Avatar asked Oct 17 '12 10:10

Navaneeth Sen


3 Answers

You can use Logger to implement this.

Apache log4j is a Java-based logging utility.

Using log4j or other logger implementation would give you class and method information and every commercial application use logger than using System.out.

The logger also gives the ability to define different levels of importance of the logged messages and the ability to use different sink for the output - the console, a file, etc.

Also it's easy to enable or disable only some type of message when using a logger - for example you don't want to see every debug message in production.

If your application is using spring framework, then you can use use Log4j and AOP to implement this.

Setting the log level

There are 5 log levels that you can set:

`FATAL` — logs only fatal errors

`ERROR` — logs all errors

`WARN` — logs warnings, and all errors

`INFO` — logs information, warnings, and all errors

`DEBUG` — logs debug information, and all other levels

Sample log4j.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
 <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
   <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%d{ABSOLUTE} 
      %5p %c{1}:%L - %m%n"/> 
    </layout>
     </appender>
       <root>
      <priority value="debug"></priority>
      <appender-ref ref="stdout"/>
    </root>
</log4j:configuration>

In this configuration file , you can choose what you want to store .

Like %m means it will store method name . Similarly others, you have to read it for different usage . Good part is you can customized it according to your needs.

like image 174
vikiiii Avatar answered Nov 07 '22 21:11

vikiiii


You should consider using a logging framework as suggested by @vikiiii. I would go further and suggest you look at sl4fj which provides a seamless way of incorporating logging frameworks, including log4j. Whilst this may seem like more work (and more to learn), there are very good performance reasons for not using System.out.println().

Specifically, System.out.println() blocks all threads whilst it writes, impacting the performance of any significant application as described here

like image 41
Romski Avatar answered Nov 07 '22 21:11

Romski


Currently, after some testing, I see no way to achieve the same functionality in netbeans. Perhaps you could just stick with eclipse, as it seems to have worked well in the past for you.

If you really wanted to get this done in NetBeans (I still prefer (and recommend) eclipse), I would recommend taking a look at this plugin, and possibly editing it to add the SystemTrace functionality you are looking for.

like image 20
Azulflame Avatar answered Nov 07 '22 20:11

Azulflame