Skip to content

Commit

Permalink
feat: Add associated
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Mar 11, 2024
1 parent 9f56015 commit 1cf530e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
31 changes: 17 additions & 14 deletions lib/src/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,58 +37,59 @@ class Log {
/// and result is the new [obj]
/// @param [override], the original log object is still passed around, but the stringified version of the [obj] is replaced with this.
/// @param [append], appends the message to the log entry.
/// @param [associated], a [StackTrace] associated with the object. Usually not directly available from the object.
/// {@endtemplate}
static void t(obj, {String? override, String? append}) {
static void t(obj, {String? override, String? append, StackTrace? associated}) {
if (level.value <= Level.trace.value) {
return _applyObjToLog(Level.trace, obj, override, append, traceLogConfig);
return _applyObjToLog(Level.trace, obj, override, append, associated, traceLogConfig);
}
}

/// debug.
///
/// {@macro Logging.levelParams}
static void d(obj, {String? override, String? append}) {
static void d(obj, {String? override, String? append, StackTrace? associated}) {
if (level.value <= Level.debug.value) {
return _applyObjToLog(Level.debug, obj, override, append, debugLogConfig);
return _applyObjToLog(Level.debug, obj, override, append, associated, debugLogConfig);
}
}

/// info.
///
/// {@macro Logging.levelParams}
static void i(obj, {String? override, String? append}) {
static void i(obj, {String? override, String? append, StackTrace? associated}) {
if (level.value <= Level.info.value) {
return _applyObjToLog(Level.info, obj, override, append, infoLogConfig);
return _applyObjToLog(Level.info, obj, override, append, associated, infoLogConfig);
}
}

/// warning.
///
/// {@macro Logging.levelParams}
static void w(obj, {String? override, String? append}) {
static void w(obj, {String? override, String? append, StackTrace? associated}) {
if (level.value <= Level.warning.value) {
return _applyObjToLog(
Level.warning, obj, override, append, warningLogConfig);
Level.warning, obj, override, append, associated, warningLogConfig);
}
}

/// error.
///
/// {@macro Logging.levelParams}
static void e(obj, {String? override, String? append}) {
static void e(obj, {String? override, String? append, StackTrace? associated}) {
if (level.value <= Level.error.value) {
return _applyObjToLog(
Level.error, obj, override, append, errorLogConfig);
Level.error, obj, override, append, associated, errorLogConfig);
}
}

/// fatal.
///
/// {@macro Logging.levelParams}
static void f(obj, {String? override, String? append}) {
static void f(obj, {String? override, String? append, StackTrace? associated}) {
if (level.value <= Level.fatal.value) {
return _applyObjToLog(
Level.fatal, obj, override, append, fatalLogConfig);
Level.fatal, obj, override, append, associated, fatalLogConfig);
}
}

Expand All @@ -99,6 +100,7 @@ class Log {
Object objToLog,
String? messageOverride,
String? messageAppend,
StackTrace? associatedStackTrace,
LogLevelConfig logConfig) {
String? logId;
if (logConfig._willCreateLogId) {
Expand All @@ -113,7 +115,7 @@ class Log {
logPointStackTrace = modifyStackTrace(StackTrace.current, startOffset: 2);
}

final logEvent = LogEvent(level, objToLog, messageOverride, messageAppend,
final logEvent = LogEvent(level, objToLog, messageOverride, messageAppend, associatedStackTrace,
time, logId, logPointStackTrace);

logConfig.onLog?.call(logEvent);
Expand Down Expand Up @@ -148,11 +150,12 @@ class LogEvent {
final Object obj;
final String? override;
final String? append;
final StackTrace? associatedStackTrace;
final DateTime? time;
final String? id;
final StackTrace? logPointStackTrace;

LogEvent(this.level, this.obj, this.override, this.append, this.time, this.id,
LogEvent(this.level, this.obj, this.override, this.append, this.associatedStackTrace, this.time, this.id,
this.logPointStackTrace);
}

Expand Down
23 changes: 21 additions & 2 deletions lib/src/log_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class LogLevelConfig {
}) : components = const [
ObjectTypeComponent(),
StringifiedComponent(),
AssociatedStackTraceComponent(),
AppendLogComponent(),
IdComponent(),
TimeComponent(),
Expand Down Expand Up @@ -87,9 +88,8 @@ class ObjectTypeComponent extends LogComponent {
}

class StringifiedComponent extends LogComponent {
final int ifHasStacktraceKeep;

const StringifiedComponent({this.ifHasStacktraceKeep = 6});
const StringifiedComponent();

@override
LogField build(LogEvent event) {
Expand All @@ -106,6 +106,25 @@ class StringifiedComponent extends LogComponent {
List<ToCapture> get toCapture => const [];
}

class AssociatedStackTraceComponent extends LogComponent {

const AssociatedStackTraceComponent();

@override
LogField? build(LogEvent event) {
if (event.associatedStackTrace == null) {
return null;
}
return LogField(
header: 'Associated StackTrace',
body: event.associatedStackTrace!,
);
}

@override
List<ToCapture> get toCapture => const [];
}

class AppendLogComponent extends LogComponent {
const AppendLogComponent();

Expand Down
8 changes: 5 additions & 3 deletions test/rewind_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ void main() {
late void Function() logFn;

test('test1', () {
logFn = () => Log.t('test', override: 'override', append: 'append');
final current = StackTrace.current;
logFn = () => Log.t('test', override: 'override', append: 'append', associatedStackTrace: current);
func(logFn, 4);
});

test('test2', () {
logFn = () => Log.i('test', append: 'append');
final current = StackTrace.current;
logFn = () => Log.i('test', append: 'append', associatedStackTrace: current);
func(logFn, 4);
});

Expand All @@ -36,7 +38,7 @@ void main() {
});

test('test-anyhow1', () {
logFn = () => Log.t(bail("bailing here trace"));
logFn = () => Log.t(bail("bailing here trace"), associatedStackTrace: StackTrace.current);
func(logFn, 4);
});

Expand Down

0 comments on commit 1cf530e

Please sign in to comment.