use of org.apache.log4j.jdbcplus.JDBCLogColumn in project jaffa-framework by jaffa-projects.
the class JDBCLoggerWithAttachment method append.
/**
* Invokes the append method of the base class.
* Additionally it'll create an attachment, if provided in the MDC.
* @param event the LoggingEvent to log
* @param layout layout to use for message
* @exception Exception Description of Exception
*/
public void append(LoggingEvent event, Layout layout) throws Exception {
// If attachment is not provided, simply invoke the append() method of the base class and return
if (event.getMDC(m_attachmentMDCKey) == null) {
super.append(event, layout);
return;
}
// The following is a copy of the original method, but with the ability to obtain the auto-generated key.
if (!ready())
throw new Exception("JDBCLogger::append(), Not ready to append !");
boolean errorOccurred = false;
PreparedStatement prepStmt = null;
try {
prepareConnectionForAttachment();
int paramIndex = 1;
prepStmt = (PreparedStatement) c_stmt.get(this);
String logId = null;
for (int i = 0; i < ((Number) c_num.get(this)).intValue(); i++) {
JDBCLogColumn logcol = (JDBCLogColumn) ((List) c_logcols.get(this)).get(i);
if (((Boolean) c_ignore.get(logcol)).booleanValue())
continue;
int logtype = ((Number) c_logtype.get(logcol)).intValue();
int sqlType = ((Number) c_sqlType.get(logcol)).intValue();
Object value = c_value.get(logcol);
Object parameter = null;
if (logtype == JDBCLogType.MSG) {
parameter = event.getRenderedMessage();
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.LAYOUT) {
parameter = layout.format(event);
// default: use first pattern
int layoutIndex = ((Integer) value).intValue();
// LAYOUT~x. use tokens in layout string
List tokenList = getTokenList((String) parameter);
String token = getTokenFromList(tokenList, layoutIndex);
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setString(paramIndex, token);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.PRIO) {
parameter = event.getLevel().toString();
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.CAT) {
parameter = event.getLoggerName();
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.THREAD) {
parameter = event.getThreadName();
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.ID) {
parameter = ((JDBCIDHandler) c_idhandler.get(logcol)).getID();
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.STATIC) {
parameter = value;
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.TIMESTAMP) {
parameter = new Timestamp((new java.util.Date()).getTime());
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.INC) {
parameter = ((Number) c_inc.get(this));
c_inc.set(this, ((Number) parameter).longValue() + 1);
prepStmt.setObject(paramIndex, parameter);
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.DYNAMIC) {
parameter = ((JDBCColumnHandler) c_columnHandler.get(logcol)).getObject(event, (String) c_table.get(this), (String) c_name.get(logcol));
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.THROWABLE) {
// extract throwable information from loggingEvent if available
parameter = (String) c_quotedString.invoke(this, getThrowableRepresentationFromLoggingEvent(event));
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.NDC) {
parameter = event.getNDC();
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.MDC) {
Object mdcObject = event.getMDC(value.toString());
parameter = mdcObject == null ? null : mdcObject.toString();
if (parameter == null) {
prepStmt.setNull(paramIndex, sqlType);
} else {
prepStmt.setObject(paramIndex, parameter);
}
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.IPRIO) {
parameter = event.getLevel().toInt();
prepStmt.setObject(paramIndex, parameter);
paramIndex = paramIndex + 1;
} else if (logtype == JDBCLogType.ORACLE_SEQUENCE) {
// do nothing
}
// Obtain the logId
if (parameter != null && "log_id".equalsIgnoreCase((String) c_name.get(logcol)))
logId = parameter.toString();
}
prepStmt.executeUpdate();
// Obtain the auto-generated key
if (logId == null)
throw new Exception("JDBCLogger::append(), LogId was not determined for the Log. Cannot add the attachment");
prepStmt.close();
// Now create the attachment
Object attachment = event.getMDC(m_attachmentMDCKey);
prepStmt = m_stmtForAttachment;
IVoucherGenerator vg = VoucherGeneratorFactory.instance();
vg.setConnection((Connection) c_con.get(this));
vg.setDomainClassName(AttachmentMeta.getName());
vg.setFieldName(AttachmentMeta.ATTACHMENT_ID);
vg.setLabelToken(AttachmentMeta.META_ATTACHMENT_ID.getLabelToken());
prepStmt.setString(1, vg.generate());
prepStmt.setString(2, "org.jaffa.modules.messaging.domain.BusinessEventLog;" + logId);
prepStmt.setString(3, AttachmentService.createAttachmentName(attachment));
prepStmt.setString(4, "E");
prepStmt.setString(5, AttachmentService.createContentType(attachment));
prepStmt.setTimestamp(6, new Timestamp((new java.util.Date()).getTime()));
if (SecurityManager.getPrincipal() != null && SecurityManager.getPrincipal().getName() != null)
prepStmt.setString(7, SecurityManager.getPrincipal().getName());
else
prepStmt.setNull(7, Types.VARCHAR);
byte[] bytes = AttachmentService.createAttachmentData(attachment);
if ("oracle".equalsIgnoreCase(m_engine)) {
Blob blob = (Blob) c_createBlob.invoke(null, c_con.get(this), bytes);
prepStmt.setBlob(8, blob);
} else {
InputStream stream = new BufferedInputStream(new ByteArrayInputStream(bytes));
prepStmt.setBinaryStream(8, stream, bytes.length);
}
prepStmt.executeUpdate();
} catch (Exception e) {
// e.printStackTrace();
errorOccurred = true;
throw (e);
} finally {
try {
if (prepStmt != null) {
prepStmt.close();
}
freeConnection();
} catch (Exception exception) {
if (errorOccurred) {
// consume exception
} else {
throw (exception);
}
}
}
}
use of org.apache.log4j.jdbcplus.JDBCLogColumn in project jaffa-framework by jaffa-projects.
the class JDBCLoggerWithAttachment method createStatementsForAttachment.
/**
* This method is similar to the createStatement() method of the base class.
* It creates a PreparedStatement that returns the auto-generated key.
* It also creates a statement for the attachment.
* @exception Exception Description of Exception
*/
protected void createStatementsForAttachment() throws Exception {
// Create a statement for logging an event
if (c_preparedSql.get(this) == null) {
StringBuilder sql = new StringBuilder("insert into ").append(c_table.get(this)).append(" (").append(c_column_list.get(this)).append(") values (");
boolean addedColumn = false;
for (int i = 0; i < ((Number) c_num.get(this)).intValue(); i++) {
JDBCLogColumn logcol = (JDBCLogColumn) ((List) c_logcols.get(this)).get(i);
// only required columns
if (!((Boolean) c_ignore.get(logcol)).booleanValue()) {
if (addedColumn)
sql.append(", ");
if (((Number) c_logtype.get(logcol)).intValue() == JDBCLogType.ORACLE_SEQUENCE) {
sql.append(c_value.get(logcol).toString()).append(".NEXTVAL");
} else {
sql.append('?');
}
addedColumn = true;
}
}
sql.append(')');
LogLog.debug("JDBCLoggerWithAttachment::createStatementsForAttachment(), prepared statement: " + sql.toString());
c_preparedSql.set(this, sql.toString());
}
Statement stmt = ((Connection) c_con.get(this)).prepareStatement((String) c_preparedSql.get(this));
c_stmt.set(this, stmt);
// Create a statement for the attachment
if (m_preparedSqlForAttachment == null) {
StringBuilder sqlForAttachment = new StringBuilder("insert into ").append(m_attachmentTable).append("(ATTACHMENT_ID, SERIALIZED_KEY, ORIGINAL_FILE_NAME, ATTACHMENT_TYPE, CONTENT_TYPE, CREATED_ON, CREATED_BY, DATA)").append(" values(?, ?, ?, ?, ?, ?, ?, ?)");
LogLog.debug("JDBCLoggerWithAttachment::createStatementsForAttachment(), prepared statement for attachment: " + sqlForAttachment.toString());
m_preparedSqlForAttachment = sqlForAttachment.toString();
}
m_stmtForAttachment = ((Connection) c_con.get(this)).prepareStatement(m_preparedSqlForAttachment);
}
Aggregations