source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/transverse/util/log4j/AbstractConcisePatternConverter.java @ 9387

Last change on this file since 9387 was 9387, checked in by mlg, 13 years ago

First commit for jiwigo-ws-api

File size: 2.2 KB
Line 
1package fr.mael.jiwigo.transverse.util.log4j;
2
3import org.apache.log4j.helpers.FormattingInfo;
4import org.apache.log4j.helpers.PatternConverter;
5import org.apache.log4j.spi.LoggingEvent;
6
7/**
8 * @author mael
9 *
10 */
11public abstract class AbstractConcisePatternConverter extends PatternConverter {
12
13    /**
14     *
15     */
16    private int precision;
17
18    /**
19     * Constructeur de AbstractConcisePatternConverter.
20     * @param formattingInfo .
21     * @param precision .
22     */
23    public AbstractConcisePatternConverter(final FormattingInfo formattingInfo, final int precision) {
24        super(formattingInfo);
25
26        this.precision = precision;
27    }
28
29    /**
30     * @param paramLoggingEvent .
31     * @return le nom
32     */
33    protected abstract String getConciseName(LoggingEvent paramLoggingEvent);
34
35    /**.
36     * {@inheritDoc}
37     */
38    protected final String convert(final LoggingEvent event) {
39        final String n = getConciseName(event);
40
41        if (this.precision <= 0) {
42            return n;
43        }
44
45        final int len = n.length();
46
47        int end = len - 1;
48        final int debut = 46;
49        for (int i = this.precision; i > 0; --i) {
50            end = n.lastIndexOf(debut, end - 1);
51            if (end == -1) {
52                return n;
53            }
54        }
55        return n.substring(end + 1, len);
56    }
57
58    /**
59     * @param className le nom de la class
60     * @param maxLength la longueur
61     * @return la string
62     */
63    public static String simplify(final String className, final int maxLength) {
64        if (className.length() <= maxLength) {
65            return className;
66        }
67
68        final StringBuffer result = new StringBuffer();
69
70        int index = -1;
71        while (true) {
72            if (className.indexOf(".", index + 1) == -1) {
73                String remainingStr = className.substring(index + 1);
74
75                final int availableLength = maxLength - result.length();
76
77                if (remainingStr.length() > availableLength) {
78                    remainingStr = remainingStr.substring(0, availableLength - 1) + '~';
79                }
80
81                result.append(remainingStr);
82
83                break;
84            }
85
86            result.append(className.charAt(index + 1));
87
88            if (result.length() + 1 == maxLength) {
89                result.append('~');
90
91                break;
92            }
93
94            result.append('.');
95
96            index = className.indexOf(".", index + 1);
97        }
98
99        return result.toString();
100    }
101}
Note: See TracBrowser for help on using the repository browser.