Java Long Number to Time logDuration(Logger logger, Level level, long startTime, String message)

Here you can find the source of logDuration(Logger logger, Level level, long startTime, String message)

Description

log Duration

License

Open Source License

Declaration

public static void logDuration(Logger logger, Level level, long startTime, String message) 

Method Source Code


//package com.java2s;
/*/* w w  w .  j a  va2s  . c  o m*/
 * Copyright (c) 2008-2016, GigaSpaces Technologies, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class Main {
    private static final MessageFormat messageFormat = new MessageFormat("{0,date,yyyy-MM-dd HH:mm:ss,SSS}");

    public static void logDuration(Logger logger, Level level, long startTime, String message) {
        final long duration = System.currentTimeMillis() - startTime;
        logger.log(level, message + " [Duration = " + duration + "ms]");
    }

    /**
     * log the message under the logName and level. if logger is <code>null</code> tries to log to
     * system.out/err
     *
     * @return <code>true</code> if logged to log or system.out/err, <code>false</code> otherwise.
     */
    public static boolean log(String logName, Level level, String message, Throwable thrown) {
        LogManager logManager = LogManager.getLogManager();
        Logger logger = logManager.getLogger(logName);
        if (logger != null && logger.isLoggable(level)) {
            logger.log(level, message, thrown);
            return true;
        }
        String key = logName + ".level";
        String levelOverride = System.getProperty(key, logManager.getProperty(key));
        if (levelOverride != null) {
            Level levelParsed = Level.parse(levelOverride);
            //isLoggable?
            if (level.intValue() < levelParsed.intValue() || level.equals(Level.OFF)) {
                return false;
            } else {
                println(logName, level, message, thrown);
            }
        }
        return false;
    }

    public static void println(String logName, Level level, String message) {
        println(logName, level, message, null);
    }

    public static void println(String logName, Level level, String message, Throwable t) {
        String dateTimeAsString = messageFormat.format(new Object[] { new Date() });
        String stackTrace = "";
        if (t != null) {
            stackTrace = "; Caught: " + t + "\n" + getStackTrace(t);
        }
        String msg = dateTimeAsString + " " + level + " [" + logName + "] - " + message + stackTrace;

        if (Level.WARNING.equals(level) || Level.SEVERE.equals(level)) {
            System.err.println(msg);
        } else {
            System.out.println(msg);
        }
    }

    private static String getStackTrace(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        pw.flush();

        return sw.toString();
    }
}

Related

  1. getTimeString(long nanoTime)
  2. getTimeString(long time)
  3. getTimeTagged(long time, boolean round)
  4. getTimeText(Long oldTime)
  5. getUploadPath(String fileName, long time)
  6. long2Str(long time)
  7. longToPgnDate(long time)
  8. longToTimedDate(long time)
  9. prettyDate(long time)