gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / logger / FilteredLogger.java @ 2402
History | View | Annotate | Download (2.54 KB)
1 | 1088 | jjdelcerro | |
---|---|---|---|
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 | 2001 | jjdelcerro | protected long interval; |
13 | protected long lastTime; |
||
14 | protected boolean showTooManyErrorsMessage; |
||
15 | 1088 | jjdelcerro | |
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 | 2001 | jjdelcerro | this.interval = -1; |
22 | this.lastTime = -1; |
||
23 | this.showTooManyErrorsMessage = false; |
||
24 | 1088 | jjdelcerro | } |
25 | 2001 | jjdelcerro | |
26 | 2402 | jjdelcerro | public FilteredLogger(Logger logger, String processName, long milis) { |
27 | this(logger, processName, 0); |
||
28 | this.interval = milis;
|
||
29 | } |
||
30 | |||
31 | 2001 | jjdelcerro | public void setInterval(long interval) { |
32 | this.interval = interval;
|
||
33 | } |
||
34 | 1088 | jjdelcerro | |
35 | protected boolean canDumpMoreMessages() { |
||
36 | 2001 | jjdelcerro | if( interval>0 ) { |
37 | long now = System.currentTimeMillis(); |
||
38 | if( this.lastTime < 0 ) { |
||
39 | this.lastTime = now;
|
||
40 | this.showTooManyErrorsMessage = true; |
||
41 | return true; |
||
42 | } |
||
43 | 2256 | fdiaz | if( now - this.lastTime > this.interval ) { |
44 | 2001 | jjdelcerro | this.lastTime = now;
|
45 | this.showTooManyErrorsMessage = true; |
||
46 | return true; |
||
47 | } |
||
48 | 2256 | fdiaz | |
49 | 2001 | jjdelcerro | if( this.showTooManyErrorsMessage ) { |
50 | this.logger.info("Too many errors, skip some in this process ("+processName+")."); |
||
51 | this.showTooManyErrorsMessage = false; |
||
52 | } |
||
53 | return false; |
||
54 | } else {
|
||
55 | if( ++this.count < this.max ) { |
||
56 | return true; |
||
57 | } else if( this.count == this.max ) { |
||
58 | this.logger.info("Too many errors, don't dump more in this process ("+processName+")."); |
||
59 | } |
||
60 | return false; |
||
61 | 1088 | jjdelcerro | } |
62 | } |
||
63 | |||
64 | public void warn(String msg, Throwable th) { |
||
65 | if( canDumpMoreMessages() ) {
|
||
66 | this.logger.warn(msg,th);
|
||
67 | } |
||
68 | } |
||
69 | |||
70 | public void warn(String msg) { |
||
71 | if( canDumpMoreMessages() ) {
|
||
72 | this.logger.warn(msg);
|
||
73 | } |
||
74 | } |
||
75 | |||
76 | public void info(String msg) { |
||
77 | if( canDumpMoreMessages() ) {
|
||
78 | this.logger.info(msg);
|
||
79 | } |
||
80 | } |
||
81 | } |