Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / logger / FilteredLogger.java @ 2001

History | View | Annotate | Download (2.37 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 void setInterval(long interval) {
27
            this.interval = interval;
28
        }
29

    
30
        protected boolean canDumpMoreMessages() {
31
            if( interval>0 ) {
32
                long now = System.currentTimeMillis();
33
                if( this.lastTime < 0 ) {
34
                    this.lastTime = now;
35
                    this.showTooManyErrorsMessage = true;
36
                    return true;
37
                }
38
                if( this.lastTime+this.interval > now ) {
39
                    this.lastTime = now;
40
                    this.showTooManyErrorsMessage = true;
41
                    return true;
42
                }
43
                if( this.showTooManyErrorsMessage ) {
44
                    this.logger.info("Too many errors, skip some in this process ("+processName+").");
45
                    this.showTooManyErrorsMessage = false;
46
                }
47
                return false;
48
            } else {
49
                if( ++this.count < this.max ) {
50
                    return true;
51
                } else if( this.count == this.max ) {
52
                    this.logger.info("Too many errors, don't dump more in this process ("+processName+").");
53
                }
54
                return false;
55
            }
56
        }
57
                
58
        public void warn(String msg, Throwable th) {
59
            if( canDumpMoreMessages() ) {
60
                this.logger.warn(msg,th);
61
            }
62
        }
63
        
64
        public void warn(String msg) {
65
            if( canDumpMoreMessages() ) {
66
                this.logger.warn(msg);
67
            }
68
        }
69
        
70
        public void info(String msg) {
71
            if( canDumpMoreMessages() ) {
72
                this.logger.info(msg);
73
            }
74
        }
75
}