1 package org.astrogrid.mySpace.mySpaceStatus;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 /***
10 * The <code>Logger</code> class is the MySpace class for logging
11 * messages.
12 *
13 * <p>
14 * The <code>Logger</code> class is used to log both error and
15 * informational messages. The messages may optionally be sent to
16 * any combination of: (i) the standard AstroGrid log file (using
17 * commons logging). (ii) a log file local to the MySpace System and
18 * (iii) standard output.
19 *
20 * @author A C Davenhall (Edinburgh)
21 * @since Iteration 4.
22 * @version Iteration 5.
23 */
24
25 public class Logger
26 { private static boolean astroGridLog = true;
27 private static boolean mySpaceLog = true;
28 private static boolean echoLog = true;
29
30
31 private static String mySpaceLogFileName = "./myspace.log";
32
33 private static String accountId = null;
34 private static String actionName = null;
35
36
37
38
39 private static PrintWriter mySpaceLogWriter;
40
41
42
43
44 private static Log commonsLog = LogFactory.getLog(Logger.class);
45
46
47
48
49
50
51 /***
52 * Create a <code>MySpaceStatus</code> object.
53 *
54 * @param astroGridLog Flag indicating whether messages are to be
55 * written to the standard AstroGrid log.
56 * @param mySpaceLog Flag indicating whether messages are to be
57 * written to the local MySpace log file.
58 * @param echoLog Flag indicating whether messages are to be
59 * written to standard output.
60 * @param mySpaceLogFileName Name of the local MySpace log file. The
61 * name should include the full, absolute directory path of the name.
62 */
63
64 public Logger (boolean astroGridLog, boolean mySpaceLog, boolean echoLog,
65 String mySpaceLogFileName)
66 { this.astroGridLog = astroGridLog;
67 this.mySpaceLog = mySpaceLog;
68 this.echoLog = echoLog;
69 this.mySpaceLogFileName = mySpaceLogFileName;
70
71
72
73
74
75
76
77
78 if (astroGridLog)
79 { try
80 {
81 }
82 catch (Exception all)
83 { all.printStackTrace();
84 }
85 }
86
87
88
89
90
91
92 if (mySpaceLog)
93 { try
94 { FileOutputStream fos = new FileOutputStream(
95 mySpaceLogFileName, true);
96 mySpaceLogWriter = new PrintWriter(fos);
97
98 Date startDate = new Date();
99 String startMessage = "===== Start of MySpace Service "
100 + "logging session " + startDate.toString();
101
102 mySpaceLogWriter.println(startMessage);
103 mySpaceLogWriter.flush();
104 }
105 catch (Exception all)
106 { all.printStackTrace();
107 }
108 }
109 }
110
111
112 /***
113 * Create a <code>Logger</code> object and pass a message. The
114 * logging configuration parameters are unaltered.
115 *
116 * @param message Message to be logged.
117 */
118
119 public Logger (String message)
120 { this.appendMessage(message);
121 }
122
123
124 /***
125 * Constructor with no arguments.
126 */
127
128 public Logger ()
129 {
130 }
131
132
133
134
135
136
137
138 /***
139 * Set the details of the current Account using the MySpace system.
140 *
141 * @param account Account identifier or name.
142 */
143
144 public void setAccount (String accountId)
145 { this.accountId = accountId;
146 }
147
148
149 /***
150 * Set the details of the current action.
151 *
152 * @param actionName Name of the current action.
153 */
154
155 public void setActionName (String actionName)
156 { this.actionName = actionName;
157 }
158
159
160 /***
161 * Append a message to any of the logs which are currently in operation.
162 */
163
164 public void appendMessage(String message)
165 {
166
167
168
169
170 Date currentDate = new Date();
171
172 String messageHead = "--- MySpace " + currentDate.toString()
173 + " (";
174
175 if (accountId != null)
176 { messageHead = messageHead + accountId + ", ";
177 }
178
179 if (actionName != null)
180 { messageHead = messageHead + actionName;
181 }
182
183 messageHead = messageHead + ") --- \n";
184
185
186
187
188 String completeMessage = messageHead + " " + message + "\n";
189
190
191
192
193 if (astroGridLog)
194 { try
195 { commonsLog.info(completeMessage);
196 }
197 catch (Exception all)
198 { all.printStackTrace();
199 }
200 }
201
202
203
204
205 if (mySpaceLog)
206 { try
207 { mySpaceLogWriter.println(completeMessage);
208 mySpaceLogWriter.flush();
209 }
210 catch (Exception all)
211 { all.printStackTrace();
212 }
213 }
214
215
216
217
218
219 if (echoLog)
220 { System.out.println(message);
221 }
222 }
223
224
225 /***
226 * Close the log files.
227 */
228
229 public void close()
230 {
231
232
233
234
235
236
237
238
239 if (mySpaceLog)
240 { try
241 { Date closeDate = new Date();
242 String closeMessage = "--- Close of MySpace logging "
243 + "session " + closeDate.toString() + "\n\n\n";
244
245 mySpaceLogWriter.println(closeMessage);
246 mySpaceLogWriter.flush();
247 mySpaceLogWriter.close();
248 }
249 catch (Exception all)
250 { all.printStackTrace();
251 }
252 }
253 }
254 }