⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.util;

import java.sql.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.text.*;

import org.compiere.*;
import org.compiere.util.*;
import org.compiere.db.*;
import org.apache.log4j.*;

/**
 *  Logging  Facilities.
 *  - The Methods returning void can be removed
 *
 *  @author     Jorg Janke
 *  @version    $Id: Log.java,v 1.3 2003/03/15 07:04:16 jjanke Exp $
 */
public final class Log implements Serializable
{
	/**	Error Level         */
	private static final int l0_Error = 0;
	/** User Level          */
	public static final int l1_User = 1;
	/** Sub program Level   */
	public static final int l2_Sub = 2;
	/** Utility Level       */
	public static final int l3_Util = 3;
	/** Data Level          */
	public static final int l4_Data = 4;
	/** Detail Data Level   */
	public static final int l5_DData = 5;
	/** Database Level      */
	public static final int l6_Database = 6;

	/** Used trace level    */
	private static int 			s_traceLevel = 0;
	/** Output device       */
	private static PrintWriter	s_out = null;
	/** Do we write to the console  */
	private static boolean      s_console = false;

	/**	Log4J						*/
	private static Logger		s_log = Logger.getLogger(Log.class);


	/**
	 *	Initiate log writing to System.out with the log level of the environment
	 *
	 *  @see org.compiere.util.Ini
	 */
	public static void initLog ()
	{
		initLog(false, -1);
	}   //  initLog

	/**
	 *	Initiate log writing to temp file or System.out
	 *
	 *  @param writeToTempLogFile   if true, a trace wile will be created in the local temp directory
	 *  @param traceLevel Set level from 0 (only errors) to 9 (everything) - if -1 the tracelevel of the user environment is used
	 *  @see org.compiere.util.Ini
	 */
	public static void initLog (boolean writeToTempLogFile, int traceLevel)
	{
	//	System.out.println("Log.initLog - ToTemp=" + writeToTempLogFile);
		//  TraceLevel
		int level = 0;
		try
		{
			if (traceLevel < 0)
				level = Integer.parseInt(Ini.getProperty(Ini.P_DEBUGLEVEL));
			else
				level = traceLevel;
			if (level < 0)
				level = 0;
		}
		catch (Exception e)
		{
			System.err.println("Log.initLog - Cannot parse Trace level - Set to 9");
			level = 9;
		}
		setTraceLevel(level);

		//  Write to Temp file or System.out
		if (Ini.isClient())
			setOutput(writeToTempLogFile);

		//	Database tracing
		if (level < l6_Database)
			return;
	//	traceJDBC(true);

		//	Print System Properties
		if (level < 10)
			return;
		printProperties (System.getProperties(), "System", writeToTempLogFile);
	}	//	initDebug

	/**
	 *  Print Properties
	 *
	 *  @param p Properties to print
	 *  @param description Description of properties
	 *  @param logLevel10 if true write to Log with level 10, else to System.out
	 */
	public static void printProperties (Properties p, String description, boolean logLevel10)
	{
		if (p == null)
			return;
		if (logLevel10)
			Log.trace(9, "Log.printProperties = " + description, "Size=" + p.size()
				+ ", Hash=" + p.hashCode() + "\n" + getLocalHost());
		else
			System.out.println("Log.printProperties = " + description + ", Size=" + p.size()
				+ ", Hash=" + p.hashCode() + "\n" + getLocalHost());

		Object[] pp = p.keySet().toArray();
		Arrays.sort(pp);
		for (int i = 0; i < pp.length; i++)
		{
			String key = pp[i].toString();
			String value = p.getProperty(key);
			if (logLevel10)
				Log.trace(10, key, value);
			else
				System.out.println("  " + key + " = " + value);
		}
	}   //  printProperties

