Java Log to File logErrPrint(String s)

Here you can find the source of logErrPrint(String s)

Description

logPrintErr() prints the String s to System.err with a timestamp indicating the hour and minute it was printed.

License

Open Source License

Parameter

Parameter Description
s String to be printed.

Declaration

public static void logErrPrint(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.logging.*;
import java.text.NumberFormat;

public class Main {
    static Calendar cal = Calendar.getInstance();
    private static Logger logger;
    private static NumberFormat formatFloat, formatInt;

    /**//from   w w w. j av a  2s  .com
     * logPrintErr() prints the String s to System.err with a timestamp indicating the hour and minute
     * it was printed.
     * @param s String to be printed.
     */
    public static void logErrPrint(String s) {
        cal.setTimeInMillis(System.currentTimeMillis());
        System.err.println(nf(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
                + nf(cal.get(Calendar.MINUTE), 2) + "|  " + s);

        if (logger != null)
            logger.warning(s);
    }

    /**
     * Format floating point number for printing
     * 
     * @param num
     *          Number to format
     * @param lead
     *          Minimum number of leading digits
     * @param digits
     *          Number of decimal digits to show
     * @return Formatted number string
     */
    static public String nf(float num, int lead, int decimal) {
        if (formatFloat == null)
            nfInitFormats();
        formatFloat.setMinimumIntegerDigits(lead);
        formatFloat.setMaximumFractionDigits(decimal);
        formatFloat.setMinimumFractionDigits(decimal);

        return formatFloat.format(num).replace(",", ".");
    }

    static public String nf(double num, int lead, int decimal) {
        return nf((float) num, lead, decimal);
    }

    /**
     * Format floating point number for printing with maximum 3 decimal points.
     * 
     * @param num
     *          Number to format
     * @return Formatted number string
     */
    static public String nf(float num) {
        if (formatFloat == null)
            nfInitFormats();
        formatFloat.setMinimumIntegerDigits(1);
        formatFloat.setMaximumFractionDigits(3);

        return formatFloat.format(num).replace(",", ".");
    }

    static public String nf(double num) {
        return nf((float) num);
    }

    /**
     * Format integer number for printing, padding with zeros if number has fewer
     * digits than desired.
     * 
     * @param num
     *          Number to format
     * @param digits
     *          Minimum number of digits to show
     * @return Formatted number string
     */
    static public String nf(int num, int digits) {
        if (formatInt == null)
            nfInitFormats();
        formatInt.setMinimumIntegerDigits(digits);
        return formatInt.format(num);
    }

    static public void nfInitFormats() {
        formatFloat = NumberFormat.getInstance();
        formatFloat.setGroupingUsed(false);

        formatInt = NumberFormat.getInstance();
        formatInt.setGroupingUsed(false);
    }
}

Related

  1. log(String name, String message)
  2. log(String tag, T msg)
  3. logError(ILog log, String message)
  4. logError(String message)
  5. logError(String msg)
  6. logger(String s)
  7. loggerOld(String s)
  8. logInfo(ILog log, String message)
  9. logRecordToString(LogRecord record)