22
33import com .intellij .openapi .diagnostic .Logger ;
44import com .intellij .openapi .project .Project ;
5+ import org .digma .intellij .plugin .analytics .NoSelectedEnvironmentException ;
56import org .digma .intellij .plugin .errorreporting .*;
67
78import java .time .Duration ;
@@ -31,6 +32,11 @@ public static void log(Consumer<String> consumer, String format, Object... args)
3132 }
3233
3334 public static void debugWithException (Logger logger ,Throwable e , String format , Object ... args ) {
35+
36+ if (!logger .isDebugEnabled ()) {
37+ return ;
38+ }
39+
3440 if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , e )) {
3541 return ;
3642 }
@@ -39,24 +45,43 @@ public static void debugWithException(Logger logger,Throwable e, String format,
3945 }
4046
4147 public static void debugWithException (Logger logger , Project project ,Throwable e , String format , Object ... args ) {
48+
49+ if (!logger .isDebugEnabled ()) {
50+ return ;
51+ }
52+
4253 if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , e )) {
4354 return ;
4455 }
56+
4557 logger .debug (DIGMA_PROJECT + project .getName () + ": " + String .format (format .replace ("{}" , "%s" ), args ),e );
4658 }
4759
60+
4861 public static void warnWithException (Logger logger ,Throwable e , String format , Object ... args ) {
49- if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , e )) {
50- return ;
62+
63+ //don't log NoSelectedEnvironmentException in warn level
64+ if (e instanceof NoSelectedEnvironmentException ) {
65+ debugWithException (logger , e , format , args );
66+ } else {
67+ if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , e )) {
68+ return ;
69+ }
70+ logger .warn (DIGMA + String .format (format .replace ("{}" , "%s" ), args ), e );
5171 }
52- logger .warn (DIGMA + String .format (format .replace ("{}" , "%s" ), args ),e );
5372 }
5473
5574 public static void warnWithException (Logger logger , Project project ,Throwable e , String format , Object ... args ) {
56- if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , e )) {
57- return ;
75+
76+ //don't log NoSelectedEnvironmentException in warn level
77+ if (e instanceof NoSelectedEnvironmentException ) {
78+ debugWithException (logger , project , e , format , args );
79+ } else {
80+ if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , e )) {
81+ return ;
82+ }
83+ logger .warn (DIGMA_PROJECT + project .getName () + ": " + String .format (format .replace ("{}" , "%s" ), args ), e );
5884 }
59- logger .warn (DIGMA_PROJECT + project .getName () + ": " + String .format (format .replace ("{}" , "%s" ), args ),e );
6085 }
6186
6287
@@ -68,30 +93,40 @@ public static void log(Consumer<String> consumer, String msg) {
6893 consumer .accept (DIGMA + msg );
6994 }
7095
71- public static void error (Logger logger ,Project project , Exception exception , String format , Object ... args ) {
72- if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , exception )) {
73- return ;
74- }
7596
76- var msg = String .format (format .replace ("{}" , "%s" ), args );
77- error (logger , exception , DIGMA_PROJECT + project .getName () + ": " + msg );
78- ErrorReporter .getInstance ().reportError (project , "Log.error" , exception );
97+ //Note: We should never log error in intellij because logging error will popup a red error to the user.
98+
99+ private static void error (Logger logger , Project project , Exception exception , String format , Object ... args ) {
100+
101+ if (exception instanceof NoSelectedEnvironmentException ) {
102+ debugWithException (logger , exception , format , args );
103+ } else {
104+
105+ if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , exception )) {
106+ return ;
107+ }
108+
109+ var msg = String .format (format .replace ("{}" , "%s" ), args );
110+ error (logger , exception , DIGMA_PROJECT + project .getName () + ": " + msg );
111+ ErrorReporter .getInstance ().reportError (project , "Log.error" , exception );
112+ }
79113 }
80- public static void error (Logger logger , Exception exception , String format , Object ... args ) {
114+
115+ private static void error (Logger logger , Exception exception , String format , Object ... args ) {
81116 if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (format , exception )) {
82117 return ;
83118 }
84119 error (logger , exception , DIGMA + String .format (format .replace ("{}" , "%s" ), args ));
85120 }
86121
87- public static void error (Logger logger , Exception exception , String msg ) {
122+ private static void error (Logger logger , Exception exception , String msg ) {
88123 if (FREQUENT_ERROR_DETECTOR .isTooFrequentException (msg , exception )) {
89124 return ;
90125 }
91126 logger .error (DIGMA + msg , exception );
92127 }
93128
94- public static void error (Logger logger , String msg ) {
129+ private static void error (Logger logger , String msg ) {
95130 logger .error (DIGMA + msg );
96131 }
97132
0 commit comments