?? dbloglistener.java
字號:
package log4plsql.backgroundProcess;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import oracle.jdbc.pool.OracleDataSource;
import oracle.xml.parser.v2.XMLNode;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.apache.log4j.spi.LoggingEvent;
/**
* <p>Title: DbLogListener</p>
* <p>Description: Loops on a given connection, reading messages from the Log4Plsql pipe on the other end.
* Default logging is by Log4Plsql section as the Logger name. Change the DbLogHelper class to do special Logger configuration.
* <p>
* <p>Copyright: Copyright (c) 2004 Sabrix, Inc. This material is confidential and may not be reproduced by any means.</p>
* <p>Company: Sabrix, Inc.</p>
* <p>Header: $Header: $</p>
* @author gregw
* @version $Version:$
*/
public class DbLogListener implements Runnable {
/**
* Default to 5 minute wait for the next pipe message before returning and starting the loop again.
* It can take up to this long for the class to see a change to the keepWatching flag and stop processing logs.
*/
public static final int DEFAULT_PIPE_TIMEOUT = 300;
/**
* Constant for Log4Plsql level value. maps to relevant Log4J Level constant.
*/
public static final Integer LEVEL_OFF = new Integer(10);
/**
* Constant for Log4Plsql level value. maps to relevant Log4J Level constant.
*/
public static final Integer LEVEL_FATAL = new Integer(20);
/**
* Constant for Log4Plsql level value. maps to relevant Log4J Level constant.
*/
public static final Integer LEVEL_ERROR = new Integer(30);
/**
* Constant for Log4Plsql level value. maps to relevant Log4J Level constant.
*/
public static final Integer LEVEL_WARN = new Integer(40);
/**
* Constant for Log4Plsql level value. maps to relevant Log4J Level constant.
*/
public static final Integer LEVEL_INFO = new Integer(50);
/**
* Constant for Log4Plsql level value. maps to relevant Log4J Level constant.
*/
public static final Integer LEVEL_DEBUG = new Integer(60);
/**
* Constant for Log4Plsql level value. maps to relevant Log4J Level constant.
*/
public static final Integer LEVEL_ALL = new Integer(70);
/**
* Mapped Domain Context value for Appender formatting
*/
public static final String MDC_ID = "LOG4PLSQL_ID";
/**
* Mapped Domain Context value for Appender formatting
*/
public static final String MDC_DATE = "LOG4PLSQL_DATE";
/**
* Mapped Domain Context value for Appender formatting
*/
public static final String MDC_USER = "LOG4PLSQL_USER";
/**
* Map linking Log4Plsql level values to Log4J level constant instances. Yes, this adds maintenance overhead,
* but really, how often will new log levels be defined? And querying for them dynamically needs a connection,
* which means it would have to be done per instance, which seems needlessly expensive. This can be changed if
* custom log levels are truely that desirable.
*/
private static final Map logLevels_ = new HashMap();
static {
logLevels_.put(LEVEL_OFF, Level.OFF);
logLevels_.put(LEVEL_FATAL, Level.FATAL);
logLevels_.put(LEVEL_ERROR, Level.ERROR);
logLevels_.put(LEVEL_WARN, Level.WARN);
logLevels_.put(LEVEL_INFO, Level.INFO);
logLevels_.put(LEVEL_DEBUG, Level.DEBUG);
logLevels_.put(LEVEL_ALL, Level.ALL);
}
/**
* Generate the proper statement to get log pipe messages. This statement can be executed repeatedly.
*
* @param conn the database connection to use
* @param timeout seconds to wait for a new message if no message is currently in the pipe.
* @return
* @throws SQLException
*/
private static CallableStatement getDbLogPipeReadStatement(Connection conn, int timeout, String pipeName) throws SQLException {
CallableStatement stmt = conn.prepareCall("BEGIN :1 := PLOG_PIPE.read_message(:2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13, :14); END;");
stmt.registerOutParameter(1, Types.INTEGER); // 1. return value
stmt.setInt(2, timeout); // 2. seconds to wait for the next message
stmt.registerOutParameter(3, Types.BIGINT); // 3. ID
stmt.registerOutParameter(4, Types.TIMESTAMP); // 4. Date
stmt.registerOutParameter(5, Types.INTEGER); // 5. hundredths of seconds for date
stmt.registerOutParameter(6, Types.INTEGER); // 6. level
stmt.registerOutParameter(7, Types.VARCHAR); // 7. section
stmt.registerOutParameter(8, Types.VARCHAR); // 8. user
stmt.registerOutParameter(9, Types.VARCHAR); // 9. log command
stmt.registerOutParameter(10, Types.VARCHAR); //10. message text
stmt.registerOutParameter(11, Types.VARCHAR); //11. MDC keys
stmt.registerOutParameter(12, Types.VARCHAR); //12. MDC values
stmt.registerOutParameter(13, Types.VARCHAR); //13. MDC separator
stmt.setString(14, pipeName); //14. Pipe name to read from.
return stmt;
}
private static Map dbLogListeners_ = new HashMap();
/**
* Gets the instance of DbLogListener for the given connection. If it does not exist, it is created with the default base logger and timeout.
* @param conn
* @return
* @throws SQLException
*/
public static DbLogListener getDbLogListener(Connection conn) throws SQLException {
return getDbLogListener(conn, null, DEFAULT_PIPE_TIMEOUT, null);
}
/**
* Gets the instance of DbLogListener for the given connection. If it does not exist, it is created with the default base logger and timeout.
* @param conn
* @param pipeName
* @return
* @throws SQLException
*/
public static DbLogListener getDbLogListener(Connection conn, String pipeName) throws SQLException {
return getDbLogListener(conn, null, DEFAULT_PIPE_TIMEOUT, pipeName);
}
/**
* Gets the instance of DbLogListener for the given connection. If it does not exist, it is created with the given base logger and the default timeout.
* @param conn
* @return
* @throws SQLException
*/
public static DbLogListener getDbLogListener(Connection conn, Logger rootLogger) throws SQLException {
return getDbLogListener(conn, rootLogger, DEFAULT_PIPE_TIMEOUT, null);
}
/**
* Gets the instance of DbLogListener for the given connection. If it does not exist, it is created with the given base logger and the default timeout.
* @param conn
* @param pipeName
` * @return
* @throws SQLException
*/
public static DbLogListener getDbLogListener(Connection conn, Logger rootLogger, String pipeName) throws SQLException {
return getDbLogListener(conn, rootLogger, DEFAULT_PIPE_TIMEOUT, pipeName);
}
/**
* Create or retrieve the database log pipe listener for the given connection. Assumes the schema on the other
* end of the connection contains Log4Plsql package PLOG_PIPE to read from a given pipe.
* <p>
* Only one instance can exist at a time per connection/username/pipe combination, so all messages are sure to go to the same Logger.
* If public pipes are being used, there is still a possibility of duplicate DbLogListener instances listening to the same pipe from
* different user connections.
* <p>
* Messages will be logged using Logger instances decended from the given named Logger,
* which is assumed to already be configured. If rootLoggerName is null, the default is assumed
* to be the Root Logger.
* <p>
*/
public static DbLogListener getDbLogListener(Connection conn, Logger rootLogger, int pipeTimeout, String pipeName) throws SQLException {
DatabaseMetaData metaData = conn.getMetaData();
String key = metaData.getURL() + "-" + metaData.getUserName() + "-" + pipeName;
DbLogListener listener = null;
synchronized (dbLogListeners_) {
listener = (DbLogListener) dbLogListeners_.get(key);
if (listener == null) {
listener = new DbLogListener(conn, key, rootLogger, pipeTimeout, pipeName);
dbLogListeners_.put(key, listener);
}
}
return listener;
}
public static DbLogListener getDbLogListener(Connection conn, Logger rootLogger, int pipeTimeout) throws SQLException {
return getDbLogListener(conn, rootLogger, pipeTimeout, null);
}
/**
* Removes a DBLogListener from the map based on it's connection URL/username key string.
* Stops the listener from listening, if it hasn't already stopped, and removes it from the map.
* Call this to clean up the map. Otherwise, the same listeners will be reused, just stopped and started
* multiple times, if desired.
* @param key
*/
public static void removeDbLogListener(String key) {
synchronized (dbLogListeners_) {
DbLogListener listener = (DbLogListener) dbLogListeners_.get(key);
listener.setStillWatching(false);
dbLogListeners_.remove(key);
}
}
/**
* Connection to use for reading the Oracle pipe.
*/
private Connection conn_;
/**
* Base Logger name string to append the current context to. If null, then the root logger is assumed to be the parent
* Logger for all Log4Plsql messages.
*/
private String rootLoggerName_ = null;
/**
* Flag to stop looping for messages.
*/
private boolean stillWatching_ = true;
/**
* Object to lock on to make changing the value of stillWatching_ thread safe.
*/
private Object stillWatchingLock_ = new Object();
private String mapKey_;
private boolean isRunning_ = false;
private Object isRunningLock_ = new Object();
private CallableStatement stmt_;
/**
* Name of Oracle Pipe to read from. If null, use the Log4Plsql default pipe name found in the PLOGPARAM package.
*/
private String pipeName_;
/**
* Sequential identifier of the current log message - 0 = no current message.
*/
private long id_;
/**
* Date stamp of the current log message. Granularity to hundredths of seconds only.
*/
private Date date_;
/**
* Log message level - current value is looked up in the DB log level map.
*/
private Level level_;
/**
* Log4Plsql section for the current message, composed of period delimited strings.
* This commonly maps to a Logger name. The default implementation appends this to any
* root logger name given in the factory constructor to define the Logger name to use.
*/
private String section_;
/**
* Database user the current log message comes from. May or may not be useful depending on the app architecture.
*/
private String user_;
/**
* Text of the current log message.
*/
private String message_;
/**
* Current Log command from the pipe. Currently does nothing, as there is only one command.
*/
private String command_;
/**
* MDC keys for the current message.
*/
private List mdcKeys_ = new ArrayList();
/**
* MDC values for the current message.
*/
private List mdcValues_ = new ArrayList();
/**
* MDC Seprarator for the current message.
*/
private String mdcSeparator_;
/**
* Class that provides logic to decide what Logger instance to use for a given message statement, assuming multiple
* logical loggers use the same database pipe. The default implementation uses the message section appended to the
* base logger name given in the factory (constructor) call as the logger name.
*/
private DbLogHelper dbLogHelper_ = new DbLogHelper(this);
/**
* Create a new listener ready to read messages from the given connection. Called by factory methods and unit tests.
* <p/>
*
* @param conn Connection to read pipe from
* @param key map key for this object. From factory method, and used by remove method.
* @param rootLogger base Logger pipe message loggers. Can be null.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -