Package org.logevents.observers
Class DatabaseLogEventObserver
- java.lang.Object
-
- org.logevents.core.AbstractFilteredLogEventObserver
-
- org.logevents.observers.AbstractBatchingLogEventObserver
-
- org.logevents.observers.DatabaseLogEventObserver
-
- All Implemented Interfaces:
LogEventObserver
,LogEventSource
public class DatabaseLogEventObserver extends AbstractBatchingLogEventObserver implements LogEventSource
Writes log events asynchronously to relational database with JDBC. Will create the necessary tables at startup if they don't exist.DatabaseLogEventObserver
can use an existing database and doesn't interfere with database migrations with Flyway or similar tools. Log events are written with UUID primary keys and the same database can be used for several applications if you want. This class should work with all relational database providers but has only been verified with Postgres and H2.DatabaseLogEventObserver
is a nice compromise if you want to take a step towards centralized logging, but don't want to set up Elastic search, Splunk or a similar solution quite yet.DatabaseLogEventObserver
is designed to be used withLogEventsServlet
throughWebLogEventObserver
Sample configuration
observer.db=DatabaseLogEventObserver observer.db.jdbcUrl=jdbc:postgres://localhost/logdb observer.db.jdbcUsername=logevents observer.db.jdbcPassword=sdgawWWF/)l31L observer.db.logeventsTable=log_events observer.db.logeventsMdcTable=log_events_mdc observer.db.loginTimeout=20 observer.db.noFetchFirstSupport=false
-
-
Field Summary
-
Fields inherited from class org.logevents.observers.AbstractBatchingLogEventObserver
defaultBatcher, executor, scheduledExecutorService, shutdownHook
-
-
Constructor Summary
Constructors Constructor Description DatabaseLogEventObserver(Map<String,String> properties, String prefix)
DatabaseLogEventObserver(Configuration configuration)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
createLogEventsTable(Connection connection)
void
createMdcTable(Connection connection)
LogEventSummary
getSummary(LogEventQuery query)
Retrieves a summary of log events within the specified interval above the threshold log levelvoid
processBatch(LogEventBatch batch)
LogEventQueryResult
query(LogEventQuery query)
String
questionMarks(int size)
-
Methods inherited from class org.logevents.observers.AbstractBatchingLogEventObserver
configureBatching, configureMarkers, createBatcher, createMdcBatcher, createProcessor, doLogEvent, getBatcher, getBatcherFactory, getDefaultBatcher, getMarkerBatcher, processBatch, toString
-
Methods inherited from class org.logevents.core.AbstractFilteredLogEventObserver
configureFilter, filteredOn, getCondition, getThreshold, isEnabled, logEvent, setCondition, setFilter, setThreshold, shouldLogEvent
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.logevents.LogEventObserver
filteredOn, isEnabled, isEnabled, logEvent, stream, toList
-
-
-
-
Constructor Detail
-
DatabaseLogEventObserver
public DatabaseLogEventObserver(Map<String,String> properties, String prefix)
-
DatabaseLogEventObserver
public DatabaseLogEventObserver(Configuration configuration)
-
-
Method Detail
-
createMdcTable
public void createMdcTable(Connection connection) throws SQLException
- Throws:
SQLException
-
createLogEventsTable
public void createLogEventsTable(Connection connection) throws SQLException
- Throws:
SQLException
-
questionMarks
public String questionMarks(int size)
-
processBatch
public void processBatch(LogEventBatch batch)
- Specified by:
processBatch
in classAbstractBatchingLogEventObserver
-
query
public LogEventQueryResult query(LogEventQuery query)
- Specified by:
query
in interfaceLogEventSource
-
getSummary
public LogEventSummary getSummary(LogEventQuery query) throws SQLException
Retrieves a summary of log events within the specified interval above the threshold log level- Throws:
SQLException
-
-