EnterpriseLog.java
5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package it.softecspa.fileproxy.services.common;
import it.softecspa.PortalSettings;
import it.softecspa.database.dbconnect.ConnectionManager;
import it.softecspa.fileproxy.DatabaseBalancer;
import it.softecspa.fileproxy.db.Log;
import it.softecspa.fileproxy.services.ClusterSynchronizer;
import it.softecspa.kahuna.lang.XString;
import it.softecspa.kahuna.log.Ticket;
import it.softecspa.kahuna.services.PostmanPat;
import it.softecspa.kahuna.util.calendar.EnterpriseCalendar;
import it.softecspa.portal.Parameters;
import java.sql.SQLException;
import java.util.Calendar;
import org.apache.log4j.Logger;
@SuppressWarnings("serial")
public class EnterpriseLog extends Throwable {
private Logger log = Logger.getLogger(getClass());
private final int LOG_LIFE = 30;
public EnterpriseLog(String message, Throwable e) {
super(message, e);
}
public EnterpriseLog(String message) {
super(message);
}
public EnterpriseLog(Throwable e) {
super(e);
}
public int write() {
return write(ClusterSynchronizer.getInstance().getPublicHostName(), DatabaseBalancer.getInstance().getStage(), null);
}
public int write(String source) {
return write(source, DatabaseBalancer.getInstance().getStage(), null);
}
/*
public int write(String source, ConnectionManager cm) {
return write(source, cm, null);
}
*/
/*
public int write(String source, Integer ticket) {
return write(source, DatabaseBalancer.getInstance().getStage(), ticket);
}
*/
/**
* Scrive l'errore su database nella tabella dei log
* @param source
* @param cm
* @param ticket
* @return
*/
private int write(String source, ConnectionManager cm, Integer ticket) {
try {
String message = getMessage();
if (ticket==null) ticket = Ticket.generaTicket();
try {
// Metto una traccia nel log
log.error("Traced error on database log with ticket #"+ticket +"\n" +
">> "+ (message != null ? message : toString()), (this.getCause()!=null?this.getCause():null));
//
Log elog = new Log();
elog.setSource(source);
elog.setMessage(message != null ? message : toString());
elog.setTicket(ticket);
if (this.getCause()!=null) elog.setStacktrace(stringStackTrace(this.getCause()));
elog.insert(cm);
// Cancella tutti gli errori più vecchi di 30 giorni
EnterpriseCalendar before = EnterpriseCalendar.now();
before.add(Calendar.DAY_OF_MONTH, -LOG_LIFE);
flushBefore(cm, before);
} catch (SQLException e) {
log.fatal("Unhandled SQLException writing error on database log",e);
} catch (Exception e) {
log.fatal("Unhandled Exception writing error on database log",e);
} catch (Error e) {
log.fatal("Unhandled Error writing error on database log",e);
}
return ticket;
} catch (Exception e) {
Logger.getLogger(EnterpriseLog.class).fatal("Very unhandled exception",e);
return -1;
}
}
/**
* Invia una mail al gestore del servizio con la segnalazione
*/
public void sendEmail() {
sendEmail("Exception");
}
/**
* Invia una mail al gestore del servizio con la segnalazione
* impostando il subject
*/
public void sendEmail(String subject) {
sendEmail(PortalSettings.MAIL_ERROR_ENABLE, subject);
}
/**
* Invia una mail al gestore del servizio con la segnalazione
* impostando il subject
* @param param
* @param subject
*/
public void sendEmail(String param, String subject) {
if (XString.isNotBlankNullTrim(param)) {
Parameters parameters = Parameters.getInstance();
if (!parameters.getBoolean(param, false)) return;
}
PostmanPat pat = new PostmanPat();
pat.setSubject(subject);
pat.addMessage(getMessage());
if (getCause()!=null) {
pat.addMessage("\n"+
stringStackTrace(getCause()));
}
//
pat.send();
}
/**
* Cancella tutti i messaggi dal log su database
* @param cm
*/
public static void flushAll(ConnectionManager cm) {
try {
Log elog = new Log();
elog.delete(cm);
} catch (SQLException e) {
Logger.getLogger(EnterpriseLog.class).fatal("Impossible to delete record from "+Log.NAME, e);
}
}
/**
* Cancella i messaggi più vecchi della data indicata
* @param cm
* @param before
*/
public static void flushBefore(ConnectionManager cm, EnterpriseCalendar before) {
try {
Log elog = new Log();
elog.setRecord(before);
elog.deleteBefore(cm);
} catch (SQLException e) {
Logger.getLogger(EnterpriseLog.class).fatal("Impossible to delete record from "+Log.NAME, e);
}
}
/**
* Provides programmatic access to the stack trace information printed by
* {@link #stringStackTrace()}. Returns a string of stack trace elements,
* each representing one stack frame.
*
* @return a string of stack trace elements representing the stack trace
* pertaining to this throwable.
*/
public String stringStackTrace(Throwable e) {
String s = e.toString() + "\n";
StackTraceElement[] trace = e.getStackTrace();
for (int i = 0; i < trace.length; i++) {
s += "\tat " + trace[i] + "\n";
}
if (e.getCause() != null) s += "Caused by: "+stringStackTrace(e.getCause());
return s;
}
}