Skip to content

Commit 79461c2

Browse files
[go_router] Avoid logging when debugLogDiagnostics is false (#4875)
Fixes flutter/flutter#134187
1 parent 95c88f8 commit 79461c2

File tree

8 files changed

+93
-23
lines changed

8 files changed

+93
-23
lines changed

packages/go_router/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 11.1.2
2+
3+
- Fixes a bug where the known routes and initial route were logged even when `debugLogDiagnostics` was set to `false`.
4+
15
## 11.1.1
26

37
- Fixes a missing `{@end-tool}` doc directive tag for `GoRoute.name`.

packages/go_router/lib/src/builder.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -445,17 +445,17 @@ class RouteBuilder {
445445
final Element? elem = context is Element ? context : null;
446446

447447
if (elem != null && isMaterialApp(elem)) {
448-
log.info('Using MaterialApp configuration');
448+
log('Using MaterialApp configuration');
449449
_pageBuilderForAppType = pageBuilderForMaterialApp;
450450
_errorBuilderForAppType =
451451
(BuildContext c, GoRouterState s) => MaterialErrorScreen(s.error);
452452
} else if (elem != null && isCupertinoApp(elem)) {
453-
log.info('Using CupertinoApp configuration');
453+
log('Using CupertinoApp configuration');
454454
_pageBuilderForAppType = pageBuilderForCupertinoApp;
455455
_errorBuilderForAppType =
456456
(BuildContext c, GoRouterState s) => CupertinoErrorScreen(s.error);
457457
} else {
458-
log.info('Using WidgetsApp configuration');
458+
log('Using WidgetsApp configuration');
459459
_pageBuilderForAppType = pageBuilderForWidgetApp;
460460
_errorBuilderForAppType =
461461
(BuildContext c, GoRouterState s) => ErrorScreen(s.error);

packages/go_router/lib/src/configuration.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class RouteConfiguration {
3232
routes, <GlobalKey<NavigatorState>>[navigatorKey])) {
3333
assert(_debugCheckStatefulShellBranchDefaultLocations(routes));
3434
_cacheNameToPath('', routes);
35-
log.info(debugKnownRoutes());
35+
log(debugKnownRoutes());
3636
}
3737

3838
static bool _debugCheckPath(List<RouteBase> routes, bool isTopLevel) {
@@ -234,7 +234,7 @@ class RouteConfiguration {
234234
Map<String, dynamic> queryParameters = const <String, dynamic>{},
235235
}) {
236236
assert(() {
237-
log.info('getting location for name: '
237+
log('getting location for name: '
238238
'"$name"'
239239
'${pathParameters.isEmpty ? '' : ', pathParameters: $pathParameters'}'
240240
'${queryParameters.isEmpty ? '' : ', queryParameters: $queryParameters'}');
@@ -492,7 +492,7 @@ class RouteConfiguration {
492492
_addRedirect(redirectHistory, newMatch, previousLocation);
493493
return newMatch;
494494
} on GoException catch (e) {
495-
log.info('Redirection exception: ${e.message}');
495+
log('Redirection exception: ${e.message}');
496496
return _errorRouteMatchList(previousLocation, e);
497497
}
498498
}
@@ -522,7 +522,7 @@ class RouteConfiguration {
522522

523523
redirects.add(newMatch);
524524

525-
log.info('redirecting to $newMatch');
525+
log('redirecting to $newMatch');
526526
}
527527

528528
String _formatRedirectionHistory(List<RouteMatchList> redirections) {

packages/go_router/lib/src/logging.dart

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,35 @@
44

55
import 'dart:async';
66
import 'dart:developer' as developer;
7+
78
import 'package:flutter/foundation.dart';
89
import 'package:logging/logging.dart';
910

1011
/// The logger for this package.
11-
final Logger log = Logger('GoRouter');
12+
@visibleForTesting
13+
final Logger logger = Logger('GoRouter');
14+
15+
/// Whether or not the logging is enabled.
16+
bool _enabled = false;
17+
18+
/// Logs the message if logging is enabled.
19+
void log(String message) {
20+
if (_enabled) {
21+
logger.info(message);
22+
}
23+
}
1224

1325
StreamSubscription<LogRecord>? _subscription;
1426

1527
/// Forwards diagnostic messages to the dart:developer log() API.
1628
void setLogging({bool enabled = false}) {
1729
_subscription?.cancel();
30+
_enabled = enabled;
1831
if (!enabled) {
1932
return;
2033
}
2134

22-
_subscription = log.onRecord.listen((LogRecord e) {
35+
_subscription = logger.onRecord.listen((LogRecord e) {
2336
// use `dumpErrorToConsole` for severe messages to ensure that severe
2437
// exceptions are formatted consistently with other Flutter examples and
2538
// avoids printing duplicate exceptions

packages/go_router/lib/src/parser.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
8989
// TODO(chunhtai): remove this ignore and migrate the code
9090
// https://github.com/flutter/flutter/issues/124045.
9191
// ignore: deprecated_member_use
92-
log.info('No initial matches: ${routeInformation.location}');
92+
log('No initial matches: ${routeInformation.location}');
9393
}
9494

9595
return debugParserFuture = _redirect(

packages/go_router/lib/src/router.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class GoRouter implements RouterConfig<RouteMatchList> {
161161
);
162162

163163
assert(() {
164-
log.info('setting initial location $initialLocation');
164+
log('setting initial location $initialLocation');
165165
return true;
166166
}());
167167
}
@@ -338,13 +338,13 @@ class GoRouter implements RouterConfig<RouteMatchList> {
338338
/// Navigate to a URI location w/ optional query parameters, e.g.
339339
/// `/family/f2/person/p1?color=blue`
340340
void go(String location, {Object? extra}) {
341-
log.info('going to $location');
341+
log('going to $location');
342342
routeInformationProvider.go(location, extra: extra);
343343
}
344344

345345
/// Restore the RouteMatchList
346346
void restore(RouteMatchList matchList) {
347-
log.info('restoring ${matchList.uri}');
347+
log('restoring ${matchList.uri}');
348348
routeInformationProvider.restore(
349349
matchList.uri.toString(),
350350
matchList: matchList,
@@ -376,7 +376,7 @@ class GoRouter implements RouterConfig<RouteMatchList> {
376376
/// it as the same page. The page key will be reused. This will preserve the
377377
/// state and not run any page animation.
378378
Future<T?> push<T extends Object?>(String location, {Object? extra}) async {
379-
log.info('pushing $location');
379+
log('pushing $location');
380380
return routeInformationProvider.push<T>(
381381
location,
382382
base: routerDelegate.currentConfiguration,
@@ -409,7 +409,7 @@ class GoRouter implements RouterConfig<RouteMatchList> {
409409
/// state and not run any page animation.
410410
Future<T?> pushReplacement<T extends Object?>(String location,
411411
{Object? extra}) {
412-
log.info('pushReplacement $location');
412+
log('pushReplacement $location');
413413
return routeInformationProvider.pushReplacement<T>(
414414
location,
415415
base: routerDelegate.currentConfiguration,
@@ -448,7 +448,7 @@ class GoRouter implements RouterConfig<RouteMatchList> {
448448
/// * [pushReplacement] which replaces the top-most page of the page stack but
449449
/// always uses a new page key.
450450
Future<T?> replace<T>(String location, {Object? extra}) {
451-
log.info('replace $location');
451+
log('replace $location');
452452
return routeInformationProvider.replace<T>(
453453
location,
454454
base: routerDelegate.currentConfiguration,
@@ -486,7 +486,7 @@ class GoRouter implements RouterConfig<RouteMatchList> {
486486
/// of any GoRoute under it.
487487
void pop<T extends Object?>([T? result]) {
488488
assert(() {
489-
log.info('popping ${routerDelegate.currentConfiguration.uri}');
489+
log('popping ${routerDelegate.currentConfiguration.uri}');
490490
return true;
491491
}());
492492
routerDelegate.pop<T>(result);
@@ -495,7 +495,7 @@ class GoRouter implements RouterConfig<RouteMatchList> {
495495
/// Refresh the route.
496496
void refresh() {
497497
assert(() {
498-
log.info('refreshing ${routerDelegate.currentConfiguration.uri}');
498+
log('refreshing ${routerDelegate.currentConfiguration.uri}');
499499
return true;
500500
}());
501501
routeInformationProvider.notifyListeners();

packages/go_router/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 11.1.1
4+
version: 11.1.2
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/logging_test.dart

+57-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,70 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:async';
6+
7+
import 'package:flutter/material.dart';
58
import 'package:flutter_test/flutter_test.dart';
9+
import 'package:go_router/go_router.dart';
610
import 'package:go_router/src/logging.dart';
711
import 'package:logging/logging.dart';
812

913
void main() {
1014
test('setLogging does not clear listeners', () {
11-
log.onRecord
12-
.listen(expectAsync1<void, LogRecord>((LogRecord r) {}, count: 2));
15+
final StreamSubscription<LogRecord> subscription = logger.onRecord.listen(
16+
expectAsync1<void, LogRecord>((LogRecord r) {}, count: 2),
17+
);
18+
addTearDown(subscription.cancel);
19+
1320
setLogging(enabled: true);
14-
log.info('message');
21+
logger.info('message');
1522
setLogging();
16-
log.info('message');
23+
logger.info('message');
1724
});
25+
26+
testWidgets(
27+
'It should not log anything the if debugLogDiagnostics is false',
28+
(WidgetTester tester) async {
29+
final StreamSubscription<LogRecord> subscription =
30+
Logger.root.onRecord.listen(
31+
expectAsync1((LogRecord data) {}, count: 0),
32+
);
33+
addTearDown(subscription.cancel);
34+
GoRouter(
35+
routes: <RouteBase>[
36+
GoRoute(
37+
path: '/',
38+
builder: (_, GoRouterState state) => const Text('home'),
39+
),
40+
],
41+
);
42+
},
43+
);
44+
45+
testWidgets(
46+
'It should not log the known routes and the initial route if debugLogDiagnostics is true',
47+
(WidgetTester tester) async {
48+
final List<String> logs = <String>[];
49+
Logger.root.onRecord.listen(
50+
(LogRecord event) => logs.add(event.message),
51+
);
52+
GoRouter(
53+
debugLogDiagnostics: true,
54+
routes: <RouteBase>[
55+
GoRoute(
56+
path: '/',
57+
builder: (_, GoRouterState state) => const Text('home'),
58+
),
59+
],
60+
);
61+
62+
expect(
63+
logs,
64+
const <String>[
65+
'Full paths for routes:\n => /\n',
66+
'setting initial location null'
67+
],
68+
);
69+
},
70+
);
1871
}

0 commit comments

Comments
 (0)