1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| import ch.qos.logback.classic.Level; import ch.qos.logback.classic.db.DBAppender; import ch.qos.logback.classic.spi.ILoggingEvent; import dm.jdbc.util.ReflectUtil; import org.springframework.stereotype.Component;
import javax.persistence.Column; import javax.persistence.Table; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.HashMap; import java.util.Map;
@Component public class DiyDBAppender extends DBAppender {
private ILoggingEvent iLoggingEvent;
Map<String, String> mergePropertyMaps(ILoggingEvent event) { Map<String, String> mergedMap = new HashMap(); Map<String, String> loggerContextMap = event.getLoggerContextVO().getPropertyMap(); Map<String, String> mdcMap = event.getMDCPropertyMap(); if (loggerContextMap != null) { mergedMap.putAll(loggerContextMap); }
if (mdcMap != null) { mergedMap.putAll(mdcMap); }
return mergedMap; }
protected void secondarySubAppend(ILoggingEvent event, Connection connection, long eventId) throws Throwable { Map mergedMap = mergePropertyMaps(event); iLoggingEvent = event; System.out.println("进入自定义日志插入");
insertOwnerLog(event, "传入上方的数据库链接", eventId); }
private void insertOwnerLog(ILoggingEvent event, Connection connection, long eventId) throws Exception { Object[] params = event.getArgumentArray(); Object param = params[1]; Class<?> aClass = param.getClass(); StringBuffer tableNameStr = new StringBuffer("insert into "); StringBuffer fieldStr = new StringBuffer("("); StringBuffer valueStr = new StringBuffer("values ("); DruidConfiguration druidConfiguration = new DruidConfiguration();
Table table = aClass.getAnnotation(Table.class); if (table == null) { return; } else { tableNameStr.append(table.name() + " "); }
Field[] declaredFields = aClass.getDeclaredFields();
for (int i = 0, size = declaredFields.length; i < size; i++) { Column columnAnnot = declaredFields[i].getAnnotation(Column.class); if (declaredFields[i].getAnnotation(Column.class) != null) { String columnName = columnAnnot.name(); fieldStr.append(columnName); if (i == size - 1) { fieldStr.append(") "); } else { fieldStr.append(","); }
Object fieldValue = ReflectUtil.getFieldValue(param, declaredFields[i].getName()); if (fieldValue instanceof String) { valueStr.append("\"" + fieldValue + "\""); } else { valueStr.append(fieldValue); }
if (i == size - 1) { valueStr.append(")"); } else { valueStr.append(","); } } }
tableNameStr.append(fieldStr).append(valueStr);
Level level = event.getLevel(); PreparedStatement insertStatement = null;
try { insertStatement = connection.prepareStatement(tableNameStr.toString()); if (insertStatement != null) { insertStatement.execute(); } } catch (Exception e) { e.printStackTrace(); } finally { } }
private DiyDBAppender setLong(PreparedStatement statement, int parameterIndex, Long value) throws SQLException { statement.setLong(parameterIndex, value); return this;
}
private DiyDBAppender setInt(PreparedStatement statement, int parameterIndex, Integer value) throws SQLException { statement.setInt(parameterIndex, value != null ? value : 5); return this; }
private DiyDBAppender setString(PreparedStatement statement, int parameterIndex, String value) throws SQLException { statement.setString(parameterIndex, value); return this; }
}
|