Java Log to File log(String message)

Here you can find the source of log(String message)

Description

log

License

Open Source License

Declaration

public static void log(String message) 

Method Source Code

//package com.java2s;
/***************************************************************************
 * Copyright (c) 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /*from   w w  w  .  j  a  va  2  s  .  co m*/
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;

import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void log(String message) {
        log(message, null);
    }

    public static void log(String message, Throwable t) {
        PrintWriter writer = null;

        try {
            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream("log.txt", true)));
            writer.println(new SimpleDateFormat().format(new Date()) + " " + message);

            if (t != null) {
                t.printStackTrace(writer);
            }
        } catch (Exception ignore) {
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception ignore) {
                }
            }
        }
    }

    /**
     * No exception <code>InputStream</code> close method.
     */
    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>OutputStream</code> close method.
     */
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>java.io.Reader</code> close method.
     */
    public static void close(Reader rd) {
        if (rd != null) {
            try {
                rd.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>java.io.Writer</code> close method.
     */
    public static void close(Writer wr) {
        if (wr != null) {
            try {
                wr.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }
}

Related

  1. log(Level level, String methodName, Object message)
  2. log(String message)
  3. log(String message)
  4. log(String message, Exception e)
  5. log(String method, String message)