gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / logger / FilteredLogger.java @ 2888
History | View | Annotate | Download (3.58 KB)
1 |
|
---|---|
2 |
|
3 |
package org.gvsig.tools.logger; |
4 |
|
5 |
import org.slf4j.Logger; |
6 |
|
7 |
public class FilteredLogger { |
8 |
protected int count = 0; |
9 |
protected final Logger logger; |
10 |
protected final int max; |
11 |
protected String processName; |
12 |
protected long interval; |
13 |
protected long lastTime; |
14 |
protected boolean showTooManyErrorsMessage; |
15 |
|
16 |
public FilteredLogger(Logger logger, String processName, int max) { |
17 |
this.max = max;
|
18 |
this.logger = logger;
|
19 |
this.processName = processName;
|
20 |
this.count = 0; |
21 |
this.interval = -1; |
22 |
this.lastTime = -1; |
23 |
this.showTooManyErrorsMessage = false; |
24 |
} |
25 |
|
26 |
public FilteredLogger(Logger logger, String processName, long milis) { |
27 |
this(logger, processName, 0); |
28 |
this.interval = milis;
|
29 |
} |
30 |
|
31 |
public FilteredLogger setInterval(long interval) { |
32 |
this.interval = interval;
|
33 |
return this; |
34 |
} |
35 |
|
36 |
protected boolean canDumpMoreMessages() { |
37 |
long now = System.currentTimeMillis(); |
38 |
if(this.max > 0 && interval > 0){ |
39 |
if( this.lastTime == 0){ |
40 |
if( ++this.count < this.max ) { |
41 |
return true; |
42 |
} else if( this.count == this.max ) { |
43 |
this.logger.info("Too many errors, don't dump more in this process ("+processName+") until "+this.interval+"ms."); |
44 |
this.lastTime = now;
|
45 |
} |
46 |
return false; |
47 |
} |
48 |
if( now - this.lastTime > this.interval ) { |
49 |
return false; |
50 |
} |
51 |
this.lastTime = 0; |
52 |
this.count = 0; |
53 |
return true; |
54 |
} else if( interval>0 ) { |
55 |
if( this.lastTime < 0 ) { |
56 |
this.lastTime = now;
|
57 |
this.showTooManyErrorsMessage = true; |
58 |
return true; |
59 |
} |
60 |
if( now - this.lastTime > this.interval ) { |
61 |
this.lastTime = now;
|
62 |
this.showTooManyErrorsMessage = true; |
63 |
return true; |
64 |
} |
65 |
|
66 |
if( this.showTooManyErrorsMessage ) { |
67 |
this.logger.info("Too many errors, skip some in this process ("+processName+")."); |
68 |
this.showTooManyErrorsMessage = false; |
69 |
} |
70 |
return false; |
71 |
} else {
|
72 |
if( ++this.count < this.max ) { |
73 |
return true; |
74 |
} else if( this.count == this.max ) { |
75 |
this.logger.info("Too many errors, don't dump more in this process ("+processName+")."); |
76 |
} |
77 |
return false; |
78 |
} |
79 |
} |
80 |
|
81 |
public void warn(String msg, Throwable th) { |
82 |
if( canDumpMoreMessages() ) {
|
83 |
this.logger.warn(msg,th);
|
84 |
} |
85 |
} |
86 |
|
87 |
public void warn(String msg) { |
88 |
if( canDumpMoreMessages() ) {
|
89 |
this.logger.warn(msg);
|
90 |
} |
91 |
} |
92 |
|
93 |
public void error(String msg, Throwable th) { |
94 |
if( canDumpMoreMessages() ) {
|
95 |
this.logger.error(msg,th);
|
96 |
} |
97 |
} |
98 |
|
99 |
public void error(String msg) { |
100 |
if( canDumpMoreMessages() ) {
|
101 |
this.logger.error(msg);
|
102 |
} |
103 |
} |
104 |
|
105 |
public void info(String msg) { |
106 |
if( canDumpMoreMessages() ) {
|
107 |
this.logger.info(msg);
|
108 |
} |
109 |
} |
110 |
} |