	/**
	 *  Create System Info
	 *  @param sb Optional string buffer
	 *  @param ctx Environment
	 *  @return System Info
	 */
	public static StringBuffer getInfo (StringBuffer sb, Properties ctx)
	{
		if (sb == null)
			sb = new StringBuffer();
		if (ctx == null)
			ctx = Env.getCtx();
		//  Envoronment
		CConnection cc = CConnection.get();
		sb.append("\n\n===Environment===").append(Compiere.getCheckSum())
			.append(Compiere.getSummaryAscii())
			.append("\n").append(getLocalHost())
			.append("\n").append(cc.toStringLong())
			.append("\n").append(cc.getInfo());
		//  Context
		sb.append("\n\n===Context===");
		String[] context = Env.getEntireContext(ctx);
		Arrays.sort(context);
		for (int i = 0; i < context.length; i++)
			sb.append("\n").append(context[i]);
		//  System
		sb.append("\n\n===System===");
		Object[] pp = System.getProperties().keySet().toArray();
		Arrays.sort(pp);
		for (int i = 0; i < pp.length; i++)
		{
			String key = pp[i].toString();
			String value = System.getProperty(key);
			sb.append("\n").append(key).append("=").append(value);
		}
		return sb;
	}   //  getInfo

	/**
	 *  Get Localhost
	 *  @return local host
	 */
	private static String getLocalHost()
	{
		try
		{
			InetAddress id = InetAddress.getLocalHost();
			return id.toString();
		}
		catch (Exception e)
		{
			error("Log.getLocalHost", e);
		}
		return "-no local host info -";
	}   //  getLocalHost

	/*************************************************************************/

	/**
	 * Output a trace if the threshold level is high enough
	 *
	 * @param level Trace Level
	 * @param message Information
	 * @param addlInfo Additional Information
	 */
	public static void  trace (int level, String message, Object addlInfo)
	{
		if (level < 0 || level > s_traceLevel)
			return;
		StringBuffer sb = new StringBuffer (message);
		sb.append(" - ");
		if (addlInfo == null)
			sb.append("null");
		else
			sb.append(addlInfo);
		trace (level, sb.toString());
	}   //  trace

	/**
	 *	Output a formatted trace message "{0}, {1,number}, {2}" if the threshold level is high enough
	 *
	 * @param level Trace Lebel
	 * @param message   Information like "{0}, {1,number}, {2}"
	 * @param parameter addition info like new Object[] { }
	 */
	public static void trace (int level, String message, Object[] parameter)
	{
		if (level < 0 || level > s_traceLevel || message == null)
			return;
		//
		String msg = null;
		try
		{
			msg = MessageFormat.format(message, parameter);
		}
		catch (Exception e)
		{
			System.err.println("Log.trace - invalid message - " + e.getMessage());
			msg = message;
		}
		trace(level, msg);
	}	//	trace

	/**
	 * Trace to output if the threshold level is high enough
	 *
	 * @param level Trace Level
	 * @param msg Information
	 */
	public static void trace (int level, Object msg)
	{
		if (level < 0 || level > s_traceLevel || msg == null)
			return;
		if (!Ini.isClient())
		{
			if (level == l0_Error)
				s_log.error(msg);
			else if (level == l1_User || level == l2_Sub)
				s_log.info(msg);
			else
				s_log.debug(msg);
			return;
		}

		String message = msg.toString();
		if (message.length() == 0)
			return;
		//
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		String tstr = ts.toString() + "00";
		StringBuffer sb = new StringBuffer ();
		//	Error
		if (level == 0)
		{	//		   12:12:12.123
			sb.append("===========> ").append(message);
			if (Ini.isClient())
				Toolkit.getDefaultToolkit().beep();
		}
		//	Std Message
		else
		{
			sb.append(tstr.substring(11, 23));
			//	+ " [" + level + "]"
			sb.append("                          ".substring(0, level << 1)).append(message);
		}

		//  Make sure we always can print
		if (s_out == null)
			setOutput(false);

		//  Print it
		s_out.println(sb.toString());

		//  Additional Error Info
		if (level == l0_Error)
		{
			String errMsg = ts.toString() + " " + message;
			if (!s_console)
				System.err.println (sb.toString());
			//  write to db
			writeDBLog(".", errMsg);
			//  beep
			if (Ini.isClient())
				Toolkit.getDefaultToolkit().beep();
		}
	}	//	trace

	/**
	 *	Simple Info output to out for temporary test purposes
	 *
	 * @deprecated
	 * @param message Information
	 */
	public static void print (String message)
	{
		if (s_out != null)
			s_out.println(message);
	}	//	info

	/**
	 *	Signal Error
	 *
	 * @param description Information
	 * @return true (indicator that method cannot be deleted)
	 */
	public static boolean error (String description)
	{
		if (Ini.isClient())
			trace (l0_Error, description);
		else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -