View Javadoc

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; // Write AstroGrid log?
27     private static boolean mySpaceLog = true;   // Write MySpace log?
28     private static boolean echoLog = true;      // Echo log to standard out?
29  
30  //    MySpace log file name.
31     private static String mySpaceLogFileName = "./myspace.log";
32  
33     private static String accountId = null;   // Account Identifier.
34     private static String actionName = null;  // Name of invoking action.
35  
36  //
37  //PrintWriter for the MySpace log.
38  
39     private static PrintWriter mySpaceLogWriter;
40  
41  //
42  //Logger for the Jakata commons logging.
43  
44     private static Log commonsLog = LogFactory.getLog(Logger.class); 
45  
46  //
47  // ------------------------------------------------------------------------
48  //
49  // Constructors.
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  //   If required attempt to create an AstroGrid logging object using
73  //   commons logging.
74  //
75  //   Note that the error reporting is quite crude because the Logger
76  //   objects exist `beneath' the error reporting system.
77  
78        if (astroGridLog)
79        {  try
80           {
81           }
82           catch (Exception all)
83           {  all.printStackTrace();
84           }
85        }
86  
87  //
88  //   If required attempt to open a local MySpace log file.  The file
89  //   is opened such that any new messages are appended to an existing
90  //   file.
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 // Methods.
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 //   Assemble a message header comprising a date stamp and the current
168 //   userId and communityId.
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 //   Assemble the complete, final message.
187 
188       String completeMessage = messageHead + "  " + message + "\n";
189 
190 //
191 //   If required append the message to the AstroGrid log.
192 
193       if (astroGridLog)
194       {  try
195          {  commonsLog.info(completeMessage);
196          }
197          catch (Exception all)
198          {  all.printStackTrace();
199          }
200       }
201 
202 //
203 //   If required append the message to the local MySpace log.
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 //   If required write the message to standard output.  Note that in
217 //   this case only the basic message, and not the header, is output.
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 //   The standard AstroGrid log would be closed here if any closing
234 //   was required.
235 
236 //
237 //   If required close the local MySpace log.
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 }