Skip to main content

log_trail_sqlite 0.1.1 copy "log_trail_sqlite: ^0.1.1" to clipboard
log_trail_sqlite: ^0.1.1 copied to clipboard

SQLite store for log_trail: the log survives restarts, with indexed pages, tag counts and FTS5 text search.

example/example.dart

import 'dart:io';

import 'package:log_trail/log_trail.dart';
import 'package:log_trail_sqlite/log_trail_sqlite.dart';

const userId = LogField('example.user_id', LogCodec.string);

Future<void> main() async {
  final directory = Directory.systemTemp.createTempSync('log_trail_sqlite_example_');
  final path = '${directory.path}/log.sqlite';

  // First run: write a few entries and close everything.
  await _run(path, 'first');
  // Second run: the entries of the first run are still there.
  await _run(path, 'second');

  directory.deleteSync(recursive: true);
}

Future<void> _run(String path, String run) async {
  final logger = RootLogger(sinks: [ConsoleSink()]);
  logger.info('Run $run starting');

  // The store opens synchronously; keep at most 1000 entries.
  final store = SqliteLogStore.open(path, limit: 1000);
  logger
    ..addSink(RepeatCollapsingSink(LogStoreSink(store)))
    ..finishStartup()
    ..setContextAttribute(userId('42'));
  logger.child(tags: {'auth'}).warning('Slow sign-in', attributes: [LogField.details('run $run')]);

  await logger.flush();
  final page = await store.queryEntries(LogQuery(filter: LogFilter(attributes: [userId('42')]), newestFirst: true));
  stdout.writeln('Run $run: ${store.count} entries in the store, ${page.entries.length} for user 42:');
  for (final StoredLogEntry(:id, :entry) in page.entries) {
    stdout.writeln(
      '  #$id ${formatLogTimestamp(entry.time)} ${entry.message} (${entry.attributes.get(LogField.details)})',
    );
  }

  await logger.close();
  await store.close();
}
0
likes
150
points
27
downloads

Documentation

API reference

Publisher

verified publisherastonio.com

Weekly Downloads

SQLite store for log_trail: the log survives restarts, with indexed pages, tag counts and FTS5 text search.

Repository (GitHub)
View/report issues

Topics

#logging #sqlite #persistence

License

MIT (license)

Dependencies

log_trail, meta, sqlite3

More

Packages that depend on log_trail_sqlite