Skip to content

Commit

Permalink
chore: bunch of updates
Browse files Browse the repository at this point in the history
  1. rename Tracer to Timer [BREAKING]
  2. mark Logger, Interface, Record, Timer and Level as abstract interfaces [BREAKING]
  3. mark internal implementations as final [BREAKING]
  4. mark LoggerBase mixin as base [BREAKING]
  5. updated fields names for Timer start -> started_at. Added stopped_at [BREAKING]
  6. bumped allowed SDK version to 3.0.0
  • Loading branch information
bolt-roman-vanesyan committed Nov 24, 2023
1 parent f24e9b3 commit 9850c5b
Show file tree
Hide file tree
Showing 30 changed files with 158 additions and 146 deletions.
12 changes: 1 addition & 11 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false

errors:
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error

enable-experiment:
- non-nullable

linter:
rules:
# errors
Expand All @@ -24,9 +17,7 @@ linter:
- control_flow_in_finally
- empty_statements
- hash_and_equals
- invariant_booleans
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- collection_methods_unrelated_type
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- no_duplicate_case_values
Expand Down Expand Up @@ -90,7 +81,6 @@ linter:
- prefer_const_literals_to_create_immutables
# - prefer_constructors_over_static_methods
- prefer_contains
- prefer_equal_for_default_values
- prefer_expression_function_bodies
- prefer_final_fields
- prefer_final_locals
Expand Down
2 changes: 1 addition & 1 deletion example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Future<void> main() async {
const Str('mime', 'image/png'),
});

final tracer = context.trace('Uploading!', level: Level.info);
final tracer = context.startTimer('Uploading!', level: Level.info);

// Emulate uploading, wait for 1 sec.
await Future<void>.delayed(const Duration(seconds: 1));
Expand Down
2 changes: 1 addition & 1 deletion example/console_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Future<void> main() async {
const Str('mime', 'image/png'),
});

final tracer = context.trace('Uploading!', level: Level.info);
final tracer = context.startTimer('Uploading!', level: Level.info);

// Emulate uploading, wait for 1 sec.
await Future<void>.delayed(const Duration(seconds: 1));
Expand Down
8 changes: 4 additions & 4 deletions lib/global_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ void log(Level level, String message) => _logger.log(level, message);
void debug(String message) => _logger.debug(message);

/// Starts tracing and emits a record with [message] and [level]
/// severity level; to stop tracing call [Tracer.stop] on the returned tracer.
/// severity level; to stop tracing call [Timer.stop] on the returned tracer.
///
/// See more [Interface.trace].
Tracer trace(String message, {Level level = Level.debug}) =>
_logger.trace(message, level: level);
/// See more [Interface.startTimer].
Timer trace(String message, {Level level = Level.debug}) =>
_logger.startTimer(message, level: level);

/// Emits a record with [message] and [Level.info] severity level.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/jetlog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export 'src/interface.dart';
export 'src/level.dart';
export 'src/logger.dart';
export 'src/record.dart';
export 'src/tracer.dart';
export 'src/timer.dart';
8 changes: 4 additions & 4 deletions lib/src/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef ValueProducer<V> = V Function();

