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.commons.lang3.time.StopWatch;
023import org.apache.reef.util.Optional;
024
025import java.util.logging.Level;
026import java.util.logging.Logger;
027
028/**
029 * Log time and duration for a scope.
030 */
031public class LoggingScopeImpl implements LoggingScope {
032  public static final String TOKEN = ":::";
033  public static final String START_PREFIX = "START" + TOKEN;
034  public static final String EXIT_PREFIX = "EXIT" + TOKEN;
035  public static final String DURATION = " Duration = ";
036
037  private final StopWatch stopWatch = new StopWatch();
038
039  private final Logger logger;
040
041  private final String msg;
042
043  private final Object[] params;
044
045  private final Optional<Object[]> optionalParams;
046
047  private final Level logLevel;
048
049  /**
050   * A constructor of ReefLoggingScope. It starts the timer and logs the msg
051   *
052   * @param logger
053   * @param msg
054   * @param params
055   */
056  LoggingScopeImpl(final Logger logger, final Level logLevel, final String msg, final Object[] params) {
057    this.logger = logger;
058    this.logLevel = logLevel;
059    this.msg = msg;
060    this.params = params;
061    stopWatch.start();
062    this.optionalParams = Optional.ofNullable(params);
063
064    if (logger.isLoggable(logLevel)) {
065      final StringBuilder sb = new StringBuilder();
066      log(sb.append(START_PREFIX).append(msg).toString());
067    }
068  }
069
070  /**
071   * A constructor of ReefLoggingScope.  It starts the timer and and logs the msg.
072   *
073   * @param logger
074   * @param msg
075   */
076  LoggingScopeImpl(final Logger logger, final Level logLevel, final String msg) {
077    this(logger, logLevel, msg, null);
078  }
079
080  /**
081   * The close() will be called when the object is to deleted. It stops the timer and logs the time elapsed.
082   */
083  @Override
084  public void close() {
085    stopWatch.stop();
086
087    if (logger.isLoggable(logLevel)) {
088      final StringBuilder sb = new StringBuilder();
089      log(sb.append(EXIT_PREFIX).append(msg).append(DURATION).append(stopWatch.getTime()).toString());
090    }
091  }
092
093  /**
094   * Log message.
095   * @param message
096   */
097  private void log(final String message) {
098    if (this.optionalParams.isPresent()) {
099      logger.log(logLevel, message, params);
100    } else {
101      logger.log(logLevel, message);
102    }
103  }
104}