Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClientOld / utils / Logger.java @ 3321

History | View | Annotate | Download (4.21 KB)

1
package org.gvsig.remoteClientOld.utils;
2

    
3
import org.apache.log4j.Category;
4

    
5
import java.io.CharArrayWriter;
6
import java.io.PrintWriter;
7
import java.io.IOException;
8
import java.util.Properties;
9

    
10
/**
11
 * @author        Laura Diaz
12
 * @version       1.0
13
 */
14
public class Logger 
15
{
16
        private static final String DEBUG_MAXSIZE_PROPERTY = "log4j.debug.maxsize";
17
        private static final String INFO_MAXSIZE_PROPERTY = "log4j.info.maxsize";
18

    
19
        private static Category m_category;
20
        private int m_maxSizeDebugMsg;
21
        private int m_maxSizeInfoMsg;  
22

    
23
        /**
24
         * Constructor
25
         *
26
         * Protected so, only classes in the same package or descendants can instanciate this
27
         */
28
        protected Logger(String name)
29
        {
30
                m_category = Category.getInstance(name);
31
                
32
                m_maxSizeDebugMsg = readMaxSizeProperty(DEBUG_MAXSIZE_PROPERTY, 0);
33
                m_maxSizeInfoMsg = readMaxSizeProperty(INFO_MAXSIZE_PROPERTY, 0);
34
        }
35
        
36
        /**
37
         * Adds a debug message
38
         */
39
        public void debug(String msg)
40
        {
41
                /*
42
                 * Write message to log. If enabled, if the message is too long, it
43
                 * will be truncated.
44
                 */
45
                if (m_maxSizeDebugMsg <= 0)
46
                {
47
                  m_category.debug(msg + "\n");
48
                }
49
                else {
50
                  if (msg.length() <= m_maxSizeDebugMsg)
51
                  {
52
                        m_category.debug(msg + "\n");
53
                  }
54
                  else
55
                  {
56
                        StringBuffer sb = new StringBuffer(msg.substring(0, m_maxSizeDebugMsg));
57
                        sb.append(" [TRUNCATED BY LOGGER SEE property \"");
58
                        sb.append(DEBUG_MAXSIZE_PROPERTY);
59
                        sb.append("\"]\n");
60
                        m_category.debug(sb.toString());
61
                  }
62
                }
63
        }
64

    
65
        /**
66
         * Adds an info message
67
         */
68
        public void info(String msg)
69
        {
70
                /*
71
                 * Write message to log. If enabled, if the message is too long, it
72
                 * will be truncated.
73
                 */
74
                if (m_maxSizeInfoMsg <= 0)
75
                {
76
                  m_category.info(msg + "\n");
77
                }
78
                else {
79
                  if (msg.length() <= m_maxSizeInfoMsg)
80
                  {
81
                        m_category.info(msg + "\n");
82
                  }
83
                  else
84
                  {
85
                        StringBuffer sb = new StringBuffer(msg.substring(0, m_maxSizeInfoMsg));
86
                        sb.append(" [TRUNCATED BY LOGGER SEE property \"");
87
                        sb.append(INFO_MAXSIZE_PROPERTY);
88
                        sb.append("\"]\n");
89
                        m_category.info(sb.toString());
90
                  }
91
                }
92
        }
93

    
94
        /**
95
         * Adds an warning message
96
         */
97
        public void warning(String msg)
98
        {
99
                m_category.warn(msg + "\n");
100
        }
101

    
102
        /**
103
         * Adds an error message
104
         */
105
        public void error(String msg)
106
        {
107
                m_category.fatal(msg + "\n");
108
        }
109

    
110

    
111
        /**
112
         * Writes a message to the error log. This message includes the
113
         * detail message of the given exception as well as the
114
         * exception's stack trace.
115
         * 
116
         * @param  descr  description of the error.
117
         * @param  e      exception that was caught. 
118
         */
119
        public void error(String descr, Exception e)
120
        {        
121
                m_category.fatal(descr + ": detail message = \"" + e.getMessage()
122
                        + "\", trace =\n" + Logger.stackTraceToString(e));
123
        }
124

    
125
        /**
126
         * Converts the stack trace of the given exception to a string.
127
         */  
128
        public static String stackTraceToString(Exception e)
129
        {
130
          try
131
          {
132
                CharArrayWriter writer = new CharArrayWriter();
133
          
134
                e.printStackTrace(new PrintWriter(writer));
135
                return writer.toString();
136
          }
137
          catch(Exception e2)
138
          {
139
                return " [failed to convert stacktrace to string] ";
140
          }
141
        }
142

    
143
        /**
144
         * Reads the named property from the logger properties file and returns
145
         * it. If the property is missing or if its value is invalid, the value
146
         * of <code>defValue</code> is returned.
147
         */        
148
        private int readMaxSizeProperty(String propName, int defValue)
149
        {
150
                try
151
                {
152
                        Properties props = PropertyManager.getProperties(
153
                                                                PropertyManager.LOGGER_PROPERTIES);
154

    
155
                        if (props == null)
156
                        {
157
                                System.err.println("[Logger] Failed to read property \""
158
                                        + propName + "\": cannot get properties of "
159
                                        + PropertyManager.LOGGER_PROPERTIES);
160
                                return defValue;
161
                        }
162
                        
163
                        int n = defValue;
164
                        String s = props.getProperty(propName);
165

    
166
                        if (s != null && s.length() != 0)
167
                        {
168
                                try
169
                                {
170
                                        int k  = Integer.parseInt(s);
171
                                        if (k > 0)
172
                                        {
173
                                                n = k;
174
                                        }
175
                                }
176
                                catch(NumberFormatException e)
177
                                {
178
                                        System.err.println("[Logger] invalid value for property \""
179
                                                + propName + "\": not an integer: " + s
180
                                                + "---ignoring property");
181
                                        return defValue;
182
                                }
183
                        }
184
                        return n;
185
                }
186
                catch(IOException e)
187
                {
188
                        System.err.println("[Logger] Failed to read property \"" + propName
189
                                + "\": I/O error: " + e.getMessage() + "---ignoring property");
190
                                
191
                        return defValue;
192
                }
193
        }
194
}