@@ -45,13 +45,20 @@ This file is part of the iText (R) project.
45
45
public class LogListener extends TestWatcher {
46
46
47
47
private static final String ROOT_ITEXT_PACKAGE = "com.itextpdf" ;
48
+ private static final String ITEXT_LICENCING_PACKAGE = "com.itextpdf.licensing" ;
49
+ private static final String ITEXT_ACTIONS_PACKAGE = "com.itextpdf.commons.actions.processors" ;
50
+ private static final String TOKEN_ITEXT_LOGLEVEL = "ITEXT_SILENT_MODE" ;
48
51
49
- private final CustomListAppender <ILoggingEvent > listAppender = new CustomListAppender <>();
50
-
52
+ private final CustomListAppender <ILoggingEvent > listAppender ;
51
53
private final ILoggerFactory lc = LoggerFactory .getILoggerFactory ();
52
54
53
55
private Map <Logger , Map <String , Appender <ILoggingEvent >>> appenders ;
54
56
57
+ public LogListener () {
58
+ String logLevel = getPropertyOrEnvironmentVariable (TOKEN_ITEXT_LOGLEVEL );
59
+ listAppender = new CustomListAppender <>(parseSilentMode (logLevel ));
60
+ }
61
+
55
62
@ Override
56
63
protected void starting (Description description ) {
57
64
before (description );
@@ -63,6 +70,13 @@ protected void finished(Description description) {
63
70
after ();
64
71
}
65
72
73
+ private boolean parseSilentMode (String logLevel ) {
74
+ if (logLevel == null ){
75
+ return false ;
76
+ }
77
+ return logLevel .equalsIgnoreCase ("TRUE" );
78
+ }
79
+
66
80
private int contains (LogMessage loggingStatement ) {
67
81
List <ILoggingEvent > list = listAppender .list ;
68
82
int index = 0 ;
@@ -159,9 +173,23 @@ private void checkLogMessages(Description description) {
159
173
}
160
174
}
161
175
176
+ private static String getPropertyOrEnvironmentVariable (String name ) {
177
+ String s = System .getProperty (name );
178
+ if (s == null ) {
179
+ s = System .getenv (name );
180
+ }
181
+ return s ;
182
+ }
183
+
162
184
private class CustomListAppender <E > extends ListAppender <ILoggingEvent > {
163
185
164
186
private Map <String , Boolean > expectedTemplates = new HashMap <>();
187
+ private final boolean runTestsInSilentMode ;
188
+
189
+ private CustomListAppender (boolean runTestsInSilentMode ) {
190
+ this .runTestsInSilentMode = runTestsInSilentMode ;
191
+ }
192
+
165
193
166
194
public void setExpectedTemplates (Map <String , Boolean > expectedTemplates ) {
167
195
this .expectedTemplates .clear ();
@@ -175,14 +203,27 @@ public void clear() {
175
203
176
204
protected void append (ILoggingEvent e ) {
177
205
if (!isExpectedMessageQuiet (e .getMessage ())){
178
- System .out .println (e .getLoggerName () + " " + e .getLevel () + " " + e .getMessage ());
206
+ if (shouldPrintMessage (e )){
207
+ System .out .println (e .getLoggerName () + " " + e .getLevel () + " " + e .getMessage ());
208
+ }
179
209
}
180
210
printStackTraceIfAny (e );
181
211
if (e .getLevel ().isGreaterOrEqual (Level .WARN ) || isExpectedMessage (e .getMessage ())) {
182
212
this .list .add (e );
183
213
}
184
214
}
185
215
216
+ private boolean shouldPrintMessage (ILoggingEvent level ) {
217
+ //Those 2 if statements are when we rely on the logmessages being printed for certain tests
218
+ if (level .getLoggerName ().startsWith (ITEXT_LICENCING_PACKAGE )){
219
+ return true ;
220
+ }
221
+ if (level .getLoggerName ().startsWith (ITEXT_ACTIONS_PACKAGE )){
222
+ return true ;
223
+ }
224
+ return !runTestsInSilentMode ;
225
+ }
226
+
186
227
private boolean isExpectedMessage (String message ) {
187
228
if (message != null ) {
188
229
for (String template : expectedTemplates .keySet ()) {
0 commit comments