This project has retired. For details please refer to its Attic page.
Source code
001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.reef.util.logging;
021
022import org.apache.reef.tang.JavaConfigurationBuilder;
023import org.apache.reef.tang.Tang;
024import org.apache.reef.tang.annotations.Parameter;
025import org.apache.reef.wake.time.event.StartTime;
026
027import javax.inject.Inject;
028import java.util.logging.Level;
029import java.util.logging.Logger;
030
031/**
032 * Create Logging scope objects
033 */
034public class LoggingScopeFactory {
035
036  private static final Logger LOG = Logger.getLogger(LoggingScopeFactory.class.getName());
037  public static final String DRIVER_START = "Driver Start Handler";
038  public static final String DRIVER_STOP = "Driver Stop Handler";
039  public static final String BRIDGE_SETUP = "Bridge setup";
040  public static final String EVALUATOR_REQUESTOR = "Evaluator requestor passed to C#";
041  public static final String EVALUATOR_BRIDGE_SUBMIT = "Evaluator request submit cross bridge";
042  public static final String EVALUATOR_SUBMIT = "Evaluator submit";
043  public static final String EVALUATOR_LAUNCH = "Evaluator launch";
044  public static final String EVALUATOR_ALLOCATED = "Evaluator allocated";
045  public static final String EVALUATOR_COMPLETED = "Evaluator completed";
046  public static final String EVALUATOR_FAILED = "Evaluator failed";
047  public static final String ACTIVE_CONTEXT = "Active context created";
048  public static final String TASK_RUNNING = "Task running";
049  public static final String TASK_COMPLETE = "Task complete";
050  public static final String TASK_MESSAGE = "Task message";
051  public static final String CONTEXT_MESSAGE = "Context message";
052  public static final String CONTEXT_CLOSE = "Context close";
053  public static final String DRIVER_RESTART = "Driver restart";
054  public static final String DRIVER_RESTART_COMPLETE = "Driver restart complete";
055  public static final String DRIVER_RESTART_RUNNING_TASK = "Driver restart running task";
056  public static final String DRIVER_RESTART_ACTIVE_CONTEXT = "Driver restart active context";
057  public static final String TASK_SUSPEND = "Task suspend";
058  public static final String DRIVER_SUBMIT = "Driver submit";
059  public static final String REEF_SUBMIT = "Reef submit";
060  public static final String LOCAL_JOB_SUBMIT = "Local job submit";
061  public static final String HTTP_REQUEST = "Http request";
062  public static final String HTTP_SERVER = "Http server";
063
064  /**
065   * Log level. Client can set it through LogLevelName named parameter
066   */
067  private final Level logLevel;
068
069  /**
070   * User can inject a LoggingScopeFactory with injected log level as a string
071   */
072  @Inject
073  private LoggingScopeFactory(@Parameter(LogLevelName.class) final String logLevelName) {
074    this.logLevel = Level.parse(logLevelName);
075  }
076
077  /**
078   * Get a new instance of LoggingScope with specified log level
079   * @param logLevel
080   * @param msg
081   * @return
082   */
083  public static LoggingScope getNewLoggingScope(final Level logLevel, final String msg) {
084    return new LoggingScopeImpl(LOG, logLevel, msg);
085  }
086
087  /**
088   * Get a new instance of LoggingScope with injected LoggingScopeFactory instance
089   * @param msg
090   * @return
091   */
092  public LoggingScope getNewLoggingScope(final String msg) {
093    return new LoggingScopeImpl(LOG, logLevel, msg);
094  }
095
096  /**
097   * Get a new instance of LoggingScope with msg and params through new
098   * @param msg
099   * @param params
100   * @return
101   */
102  public LoggingScope getNewLoggingScope(final String msg, final Object params[]) {
103    return new LoggingScopeImpl(LOG, logLevel, msg, params);
104  }
105
106  /**
107   * The method is to measure the time used to start the driver. It can be inserted to the code between start driver till it is started
108   * @param startTime
109   * @return
110   */
111  public LoggingScope driverStart(final StartTime startTime) {
112    return new LoggingScopeImpl(LOG, logLevel, DRIVER_START + " :" + startTime);
113  }
114
115  /**
116   * The method is to measure the time used to stop the driver. It can be inserted to the code between start driver stop till it is stopped
117   * @param timeStamp
118   * @return
119   */
120  public LoggingScope driverStop(final long timeStamp) {
121    return new LoggingScopeImpl(LOG, logLevel, this.DRIVER_STOP + " :" + timeStamp);
122  }
123
124  /**
125   * The method is to measure the time used to set up Java CRL bridge. It can be inserted to the code between beginning of bridge set up and the end of it
126   * @return
127   */
128  public LoggingScope setupBridge() {
129    return new LoggingScopeImpl(LOG, logLevel, BRIDGE_SETUP);
130  }
131
132  /**
133   * The method is to measure the time used to pass EvaluatorRequestor from Java to .Net. It can be inserted to the code between beginning to send EvaluatorRequestor to CLR until it is returned.
134   * @return
135   */
136  public LoggingScope evaluatorRequestorPassToCs() {
137    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_REQUESTOR);
138  }
139
140  /**
141   * The method is to measure the time used to submit Evaluator request from CLR to Java driver. It can be inserted to evaluator submit() method.
142   * @param evaluatorsNumber
143   * @return
144   */
145  public LoggingScope evaluatorRequestSubmitToJavaDriver(final int evaluatorsNumber) {
146    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_BRIDGE_SUBMIT + ":" + evaluatorsNumber);
147  }
148
149  /**
150   * The method is to measure the time used to submit a Evaluator request at java side
151   * @param evaluatorNumber
152   * @return
153   */
154  public LoggingScope evaluatorSubmit(final int evaluatorNumber) {
155    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_SUBMIT + ":" + evaluatorNumber);
156  }
157
158  /**
159   * This is to measure the time on evaluatorAllocated handler
160   * @param evaluatorId
161   * @return
162   */
163  public LoggingScope evaluatorAllocated(final String evaluatorId) {
164    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_ALLOCATED + " :" + evaluatorId);
165  }
166
167  /**
168   * This is to measure the time to launch an evaluator
169   * @param evaluatorId
170   * @return
171   */
172  public LoggingScope evaluatorLaunch(final String evaluatorId) {
173    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_LAUNCH + " :" + evaluatorId);
174  }
175
176  /**
177   * This is to measure the time in calling evaluatorCompleted handler
178   * @param evaluatorId
179   * @return
180   */
181  public LoggingScope evaluatorCompleted(final String evaluatorId) {
182    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_COMPLETED + " :" + evaluatorId);
183  }
184
185  /**
186   * This is to measure the time in calling evaluatorFailed handler
187   * @param evaluatorId
188   * @return
189   */
190  public LoggingScope evaluatorFailed(final String evaluatorId) {
191    return new LoggingScopeImpl(LOG, logLevel, this.EVALUATOR_FAILED + " :" + evaluatorId);
192  }
193
194  /**
195   * This is to measure the time in calling activeContext handler
196   * @param contextId
197   * @return
198   */
199  public LoggingScope activeContextReceived(final String contextId) {
200    return new LoggingScopeImpl(LOG, logLevel, ACTIVE_CONTEXT + " :" + contextId);
201  }
202
203  /**
204   * This is to measure the time in calling closedContext handler
205   * @param contextId
206   * @return
207   */
208  public LoggingScope closedContext(final String contextId) {
209    return new LoggingScopeImpl(LOG, logLevel, this.CONTEXT_CLOSE + " :" + contextId);
210  }
211
212  /**
213   * This is to measure the time in calling runningTaskHandler
214   * @param taskId
215   * @return
216   */
217  public LoggingScope taskRunning(final String taskId) {
218    return new LoggingScopeImpl(LOG, logLevel, TASK_RUNNING + " :" + taskId);
219  }
220
221  /**
222   * This is to measure the time in calling taskCompletedHandler
223   * @param taskId
224   * @return
225   */
226  public LoggingScope taskCompleted(final String taskId) {
227    return new LoggingScopeImpl(LOG, logLevel, TASK_COMPLETE + " :" + taskId);
228  }
229
230  /**
231   * This is to measure the time in calling taskSuspendedHandler
232   * @param taskId
233   * @return
234   */
235  public LoggingScope taskSuspended(final String taskId) {
236    return new LoggingScopeImpl(LOG, logLevel, TASK_SUSPEND + " :" + taskId);
237  }
238
239  /**
240   * This is to measure the time in calling taskMessageReceivedHandler
241   * @param msg
242   * @return
243   */
244  public LoggingScope taskMessageReceived(final String msg) {
245    return new LoggingScopeImpl(LOG, logLevel, TASK_MESSAGE + " :" + msg);
246  }
247
248  /**
249   * This is to measure the time in calling contextMessageReceivedHandler
250   * @param msg
251   * @return
252   */
253  public LoggingScope contextMessageReceived(final String msg) {
254    return new LoggingScopeImpl(LOG, logLevel, CONTEXT_MESSAGE + " :" + msg);
255  }
256
257  /**
258   * This is to measure the time in calling driverRestartHandler
259   * @param startTime
260   * @return
261   */
262  public LoggingScope driverRestart(final StartTime startTime) {
263    return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART + " :" + startTime);
264  }
265
266  /**
267   * This is to measure the time in calling driverRestartCompletedHandler
268   * @param timeStamp
269   * @return
270   */
271  public LoggingScope driverRestartCompleted(final long timeStamp) {
272    return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_COMPLETE + " :" + timeStamp);
273  }
274
275  /**
276   * This is to measure the time in calling driverRestartRunningTaskHandler
277   * @param taskId
278   * @return
279   */
280  public LoggingScope driverRestartRunningTask(final String taskId) {
281    return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_RUNNING_TASK + " :" + taskId);
282  }
283
284  /**
285   * This is to measure the time in calling driverRestartActiveContextReceivedHandler
286   * @param contextId
287   * @return
288   */
289  public LoggingScope driverRestartActiveContextReceived(final String contextId) {
290    return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_ACTIVE_CONTEXT + " :" + contextId);
291  }
292
293  /**
294   * This is to measure the time in handling a http request
295   * @param uri
296   * @return
297   */
298  public LoggingScope httpRequest(final String uri) {
299    return new LoggingScopeImpl(LOG, logLevel, this.HTTP_REQUEST + " :" + uri);
300  }
301
302  /**
303   * This is to measure the time used to create HttpServer
304   * @return
305   */
306  public LoggingScope httpServer() {
307    return new LoggingScopeImpl(LOG, logLevel, this.HTTP_SERVER);
308  }
309
310  /**
311   * This is to measure the time to submit a driver
312   * @param submitDriver
313   * @return
314   */
315  public LoggingScope driverSubmit(final Boolean submitDriver) {
316    return new LoggingScopeImpl(LOG, logLevel, DRIVER_SUBMIT + " :" + submitDriver);
317  }
318
319  /**
320   * This is to measure the time to call Reef.Submit
321   * @return
322   */
323  public LoggingScope reefSubmit() {
324    return new LoggingScopeImpl(LOG, logLevel, this.REEF_SUBMIT);
325  }
326
327  /**
328   * This is to measure the time for a job submission
329   * @return
330   */
331  public LoggingScope localJobSubmission() {
332    return new LoggingScopeImpl(LOG, logLevel, this.LOCAL_JOB_SUBMIT);
333  }
334}