Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go_router] Don't log if hierarchicalLoggingEnabled is true #6019

Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fc3709d
bug: Don't log if hierarchicalLoggingEnabled is true
ValentinVignal Jan 31, 2024
c3a0794
chore: Update the version
ValentinVignal Jan 31, 2024
69e2dfc
Merge remote-tracking branch 'upstream/main' into go-router/don-t-log…
ValentinVignal Feb 14, 2024
bd14b32
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Feb 26, 2024
16cad7a
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Feb 28, 2024
6031c24
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Mar 4, 2024
64edef1
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Mar 12, 2024
3f319d1
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Mar 14, 2024
e513c22
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Mar 15, 2024
998a25e
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Mar 16, 2024
a824c90
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Mar 18, 2024
df0ea66
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Mar 21, 2024
b2cac28
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Mar 24, 2024
1f06814
Merge remote-tracking branch 'upstream/main' into go-router/don-t-log…
ValentinVignal Apr 1, 2024
8401b64
Merge remote-tracking branch 'upstream/main' into go-router/don-t-log…
ValentinVignal Apr 9, 2024
5e65ecb
fix: Remove bad batch
ValentinVignal Apr 9, 2024
19452e7
Merge remote-tracking branch 'upstream/main' into go-router/don-t-log…
ValentinVignal Apr 12, 2024
82ee0bc
Merge remote-tracking branch 'upstream/main' into go-router/don-t-log…
ValentinVignal Apr 13, 2024
55e6730
refactor: Add mockable developerLog method
ValentinVignal Apr 13, 2024
213841f
test: Update the tests
ValentinVignal Apr 13, 2024
9b24e7a
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Apr 16, 2024
9f9ef3c
Merge branch 'main' into go-router/don-t-log-if-hierarchical-logging-…
ValentinVignal Apr 18, 2024
bb86864
Merge remote-tracking branch 'upstream/main' into go-router/don-t-log…
ValentinVignal Apr 25, 2024
b86d0cb
Merge branch 'go-router-builder/add-on-exit' into go-router/don-t-log…
ValentinVignal Apr 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 13.2.5

- Fixes unwanted logs when `hierarchicalLoggingEnabled` was set to `true`.

## 13.2.4

- Updates examples to use uri.path instead of uri.toString() for accessing the current location.
Expand Down
34 changes: 23 additions & 11 deletions packages/go_router/lib/src/logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ StreamSubscription<LogRecord>? _subscription;
void setLogging({bool enabled = false}) {
_subscription?.cancel();
_enabled = enabled;
if (!enabled) {
if (!enabled || hierarchicalLoggingEnabled) {
return;
}

Expand All @@ -47,16 +47,28 @@ void setLogging({bool enabled = false}) {
),
);
} else {
developer.log(
e.message,
time: e.time,
sequenceNumber: e.sequenceNumber,
level: e.level.value,
name: e.loggerName,
zone: e.zone,
error: e.error,
stackTrace: e.stackTrace,
);
_developerLogFunction(e);
}
});
}

void _developerLog(LogRecord record) {
developer.log(
record.message,
time: record.time,
sequenceNumber: record.sequenceNumber,
level: record.level.value,
name: record.loggerName,
zone: record.zone,
error: record.error,
stackTrace: record.stackTrace,
);
}

/// A function that can be set during test to mock the developer log function.
@visibleForTesting
void Function(LogRecord)? testDeveloperLog;

/// The function used to log messages.
void Function(LogRecord) get _developerLogFunction =>
testDeveloperLog ?? _developerLog;
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 13.2.4
version: 13.2.5
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down
51 changes: 50 additions & 1 deletion packages/go_router/test/logging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import 'package:go_router/src/logging.dart';
import 'package:logging/logging.dart';

void main() {
tearDown(() {
// Reset the logging state
hierarchicalLoggingEnabled = false;

// Reset the developer log function.
testDeveloperLog = null;
});
test('setLogging does not clear listeners', () {
final StreamSubscription<LogRecord> subscription = logger.onRecord.listen(
expectAsync1<void, LogRecord>((LogRecord r) {}, count: 2),
Expand All @@ -26,6 +33,7 @@ void main() {
testWidgets(
'It should not log anything the if debugLogDiagnostics is false',
(WidgetTester tester) async {
testDeveloperLog = expectAsync1((LogRecord data) {}, count: 0);
final StreamSubscription<LogRecord> subscription =
Logger.root.onRecord.listen(
expectAsync1((LogRecord data) {}, count: 0),
Expand All @@ -43,8 +51,48 @@ void main() {
);

testWidgets(
'It should not log the known routes and the initial route if debugLogDiagnostics is true',
'It should log the known routes and the initial route if debugLogDiagnostics is true',
(WidgetTester tester) async {
testDeveloperLog = expectAsync1(
(LogRecord data) {},
count: 2,
reason: 'Go router should log the 2 events',
);
final List<String> logs = <String>[];
Logger.root.onRecord.listen(
(LogRecord event) => logs.add(event.message),
);
GoRouter(
debugLogDiagnostics: true,
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (_, GoRouterState state) => const Text('home'),
),
],
);

expect(
logs,
const <String>[
'Full paths for routes:\n => /\n',
'setting initial location null'
],
reason: 'Go router should have sent the 2 events to the logger',
);
},
);

testWidgets(
'Go router should not log itself the known routes but send the events to the logger when hierarchicalLoggingEnabled is true',
(WidgetTester tester) async {
testDeveloperLog = expectAsync1(
(LogRecord data) {},
count: 0,
reason: 'Go router should log the events itself',
);
hierarchicalLoggingEnabled = true;

final List<String> logs = <String>[];
Logger.root.onRecord.listen(
(LogRecord event) => logs.add(event.message),
Expand All @@ -65,6 +113,7 @@ void main() {
'Full paths for routes:\n => /\n',
'setting initial location null'
],
reason: 'Go router should have sent the 2 events to the logger',
);
},
);
Expand Down