001: // $Id: Log.java 12 2007-08-29 05:23:13Z jcamaia $
002:
003: package net.sf.persist;
004:
005: import org.apache.log4j.Logger;
006:
007: /**
008: * Wraps log4j using reflection to avoid runtime dependencies.
009: */
010: public final class Log {
011:
012: // avoid instantiation
013: private Log() {
014: // do nothing
015: }
016:
017: public static final String ENGINE = "persist.engine";
018: public static final String PROFILING = "persist.profiling";
019: public static final String RESULTS = "persist.results";
020: public static final String PARAMETERS = "persist.parameters";
021:
022: private static boolean log4jAvailable = false;
023: static {
024: try {
025: Class.forName("org.apache.log4j.Logger");
026: log4jAvailable = true;
027: } catch (ClassNotFoundException e) {
028: log4jAvailable = false;
029: }
030: }
031:
032: public static void trace(final String name, final Object message) {
033: if (log4jAvailable) {
034: Logger.getLogger(name).trace(message);
035: }
036: }
037:
038: public static boolean isTraceEnabled(final String name) {
039: return log4jAvailable
040: && Logger.getLogger(name).isTraceEnabled();
041: }
042:
043: public static void debug(final String name, final Object message) {
044: if (log4jAvailable) {
045: Logger.getLogger(name).debug(message);
046: }
047: }
048:
049: public static boolean isDebugEnabled(final String name) {
050: return log4jAvailable
051: && Logger.getLogger(name).isDebugEnabled();
052: }
053:
054: public static void info(final String name, final Object message) {
055: if (log4jAvailable) {
056: Logger.getLogger(name).info(message);
057: }
058: }
059:
060: public static boolean isInfoEnabled(final String name) {
061: return log4jAvailable && Logger.getLogger(name).isInfoEnabled();
062: }
063:
064: public static void error(final String name, final Object message) {
065: if (log4jAvailable) {
066: Logger.getLogger(name).error(message);
067: }
068: }
069:
070: /**
071: * Converts types expected in prepared statement parameters to a suitable
072: * string to be added to log output.
073: */
074: public static String objectToString(final Object obj) {
075:
076: String str = null;
077:
078: if (obj instanceof String) {
079: str = (String) obj;
080: } else if (obj instanceof byte[]) {
081: str = java.util.Arrays.toString((byte[]) obj);
082: str = str.substring(1, str.length() - 1);
083: } else if (obj instanceof Byte[]) {
084: str = java.util.Arrays.toString((Byte[]) obj);
085: str = str.substring(1, str.length() - 1);
086: } else if (obj instanceof char[]) {
087: str = java.util.Arrays.toString((char[]) obj);
088: str = str.substring(1, str.length() - 1);
089: } else if (obj instanceof Character[]) {
090: str = java.util.Arrays.toString((Character[]) obj);
091: str = str.substring(1, str.length() - 1);
092: } else {
093: str = obj == null ? "null" : obj.toString();
094: }
095:
096: return (str.length() > 64) ? str.substring(0, 64) + "..." : str;
097: }
098:
099: public static String sqlTypeToString(final int type) {
100:
101: final String ret;
102:
103: if (type == java.sql.Types.ARRAY) {
104: ret = "ARRAY";
105: } else if (type == java.sql.Types.BIGINT) {
106: ret = "BIGINT";
107: } else if (type == java.sql.Types.BINARY) {
108: ret = "BINARY";
109: } else if (type == java.sql.Types.BIT) {
110: ret = "BIT";
111: } else if (type == java.sql.Types.BLOB) {
112: ret = "BLOB";
113: } else if (type == java.sql.Types.BOOLEAN) {
114: ret = "BOOLEAN";
115: } else if (type == java.sql.Types.CHAR) {
116: ret = "CHAR";
117: } else if (type == java.sql.Types.CLOB) {
118: ret = "CLOB";
119: } else if (type == java.sql.Types.DATALINK) {
120: ret = "DATALINK";
121: } else if (type == java.sql.Types.DATE) {
122: ret = "DATE";
123: } else if (type == java.sql.Types.DECIMAL) {
124: ret = "DECIMAL";
125: } else if (type == java.sql.Types.DISTINCT) {
126: ret = "DISTINCT";
127: } else if (type == java.sql.Types.DOUBLE) {
128: ret = "DOUBLE";
129: } else if (type == java.sql.Types.FLOAT) {
130: ret = "FLOAT";
131: } else if (type == java.sql.Types.INTEGER) {
132: ret = "INTEGER";
133: } else if (type == java.sql.Types.JAVA_OBJECT) {
134: ret = "JAVA_OBJECT";
135: } else if (type == java.sql.Types.LONGVARBINARY) {
136: ret = "LONGVARBINARY";
137: } else if (type == java.sql.Types.LONGVARCHAR) {
138: ret = "LONGVARCHAR";
139: } else if (type == java.sql.Types.NULL) {
140: ret = "NULL";
141: } else if (type == java.sql.Types.NUMERIC) {
142: ret = "NUMERIC";
143: } else if (type == java.sql.Types.OTHER) {
144: ret = "OTHER";
145: } else if (type == java.sql.Types.REAL) {
146: ret = "REAL";
147: } else if (type == java.sql.Types.REF) {
148: ret = "REF";
149: } else if (type == java.sql.Types.SMALLINT) {
150: ret = "SMALLINT";
151: } else if (type == java.sql.Types.STRUCT) {
152: ret = "STRUCT";
153: } else if (type == java.sql.Types.TIME) {
154: ret = "TIME";
155: } else if (type == java.sql.Types.TIMESTAMP) {
156: ret = "TIMESTAMP";
157: } else if (type == java.sql.Types.TINYINT) {
158: ret = "TINYINT";
159: } else if (type == java.sql.Types.VARBINARY) {
160: ret = "VARBINARY";
161: } else if (type == java.sql.Types.VARCHAR) {
162: ret = "VARCHAR";
163: } else {
164: ret = "" + type;
165: }
166:
167: return ret;
168:
169: }
170:
171: }
|