/// [FieldKind] represents a [Field.value] type. It is used for [Field]
/// serialization to remove necessity to dynamically probe [Field.value] type.
class FieldKind {
final class FieldKind {
const FieldKind(this.value);

/// Unique value for type.
Expand Down Expand Up @@ -55,7 +55,7 @@ class FieldKind {
}

/// A [Field] used to add a key-value pair to a logger's context.
abstract class Field<V> {
abstract interface class Field<V> {
/// Creates a new [Field] with [name] and [value].
const factory Field(
{required String name,
Expand Down Expand Up @@ -85,7 +85,7 @@ abstract class Field<V> {
FieldKind get kind;
}

class _StaticField<V> implements Field<V> {
base class _StaticField<V> implements Field<V> {
const _StaticField(
{required this.name, required this.value, required this.kind});

Expand All @@ -105,7 +105,7 @@ class _StaticField<V> implements Field<V> {
bool operator ==(Object other) => other is Field && other.name == name;
}

class _LazyField<V> implements Field<V> {
base class _LazyField<V> implements Field<V> {
_LazyField({required this.name, required this.producer, required this.kind});

V? _value;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/fields/bool.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
part of jetlog.fields;

class _StaticBool extends _StaticField<bool?> implements Bool {
final class _StaticBool extends _StaticField<bool?> implements Bool {
// ignore:avoid_positional_boolean_parameters
const _StaticBool(String name, bool? value)
: super(name: name, value: value, kind: FieldKind.boolean);
}

class _LazyBool extends _LazyField<bool?> implements Bool {
final class _LazyBool extends _LazyField<bool?> implements Bool {
_LazyBool(String name, ValueProducer<bool?> producer)
: super(name: name, producer: producer, kind: FieldKind.boolean);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/fields/double.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
part of jetlog.fields;

class _StaticDouble extends _StaticField<double?> implements Double {
final class _StaticDouble extends _StaticField<double?> implements Double {
const _StaticDouble(String name, double? value)
: super(name: name, value: value, kind: FieldKind.double);
}

class _LazyDouble extends _LazyField<double?> implements Double {
final class _LazyDouble extends _LazyField<double?> implements Double {
_LazyDouble(String name, ValueProducer<double?> producer)
: super(name: name, producer: producer, kind: FieldKind.double);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/fields/dtm.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
part of jetlog.fields;

class _StaticDTM extends _StaticField<DateTime?> implements DTM {
final class _StaticDTM extends _StaticField<DateTime?> implements DTM {
const _StaticDTM(String name, DateTime? value)
: super(name: name, value: value, kind: FieldKind.dateTime);
}

class _LazyDTM extends _LazyField<DateTime?> implements DTM {
final class _LazyDTM extends _LazyField<DateTime?> implements DTM {
_LazyDTM(String name, ValueProducer<DateTime?> producer)
: super(name: name, producer: producer, kind: FieldKind.dateTime);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/fields/dur.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
part of jetlog.fields;

class _StaticDur extends _StaticField<Duration?> implements Dur {
final class _StaticDur extends _StaticField<Duration?> implements Dur {
const _StaticDur(String name, Duration? value)
: super(name: name, value: value, kind: FieldKind.duration);
}

class _LazyDur extends _LazyField<Duration?> implements Dur {
final class _LazyDur extends _LazyField<Duration?> implements Dur {
_LazyDur(String name, ValueProducer<Duration?> producer)
: super(name: name, producer: producer, kind: FieldKind.duration);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/fields/int.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
part of jetlog.fields;

class _StaticInt extends _StaticField<int?> implements Int {
final class _StaticInt extends _StaticField<int?> implements Int {
const _StaticInt(String name, int? value)
: super(name: name, value: value, kind: FieldKind.integer);
}

class _LazyInt extends _LazyField<int?> implements Int {
final class _LazyInt extends _LazyField<int?> implements Int {
_LazyInt(String name, ValueProducer<int?> producer)
: super(name: name, producer: producer, kind: FieldKind.integer);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/fields/num.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
part of jetlog.fields;

class _StaticNum extends _StaticField<num?> implements Num {
final class _StaticNum extends _StaticField<num?> implements Num {
const _StaticNum(String name, num? value)
: super(name: name, value: value, kind: FieldKind.number);
}

class _LazyNum extends _LazyField<num?> implements Num {
final class _LazyNum extends _LazyField<num?> implements Num {
_LazyNum(String name, ValueProducer<num?> producer)
: super(name: name, producer: producer, kind: FieldKind.number);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/fields/obj.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
part of jetlog.fields;

class _StaticObj extends _StaticField<Iterable<Field>?> implements Obj {
final class _StaticObj extends _StaticField<Iterable<Field>?> implements Obj {
_StaticObj(String name, Loggable? value)
: super(name: name, value: value?.toFields(), kind: FieldKind.object);
}

class _LazyObj extends _LazyField<Iterable<Field>?> implements Obj {
final class _LazyObj extends _LazyField<Iterable<Field>?> implements Obj {
_LazyObj(String name, ValueProducer<Iterable<Field>?> producer)
: super(name: name, producer: producer, kind: FieldKind.object);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/fields/str.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
part of jetlog.fields;

class _StaticStr extends _StaticField<String?> implements Str {
final class _StaticStr extends _StaticField<String?> implements Str {
const _StaticStr(String name, String? value)
: super(name: name, value: value, kind: FieldKind.string);
}

class _LazyStr extends _LazyField<String?> implements Str {
final class _LazyStr extends _LazyField<String?> implements Str {
_LazyStr(String name, ValueProducer<String?> producer)
: super(name: name, producer: producer, kind: FieldKind.string);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/formatters/text_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ class TextFormatter with FormatterBase<String> {
for (final field in fields) {
final handler = getFieldFormatter(field.kind);

buffer..write(handler(field))..write(' ');
buffer
..write(handler(field))
..write(' ');
}

return buffer.toString().trim();
Expand Down
12 changes: 6 additions & 6 deletions lib/src/interface.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import 'package:jetlog/src/field.dart' show Field;
import 'package:jetlog/src/level.dart';
import 'package:jetlog/src/logger.dart';
import 'package:jetlog/src/tracer.dart';
import 'package:jetlog/src/timer.dart';

/// [Interface] represents a set of common methods that is implemented by both
/// [Logger] and logging context returned by [Interface.bind].
abstract class Interface {
abstract interface class Interface {
/// Emits a record with [message] and [level] severity level.
///
/// If [level] is either [Level.all] or [Level.off] it will immediately
/// throw [ArgumentError].
void log(Level level, String message);

/// Starts tracing and emits a record with [message] and [level]
/// severity level; to stop tracing call [Tracer.stop] on the returned tracer.
Tracer trace(String message, {Level level = Level.debug});
/// severity level; to stop tracing call [Timer.stop] on the returned timer.
Timer startTimer(String message, {Level level = Level.debug});

/// Creates and returns a new logging context with bound collection of
/// [fields] added to existing one.
Expand All @@ -27,12 +27,12 @@ abstract class Interface {
/// Str('mime', 'image/png'),
/// });
///
/// final tracer = context.trace('Uploading!', Level.info);
/// final timer = context.startTimer('Uploading!', Level.info);
///
/// // Emulate uploading, wait for 1 sec.
/// await Future<void>.delayed(const Duration(seconds: 1));
///
/// tracer.stop('Aborting...');
/// timer.stop('Aborting...');
/// context.fatal('Failed to upload!');
/// ```
///
Expand Down
2 changes: 1 addition & 1 deletion lib/src/level.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'package:jetlog/src/record.dart';
/// and does not overlap with `0x100`, `0x200`, `0x300`, `0x400`, `0x500` and
/// `0x600` reserved for [debug], [info], [warning], [danger] and [fatal]
/// levels respectively.
class Level implements Comparable<Level> {
final class Level implements Comparable<Level> {
const Level({required this.name, required this.value});

/// User-readable name of this severity level.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final _loggers = LoggerManager(_root);
/// data represented as a collection of [Field]s. Calling to [Logger.bind] will
/// result into a new context with logging capabilities and bound collection of
/// fields has been created.
abstract class Logger implements Interface {
abstract interface class Logger implements Interface {
/// Creates a new detached logger.
///
/// Created logger has no parent and children and is not a part of
Expand Down Expand Up @@ -112,7 +112,7 @@ abstract class Logger implements Interface {
/// Base mixin for implementing [Logger].
///
/// Contains default implementations of common methods across loggers.
mixin LoggerBase implements Logger {
base mixin LoggerBase implements Logger {
@override
@pragma('vm:prefer-inline')
bool isEnabledFor(Level level) {
Expand Down
18 changes: 12 additions & 6 deletions lib/src/logger_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import 'package:jetlog/src/level.dart';
import 'package:jetlog/src/logger.dart';
import 'package:jetlog/src/logging_context.dart';
import 'package:jetlog/src/record.dart';
import 'package:jetlog/src/tracer.dart';
import 'package:jetlog/src/timer.dart';

class LoggerImpl with LoggerBase {
final class LoggerImpl with LoggerBase {
LoggerImpl._(this.name, [this.children]) {
_context = LoggingContext(this);
}
Expand Down Expand Up @@ -70,8 +70,8 @@ class LoggerImpl with LoggerBase {
void log(Level level, String message) => _context.log(level, message);

@override
Tracer trace(String message, {Level level = Level.debug}) =>
_context.trace(message, level: level);
Timer startTimer(String message, {Level level = Level.debug}) =>
_context.startTimer(message, level: level);

@override
@pragma('vm:prefer-inline')
Expand All @@ -84,10 +84,16 @@ class LoggerImpl with LoggerBase {
buffer.write('Logger(');

if (name != null) {
buffer..write('name=')..write(name)..write(', ');
buffer
..write('name=')
..write(name)
..write(', ');
}

buffer..write('level=')..write(level.name)..write(')');
buffer
..write('level=')
..write(level.name)
..write(')');

return buffer.toString();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/logger_manager.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:jetlog/src/logger_impl.dart';

class LoggerManager {
final class LoggerManager {
LoggerManager(this.root) : _loggers = {};

final Map<String, LoggerImpl> _loggers;
Expand Down
14 changes: 7 additions & 7 deletions lib/src/logging_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import 'package:jetlog/src/interface.dart';
import 'package:jetlog/src/level.dart';
import 'package:jetlog/src/logger_impl.dart';
import 'package:jetlog/src/record_impl.dart';
import 'package:jetlog/src/tracer.dart';
import 'package:jetlog/src/tracer_impl.dart';
import 'package:jetlog/src/timer.dart';
import 'package:jetlog/src/timer_impl.dart';

class LoggingContext implements Interface {
LoggingContext(this._logger, [this._fields]);
LoggingContext(this._logger, [this._fields = const {}]);

final LoggerImpl _logger;
final Set<Field>? _fields;
final Set<Field> _fields;

@override
void log(Level level, String message) {
Expand All @@ -30,10 +30,10 @@ class LoggingContext implements Interface {
@pragma('vm:prefer-inline')
Interface bind([Iterable<Field>? fields]) => LoggingContext(_logger, {
...?fields,
...?_fields,
..._fields,
});

@override
Tracer trace(String message, {Level level = Level.debug}) =>
TracerImpl(this, level)..start(message);
Timer startTimer(String message, {Level level = Level.debug}) =>
TimerImpl(this, level)..start(message);
}
Loading

0 comments on commit 9850c5b

Please sign in to comment.