@Override protected void writeInternal(final LogEvent event, final Serializable serializable) { for (int i = 0; i < columnMappings.size(); i++) { final ColumnMapping columnMapping = columnMappings.get(i); if (ThreadContextMap.class.isAssignableFrom(columnMapping.getType()) || ReadOnlyStringMap.class.isAssignableFrom(columnMapping.getType())) { values[i] = event.getContextData().toMap(); } else if (ThreadContextStack.class.isAssignableFrom(columnMapping.getType())) { values[i] = event.getContextStack().asList(); } else if (Date.class.isAssignableFrom(columnMapping.getType())) { values[i] = DateTypeConverter.fromMillis(event.getTimeMillis(), columnMapping.getType().asSubclass(Date.class)); } else { values[i] = TypeConverters.convert(columnMapping.getLayout().toSerializable(event), columnMapping.getType(), null); } } final BoundStatement boundStatement = preparedStatement.bind(values); if (batchStatement == null) { session.execute(boundStatement); } else { batchStatement.add(boundStatement); } }
@Override public ColumnMapping build() { if (pattern != null) { layout = PatternLayout.newBuilder() .withPattern(pattern) .withConfiguration(configuration) .build(); } if (!(layout == null || literal == null || Date.class.isAssignableFrom(type) || ReadOnlyStringMap.class.isAssignableFrom(type) || ThreadContextMap.class.isAssignableFrom(type) || ThreadContextStack.class.isAssignableFrom(type))) { LOGGER.error("No 'layout' or 'literal' value specified and type ({}) is not compatible with ThreadContextMap, ThreadContextStack, or java.util.Date for the mapping", type, this); return null; } if (literal != null && parameter != null) { LOGGER.error("Only one of 'literal' or 'parameter' can be set on the column mapping {}", this); return null; } return new ColumnMapping(name, source, layout, literal, parameter, type); }
@BeforeClass public static void setupClass() { map.put("test1", "one"); map.put("test2", "two"); logEvent0 = new Log4jLogEvent("test", null, "MapRewritePolicyTest.setupClass()", Level.ERROR, new SimpleMessage("Test"), new RuntimeException("test"), map, null, "none", new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 28), 2); logEvent1 = new Log4jLogEvent("test", null, "MapRewritePolicyTest.setupClass()", Level.ERROR, new MapMessage(map), null, map, null, "none", new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 29), 2); final ThreadContextStack stack = new MutableThreadContextStack(new ArrayList<String>(map.values())); logEvent2 = new Log4jLogEvent("test", MarkerManager.getMarker("test"), "MapRewritePolicyTest.setupClass()", Level.TRACE, new StructuredDataMessage("test", "Nothing", "test", map), new RuntimeException("test"), null, stack, "none", new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 30), 20000000); logEvent3 = new Log4jLogEvent("test", null, "MapRewritePolicyTest.setupClass()", Level.ALL, new MapMessage(map), null, map, stack, null, new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 31), Long.MAX_VALUE); rewrite = new KeyValuePair[] {new KeyValuePair("test2", "2"), new KeyValuePair("test3", "three")}; }
@BeforeClass public static void setupClass() { stringMap.putValue("test1", "one"); stringMap.putValue("test2", "two"); map = stringMap.toMap(); logEvent0 = Log4jLogEvent.newBuilder() // .setLoggerName("test") // .setContextData(stringMap) // .setLoggerFqcn("MapRewritePolicyTest.setupClass()") // .setLevel(Level.ERROR) // .setMessage(new SimpleMessage("Test")) // .setThrown(new RuntimeException("test")) // .setThreadName("none") .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 28)) .setTimeMillis(2).build(); logEvent1 = ((Log4jLogEvent) logEvent0).asBuilder() // .setMessage(new StringMapMessage(map)) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 29)) // .build(); final ThreadContextStack stack = new MutableThreadContextStack(new ArrayList<>(map.values())); logEvent2 = ((Log4jLogEvent) logEvent0).asBuilder() // .setContextStack(stack) // .setMarker(MarkerManager.getMarker("test")) // .setLevel(Level.TRACE) // .setMessage(new StructuredDataMessage("test", "Nothing", "test", map)) // .setTimeMillis(20000000) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 30)) // .build(); logEvent3 = ((Log4jLogEvent) logEvent0).asBuilder() // .setContextStack(stack) // .setLevel(Level.ALL) // .setMessage(new StringMapMessage(map)) // .setTimeMillis(Long.MAX_VALUE) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 31)) // .build(); rewrite = new KeyValuePair[]{new KeyValuePair("test2", "2"), new KeyValuePair("test3", "three")}; }
@Override protected void writeInternal(final LogEvent event, final Serializable serializable) { StringReader reader = null; try { if (!this.isRunning() || this.connection == null || this.connection.isClosed() || this.statement == null || this.statement.isClosed()) { throw new AppenderLoggingException( "Cannot write logging event; JDBC manager not connected to the database."); } if (serializable instanceof MapMessage) { setFields((MapMessage<?, ?>) serializable); } int i = 1; // JDBC indices start at 1 for (final ColumnMapping mapping : this.columnMappings) { if (ThreadContextMap.class.isAssignableFrom(mapping.getType()) || ReadOnlyStringMap.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, event.getContextData().toMap()); } else if (ThreadContextStack.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, event.getContextStack().asList()); } else if (Date.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, DateTypeConverter.fromMillis(event.getTimeMillis(), mapping.getType().asSubclass(Date.class))); } else { StringLayout layout = mapping.getLayout(); if (layout != null) { if (Clob.class.isAssignableFrom(mapping.getType())) { this.statement.setClob(i++, new StringReader(layout.toSerializable(event))); } else if (NClob.class.isAssignableFrom(mapping.getType())) { this.statement.setNClob(i++, new StringReader(layout.toSerializable(event))); } else { final Object value = TypeConverters.convert(layout.toSerializable(event), mapping.getType(), null); if (value == null) { this.statement.setNull(i++, Types.NULL); } else { this.statement.setObject(i++, value); } } } } } for (final ColumnConfig column : this.columnConfigs) { if (column.isEventTimestamp()) { this.statement.setTimestamp(i++, new Timestamp(event.getTimeMillis())); } else if (column.isClob()) { reader = new StringReader(column.getLayout().toSerializable(event)); if (column.isUnicode()) { this.statement.setNClob(i++, reader); } else { this.statement.setClob(i++, reader); } } else if (column.isUnicode()) { this.statement.setNString(i++, column.getLayout().toSerializable(event)); } else { this.statement.setString(i++, column.getLayout().toSerializable(event)); } } if (this.isBatchSupported) { this.statement.addBatch(); } else if (this.statement.executeUpdate() == 0) { throw new AppenderLoggingException( "No records inserted in database table for log event in JDBC manager."); } } catch (final SQLException e) { throw new AppenderLoggingException("Failed to insert record for log event in JDBC manager: " + e.getMessage(), e); } finally { Closer.closeSilently(reader); } }