Skip to main content

logsmith 1.0.0 copy "logsmith: ^1.0.0" to clipboard
logsmith: ^1.0.0 copied to clipboard

A declarative structured-logging library — typed attrs, themed output, six formats (text, JSON, YAML, TOML, Mace, logfmt), and categorical stdout/stderr routing.

example/logsmith_example.dart

import 'dart:io';

import 'package:logsmith/logsmith.dart';

Future<void> main() async {
  const emitter = Emitter();

  // A handler is a formatter function plus a stream-routing policy.
  // Default policy sends warning+ to stderr; lower levels go to stdout.
  final terminal = Handler(
    format: (record) => emitter.text(record, theme: Themes.catppuccinMocha),
  );
  final compact = Logger.singleLine(name: 'api', handlers: [terminal]);
  compact.info('request\naccepted', attrs: {'status': 202});
  compact.withAttrs({'request_id': 'req-42'}).success('queued');

  final expanded = Logger.multiline(name: 'worker', handlers: [terminal]);
  expanded.warning(
    'retrying\nupstream unavailable',
    attrs: {
      'retry': {'attempt': 2, 'delay': const Duration(milliseconds: 250)},
    },
  );
  Logger.boxed(name: 'release', handlers: [terminal]).success(
    'Deployment complete',
    attrs: {'version': '1.0.0', 'healthy': true},
  );

  // Emitters can also render records directly, without a logger/handler.
  final record = Record(
    time: DateTime.now().toUtc(),
    level: Level.info,
    message: 'format tour\nsecond line',
    name: 'example',
    kind: LoggerKind.multiline,
    attrs: {
      'count': 3,
      'labels': ['demo', 'structured'],
    },
  );
  final formats = <String, String Function(Record)>{
    'text (light theme)': (r) => emitter.text(r, theme: Themes.catppuccinLatte),
    'json': (r) => emitter.json(r, indent: 2),
    'yaml': emitter.yaml,
    'toml': emitter.toml,
    // Each Mace entry is a complete document, not a concatenable Mace file.
    'mace': emitter.mace,
    'logfmt': emitter.logfmt,
  };
  for (final format in formats.entries) {
    stdout.writeln('\n--- ${format.key} ---');
    stdout.writeln(format.value(record));
  }

  // Tee to a machine-readable file with a second handler. Always-route keeps
  // even warnings/errors in this file rather than falling back to stderr.
  // A temporary directory keeps the tour safe to run repeatedly.
  final directory = await Directory.systemTemp.createTemp('logsmith-example-');
  final path = '${directory.path}${Platform.pathSeparator}events.jsonl';
  final file = FileSink(path, append: false);
  try {
    final audit = Logger.singleLine(
      name: 'audit',
      handlers: [
        terminal,
        Handler(format: emitter.json, policy: StreamPolicy.always(file)),
      ],
    );
    audit.info('signed in', attrs: {'user_id': 42});
    audit.error('permission denied', attrs: {'resource': '/admin'});
  } finally {
    // Await close before reading, deleting, or exiting the process.
    await file.close();
  }
  stdout.writeln('\nFileSink JSON-lines:\n${await File(path).readAsString()}');
  await directory.delete(recursive: true);
}
0
likes
160
points
151
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A declarative structured-logging library — typed attrs, themed output, six formats (text, JSON, YAML, TOML, Mace, logfmt), and categorical stdout/stderr routing.

Repository (GitHub)
View/report issues

Topics

#logging #structured-logging #terminal #ansi #cli

License

BSD-3-Clause (license)

Dependencies

toml

More

Packages that depend on logsmith