📄 log.java
字号:
s_log.error(description);
return true;
} // error
/**
* Signal Error
*
* @param description Information
* @param e Exception
* @return true (indicator that method cannot be deleted)
*/
public static boolean error (String description, Exception e)
{
if (!Ini.isClient())
{
s_log.error (description, e);
return true;
}
StringBuffer output = new StringBuffer();
output.append(description).append(" - ");
output.append(e.getClass().getName()).append(": ");
if (e.getMessage() == null)
output.append(e.toString());
else
output.append(e.getMessage());
if (!(e instanceof SQLException))
{
StackTraceElement[] elements = e.getStackTrace();
for (int i = 0; i < 3 || i < elements.length; i++)
output.append(" > ").append(elements[i].toString());
}
trace (l0_Error, output.toString());
e.printStackTrace(s_out);
return true;
} // error
/**
* Signal Error
*
* @param description Information
* @param sqlEx Exception
* @return true (indicator that method cannot be deleted)
*/
public static boolean error(String description, SQLException sqlEx)
{
if (description == null || sqlEx == null || description.length() == 0)
throw new IllegalArgumentException("DB.printException - required parameter missing");
if (!Ini.isClient())
{
s_log.error(description, sqlEx);
return true;
}
error("SQL Exception: " + description); // initial log
if (sqlEx == null)
return true;
//
SQLException ex = sqlEx;
while (ex != null)
{
StringBuffer buffer = new StringBuffer();
buffer.append(ex.getMessage());
buffer.append("; State=").append(ex.getSQLState()).append("; ErrorCode=").append(ex.getErrorCode());
error(buffer.toString()); // additional log infos
ex = ex.getNextException();
}
return true;
} // error
/*************************************************************************/
/**
* Trace JDBC
*
* @param traceDB if true, enable DriverManaer output
*/
public static void traceJDBC (boolean traceDB)
{
System.out.println("Log.traceJDBC = " + traceDB);
if (traceDB && getTraceLevel() >= l6_Database)
DriverManager.setLogWriter(s_out); // lists Statements
else
DriverManager.setLogWriter(null);
} // traceJDBC
/**
* Set Trace Level
*
* @param traceLevel new TraceLevel
*/
public static void setTraceLevel(int traceLevel)
{
if (traceLevel == s_traceLevel)
return;
s_traceLevel = traceLevel;
Ini.setProperty(Ini.P_DEBUGLEVEL, s_traceLevel);
// Init Log4J
if (Ini.isClient())
{
if (traceLevel == l0_Error)
s_log.getRootLogger().setLevel(Level.ERROR);
else if (traceLevel == l1_User || traceLevel == l2_Sub)
s_log.getRootLogger().setLevel(Level.INFO);
else
s_log.getRootLogger().setLevel(Level.ALL);
}
s_log.info("setTraceLevel - " + traceLevel);
} // setTraceLevel
/**
* Return Trace Level
* @return Trace Level
*/
public static int getTraceLevel()
{
return s_traceLevel;
} // getTraceLevel
/**
* Is it at least Trace Level
* @param level test level
* @return true if level less or equal tarce level
*/
public static boolean isTraceLevel(int level)
{
return level <= s_traceLevel;
} // getTraceLevel
/*************************************************************************/
/**
* Set Output
* @param out PrintWriter for logs
*/
public static void setOutput(PrintWriter out)
{
if (out == null)
throw new IllegalArgumentException("Log.setOutput - PrintWriter cannot be null");
// Close old
if (s_out != null)
{
s_out.flush();
s_out.close();
}
// Set new
s_out = out;
s_console = false;
if (getTraceLevel() > 0)
System.out.println("Log.setOutput - PW=" + s_out.toString());
} // setOutput
/**
* Set Output to file
* @param filename FileName for log output
*/
public static void setOutput(String filename)
{
if (filename == null || filename.length() == 0)
throw new IllegalArgumentException("Log.setOutput - Invalid filename");
// Close old
if (s_out != null)
{
s_out.flush();
s_out.close();
}
// Create new
try
{
Time t = new Time(System.currentTimeMillis());
String logName = filename + t;
FileWriter fw = new FileWriter(logName);
s_out = new PrintWriter(fw, true);
s_console = false;
if (getTraceLevel() > 0)
System.out.println("Log.setOutput - File=" + logName);
}
catch (Exception e)
{
System.err.println("========> Log.setOutput - " + e.getMessage());
s_out = new PrintWriter(System.out, true);
s_console = true;
if (getTraceLevel() > 0)
System.out.println("Log.setOutput - C=" + s_out.toString());
}
} // setOutput
/**
* Write Logfile in temporary directory
* @param writeToTempLogFile if true temporary file is used for log, otherwise System.out
*/
public static void setOutput (boolean writeToTempLogFile)
{
// Close old
if (s_out != null)
{
s_out.flush();
s_out.close();
}
try
{
if (writeToTempLogFile)
{
String dirName = Ini.getProperty(Ini.P_TEMP_DIR);
File tempDir = new File(dirName);
File tempFile = File.createTempFile("Compiere", ".log", tempDir);
s_out = new PrintWriter(new FileOutputStream(tempFile), true); // autoFlush
s_console = false;
if (getTraceLevel() > 0)
System.out.println("Log.setOutput - File=" + tempFile.getAbsolutePath());
}
else
{
s_out = new PrintWriter(System.out, true);
s_console = true;
// if (getTraceLevel() > 0)
// System.out.println("Log.setOutput - C=" + s_out.toString());
}
}
catch (IOException e)
{
System.err.println("========> Log.setOutput - " + e.getMessage());
s_out = new PrintWriter(System.out, true);
s_console = true;
// System.out.println("Log.setOutput - C=" + s_out.toString());
}
} // setOutput
/*************************************************************************/
/** Last Error Message */
private static ValueNamePair s_lastError = null;
/**
* Set and issue Error and save as ValueNamePair
* @param AD_Message message key
* @param message clear text message
* @return true (to avoid removal of method)
*/
public static boolean saveError (String AD_Message, String message)
{
return saveError (AD_Message, message, true);
} // setError
/**
* Set Error and save as ValueNamePair
* @param AD_Message message key
* @param message clear text message
* @param issueError print error message (default true)
* @return true (to avoid removal of method)
*/
public static boolean saveError (String AD_Message, String message, boolean issueError)
{
s_lastError = new ValueNamePair (AD_Message, message);
// print it
if (issueError)
error(AD_Message + " - " + message);
return true;
} // setError
/**
* Get Error from Stack
* @return AD_Message as Value and Message as String
*/
public static ValueNamePair retrieveError()
{
ValueNamePair vp = s_lastError;
s_lastError = null;
return vp;
} // getError
/*************************************************************************/
/**
* Write to Database
* @param summary name/title
* @param msg Database Message
*/
protected static void writeDBLog (String summary, String msg)
{
// Nothing there
if (msg == null || msg.length() == 0)
return;
// Is there a DB connection?
if (!DB.isConnected())
return;
// Don't store Error messages about AD_Error
if (msg.indexOf("AD_Error") != -1)
return;
// Get Context
Properties ctx = Env.getCtx();
//
StringBuffer sql = new StringBuffer();
try
{
sql.append("INSERT INTO AD_Error "
+ "(AD_Error_ID, AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy,"
+ "Name, Code, AD_Language) VALUES (");
sql.append("AD_Error_Seq.nextval,"); // AD_Error_ID
sql.append(Env.getContextAsInt(ctx, 0, "AD_Client_ID")).append(",");// AD_Client_ID
sql.append(Env.getContextAsInt(ctx, 0, "AD_Org_ID")).append(","); // AD_Org_ID
sql.append(Env.getContextAsInt(ctx, "#AD_User_ID")).append(","); // CreatedBy
sql.append(Env.getContextAsInt(ctx, "#AD_User_ID")).append(","); // UpdatedBy
sql.append(DB.TO_STRING(summary)).append(","); // Name
sql.append(DB.TO_STRING(msg, 2000)).append(","); // Code
sql.append("'").append(Env.getAD_Language(ctx)).append("')"); // AD_Language
String sqlExec = DB.getDatabase().convertStatement(sql.toString());
// Get standard RW connection
Connection con = DB.getConnectionRW();
if (con == null)
{
System.err.println("===========> Log.writeDB - No Connection - Error not logged in DB -");
System.err.println(msg);
return;
}
Statement stmt = con.createStatement();
stmt.executeUpdate(sqlExec);
stmt.close(); // cursor not closed, if statement fails
}
catch (Exception e)
{
System.err.println("===========> Log.writeDB - Message=" + msg + " - Error: " + e);
}
} // writeDB
} // Debug
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -