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.annotations.Parameter; 023import org.apache.reef.wake.time.event.StartTime; 024 025import javax.inject.Inject; 026import java.util.logging.Level; 027import java.util.logging.Logger; 028 029/** 030 * Create Logging scope objects. 031 */ 032public final class LoggingScopeFactory { 033 034 private static final Logger LOG = Logger.getLogger(LoggingScopeFactory.class.getName()); 035 public static final String DRIVER_START = "Driver Start Handler"; 036 public static final String DRIVER_STOP = "Driver Stop Handler"; 037 public static final String BRIDGE_SETUP = "Bridge setup"; 038 public static final String LOAD_LIB = "Load libraries"; 039 public static final String EVALUATOR_REQUESTOR = "Evaluator requestor passed to C#"; 040 public static final String EVALUATOR_BRIDGE_SUBMIT = "Evaluator request submit cross bridge"; 041 public static final String EVALUATOR_SUBMIT = "Evaluator submit"; 042 public static final String EVALUATOR_LAUNCH = "Evaluator launch"; 043 public static final String EVALUATOR_ALLOCATED = "Evaluator allocated"; 044 public static final String EVALUATOR_COMPLETED = "Evaluator completed"; 045 public static final String EVALUATOR_FAILED = "Evaluator failed"; 046 public static final String ACTIVE_CONTEXT = "Active context created"; 047 public static final String TASK_RUNNING = "Task running"; 048 public static final String TASK_COMPLETE = "Task complete"; 049 public static final String TASK_MESSAGE = "Task message"; 050 public static final String CONTEXT_MESSAGE = "Context message"; 051 public static final String CONTEXT_CLOSE = "Context close"; 052 public static final String DRIVER_RESTART = "Driver restart"; 053 public static final String DRIVER_RESTART_COMPLETE = "Driver restart complete"; 054 public static final String DRIVER_RESTART_RUNNING_TASK = "Driver restart running task"; 055 public static final String DRIVER_RESTART_ACTIVE_CONTEXT = "Driver restart active context"; 056 public static final String TASK_SUSPEND = "Task suspend"; 057 public static final String DRIVER_SUBMIT = "Driver submit"; 058 public static final String REEF_SUBMIT = "Reef submit"; 059 public static final String LOCAL_JOB_SUBMIT = "Local job submit"; 060 public static final String HTTP_REQUEST = "Http request"; 061 public static final String HTTP_SERVER = "Http server"; 062 063 /** 064 * Log level. Client can set it through LogLevelName named parameter 065 */ 066 private final Level logLevel; 067 068 /** 069 * User can inject a LoggingScopeFactory with injected log level as a string. 070 */ 071 @Inject 072 private LoggingScopeFactory(@Parameter(LogLevelName.class) final String logLevelName) { 073 this.logLevel = Level.parse(logLevelName); 074 } 075 076 /** 077 * Get a new instance of LoggingScope with specified log level. 078 * @param logLevel 079 * @param msg 080 * @return 081 */ 082 public static LoggingScope getNewLoggingScope(final Level logLevel, final String msg) { 083 return new LoggingScopeImpl(LOG, logLevel, msg); 084 } 085 086 /** 087 * Get a new instance of LoggingScope with injected LoggingScopeFactory instance. 088 * @param msg 089 * @return 090 */ 091 public LoggingScope getNewLoggingScope(final String msg) { 092 return new LoggingScopeImpl(LOG, logLevel, msg); 093 } 094 095 /** 096 * Get a new instance of LoggingScope with msg and params through new. 097 * @param msg 098 * @param params 099 * @return 100 */ 101 public LoggingScope getNewLoggingScope(final String msg, final Object[] params) { 102 return new LoggingScopeImpl(LOG, logLevel, msg, params); 103 } 104 105 /** 106 * The method is to measure the time used to start the driver. 107 * 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. 117 * It can be inserted to the code between start driver stop till it is stopped 118 * @param timeStamp 119 * @return 120 */ 121 public LoggingScope driverStop(final long timeStamp) { 122 return new LoggingScopeImpl(LOG, logLevel, DRIVER_STOP + ": " + timeStamp); 123 } 124 125 /** 126 * The method is to measure the time used to set up Java CRL bridge. 127 * It can be inserted to the code between beginning of bridge set up and the end of it 128 * @return 129 */ 130 public LoggingScope setupBridge() { 131 return new LoggingScopeImpl(LOG, logLevel, BRIDGE_SETUP); 132 } 133 134 /** 135 * The method is to measure the time used to load global files and libraries. 136 * @return 137 */ 138 public LoggingScope loadLib() { 139 return new LoggingScopeImpl(LOG, logLevel, LOAD_LIB); 140 } 141 142 /** 143 * The method is to measure the time used to pass EvaluatorRequestor from Java to .Net. 144 * It can be inserted to the code between beginning to send EvaluatorRequestor to CLR until it is returned. 145 * @return 146 */ 147 public LoggingScope evaluatorRequestorPassToCs() { 148 return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_REQUESTOR); 149 } 150 151 /** 152 * The method is to measure the time used to submit Evaluator request from CLR to Java driver. 153 * It can be inserted to evaluator submit() method. 154 * @param evaluatorsNumber 155 * @return 156 */ 157 public LoggingScope evaluatorRequestSubmitToJavaDriver(final int evaluatorsNumber) { 158 return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_BRIDGE_SUBMIT + ": " + evaluatorsNumber); 159 } 160 161 /** 162 * The method is to measure the time used to submit a Evaluator request at java side. 163 * @param evaluatorNumber 164 * @return 165 */ 166 public LoggingScope evaluatorSubmit(final int evaluatorNumber) { 167 return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_SUBMIT + ": " + evaluatorNumber); 168 } 169 170 /** 171 * This is to measure the time on evaluatorAllocated handler. 172 * @param evaluatorId 173 * @return 174 */ 175 public LoggingScope evaluatorAllocated(final String evaluatorId) { 176 return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_ALLOCATED + ": " + evaluatorId); 177 } 178 179 /** 180 * This is to measure the time to launch an evaluator. 181 * @param evaluatorId 182 * @return 183 */ 184 public LoggingScope evaluatorLaunch(final String evaluatorId) { 185 return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_LAUNCH + ": " + evaluatorId); 186 } 187 188 /** 189 * This is to measure the time in calling evaluatorCompleted handler. 190 * @param evaluatorId 191 * @return 192 */ 193 public LoggingScope evaluatorCompleted(final String evaluatorId) { 194 return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_COMPLETED + ": " + evaluatorId); 195 } 196 197 /** 198 * This is to measure the time in calling evaluatorFailed handler. 199 * @param evaluatorId 200 * @return 201 */ 202 public LoggingScope evaluatorFailed(final String evaluatorId) { 203 return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_FAILED + ": " + evaluatorId); 204 } 205 206 /** 207 * This is to measure the time in calling activeContext handler. 208 * @param contextId 209 * @return 210 */ 211 public LoggingScope activeContextReceived(final String contextId) { 212 return new LoggingScopeImpl(LOG, logLevel, ACTIVE_CONTEXT + ": " + contextId); 213 } 214 215 /** 216 * This is to measure the time in calling closedContext handler. 217 * @param contextId 218 * @return 219 */ 220 public LoggingScope closedContext(final String contextId) { 221 return new LoggingScopeImpl(LOG, logLevel, CONTEXT_CLOSE + ": " + contextId); 222 } 223 224 /** 225 * This is to measure the time in calling runningTaskHandler. 226 * @param taskId 227 * @return 228 */ 229 public LoggingScope taskRunning(final String taskId) { 230 return new LoggingScopeImpl(LOG, logLevel, TASK_RUNNING + ": " + taskId); 231 } 232 233 /** 234 * This is to measure the time in calling taskCompletedHandler. 235 * @param taskId 236 * @return 237 */ 238 public LoggingScope taskCompleted(final String taskId) { 239 return new LoggingScopeImpl(LOG, logLevel, TASK_COMPLETE + ": " + taskId); 240 } 241 242 /** 243 * This is to measure the time in calling taskSuspendedHandler. 244 * @param taskId 245 * @return 246 */ 247 public LoggingScope taskSuspended(final String taskId) { 248 return new LoggingScopeImpl(LOG, logLevel, TASK_SUSPEND + ": " + taskId); 249 } 250 251 /** 252 * This is to measure the time in calling taskMessageReceivedHandler. 253 * @param msg 254 * @return 255 */ 256 public LoggingScope taskMessageReceived(final String msg) { 257 return new LoggingScopeImpl(LOG, logLevel, TASK_MESSAGE + ": " + msg); 258 } 259 260 /** 261 * This is to measure the time in calling contextMessageReceivedHandler. 262 * @param msg 263 * @return 264 */ 265 public LoggingScope contextMessageReceived(final String msg) { 266 return new LoggingScopeImpl(LOG, logLevel, CONTEXT_MESSAGE + ": " + msg); 267 } 268 269 /** 270 * This is to measure the time in calling driverRestartHandler. 271 * @param startTime 272 * @return 273 */ 274 public LoggingScope driverRestart(final StartTime startTime) { 275 return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART + ": " + startTime); 276 } 277 278 /** 279 * This is to measure the time in calling driverRestartCompletedHandler. 280 * @param timeStamp 281 * @return 282 */ 283 public LoggingScope driverRestartCompleted(final long timeStamp) { 284 return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_COMPLETE + ": " + timeStamp); 285 } 286 287 /** 288 * This is to measure the time in calling driverRestartRunningTaskHandler. 289 * @param taskId 290 * @return 291 */ 292 public LoggingScope driverRestartRunningTask(final String taskId) { 293 return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_RUNNING_TASK + ": " + taskId); 294 } 295 296 /** 297 * This is to measure the time in calling driverRestartActiveContextReceivedHandler. 298 * @param contextId 299 * @return 300 */ 301 public LoggingScope driverRestartActiveContextReceived(final String contextId) { 302 return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_ACTIVE_CONTEXT + ": " + contextId); 303 } 304 305 /** 306 * This is to measure the time in handling a http request. 307 * @param uri 308 * @return 309 */ 310 public LoggingScope httpRequest(final String uri) { 311 return new LoggingScopeImpl(LOG, logLevel, HTTP_REQUEST + ": " + uri); 312 } 313 314 /** 315 * This is to measure the time used to create HttpServer. 316 * @return 317 */ 318 public LoggingScope httpServer() { 319 return new LoggingScopeImpl(LOG, logLevel, HTTP_SERVER); 320 } 321 322 /** 323 * This is to measure the time to submit a driver. 324 * @param submitDriver 325 * @return 326 */ 327 public LoggingScope driverSubmit(final Boolean submitDriver) { 328 return new LoggingScopeImpl(LOG, logLevel, DRIVER_SUBMIT + ": " + submitDriver); 329 } 330 331 /** 332 * This is to measure the time to call Reef.Submit. 333 * @return 334 */ 335 public LoggingScope reefSubmit() { 336 return new LoggingScopeImpl(LOG, logLevel, REEF_SUBMIT); 337 } 338 339 /** 340 * This is to measure the time for a job submission. 341 * @return 342 */ 343 public LoggingScope localJobSubmission() { 344 return new LoggingScopeImpl(LOG, logLevel, LOCAL_JOB_SUBMIT); 345 } 346}