Ich habe versucht, einen ExtHandler wie folgt zu erstellen:
Code: Select all
package org.myorg;
import io.agroal.api.AgroalDataSource;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logmanager.ExtHandler;
import org.jboss.logmanager.ExtLogRecord;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ApplicationScoped
public class DatabaseLogHandler extends ExtHandler {
@Inject
private AgroalDataSource dataSource;
@Override
protected void doPublish(final ExtLogRecord record) {
try (final Connection conn = this.dataSource.getConnection();
final PreparedStatement ps = conn.prepareStatement(
"INSERT INTO logs(timestamp, level, logger, message) VALUES (?, ?, ?, ?)")) {
ps.setLong(1, record.getMillis());
ps.setString(2, record.getLevel().toString());
ps.setString(3, record.getLoggerName());
ps.setString(4, record.getMessage());
ps.executeUpdate();
} catch (final SQLException e) {
e.printStackTrace();
}
}
}
Code: Select all
quarkus.log.handler.database=DatabaseLogHandler
Mobile version