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

fix: add disableTestsConsoleOutput option #27

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 35 additions & 7 deletions lib/src/generators/logging/logger_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class LoggerBuilder with StringBufferUtils {
LoggerBuilder({required this.loggerConfig});

LoggerBuilder addImports() {
write(loggerClassPrefex);
final withTestsLoggerImportsInPlace = loggerClassPrefex.replaceFirst(
testLoggerImports,
loggerConfig.disableTestsConsoleOutput ? '''import 'dart:io';''' : '',
);
write(withTestsLoggerImportsInPlace);

writeLine();

Expand All @@ -32,15 +36,39 @@ class LoggerBuilder with StringBufferUtils {

LoggerBuilder addLoggerNameAndOutputs() {
final withHelperNameInPlace = loggerClassNameAndOutputs.replaceFirst(
logHelperNameKey, loggerConfig.logHelperName);
logHelperNameKey,
loggerConfig.logHelperName,
);

String withConditionalLoggerInPlace = withHelperNameInPlace.replaceFirst(
disableConsoleOutputInRelease,
loggerConfig.disableReleaseConsoleOutput ? 'if(!kReleaseMode)' : '');
String withTestVarsLoggerInPlace = withHelperNameInPlace.replaceFirst(
disableConsoleOutputInTest,
loggerConfig.disableTestsConsoleOutput
? '''
const kForceConsoleOutput = bool.fromEnvironment('FORCE_CONSOLE_OUTPUT');
const kIntegrationTestMode = bool.fromEnvironment('INTEGRATION_TEST_MODE');
final kUnitTestMode = Platform.environment.containsKey('FLUTTER_TEST');
final kTestMode = kIntegrationTestMode || kUnitTestMode;
'''
: '',
);

String withConditionalLoggerInPlace =
withTestVarsLoggerInPlace.replaceFirst(
disableConsoleOutputInRelease,
loggerConfig.disableReleaseConsoleOutput &&
loggerConfig.disableTestsConsoleOutput
? 'if ((!kReleaseMode && !kTestMode) || kForceConsoleOutput)'
: loggerConfig.disableReleaseConsoleOutput
? 'if (!kReleaseMode)'
: loggerConfig.disableTestsConsoleOutput
? 'if (!kTestMode || kForceConsoleOutput)'
: '',
);

String loggerOutputsInPlace = withConditionalLoggerInPlace.replaceFirst(
multipleLoggerOutput,
loggerConfig.loggerOutputs.addCheckForReleaseModeToEachLogger);
multipleLoggerOutput,
loggerConfig.loggerOutputs.addCheckForReleaseModeToEachLogger,
);

write(loggerOutputsInPlace);

Expand Down
4 changes: 4 additions & 0 deletions lib/src/generators/logging/logger_class_content.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const String logHelperNameKey = 'logHelperName';
const String multiLoggerImports = 'MultiLoggerImport';
const String testLoggerImports = 'testLoggerImport';
const String multipleLoggerOutput = 'MultiLoggerList';
const String disableConsoleOutputInRelease = 'MultiLoggerList';
const String disableConsoleOutputInTest = 'MultiLoggerList';

const String loggerClassPrefex = '''
// ignore_for_file: avoid_print, depend_on_referenced_packages
$testLoggerImports

/// Maybe this should be generated for the user as well?
///
Expand Down Expand Up @@ -149,6 +152,7 @@ Logger $logHelperNameKey(
List<String> exludeLogsFromClasses = const [],
String? showOnlyClass,
}) {
$disableConsoleOutputInTest
return Logger(
printer: SimpleLogPrinter(
className,
Expand Down
11 changes: 8 additions & 3 deletions lib/src/generators/logging/logger_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ class LoggerConfig {
final Set<String> imports;
final List<String> loggerOutputs;

/// When set to true console logs will not be printed in release mode
/// When set to true, console logs will not be printed in release mode
/// Default is true
final bool disableReleaseConsoleOutput;
// Future
// final bool enableGoogleCloudLogging;

/// When set to true, console logs will not be printed while running unit
/// tests or integration tests
///
/// Default is false
final bool disableTestsConsoleOutput;

LoggerConfig({
this.imports = const {},
this.loggerOutputs = const [],
this.logHelperName = 'getLogger',
this.disableReleaseConsoleOutput = true,
this.disableTestsConsoleOutput = false,
});
}
10 changes: 8 additions & 2 deletions lib/src/generators/logging/logger_config_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import 'package:source_gen/source_gen.dart';
import 'package:stacked_generator/import_resolver.dart';
import 'package:stacked_generator/src/generators/logging/logger_config.dart';

/// Reolves the [LoggerConfig] and returns the object if it's supplied
/// Resolves the [LoggerConfig] and returns the object if it's supplied
class LoggerConfigResolver {
Future<LoggerConfig?> resolve(
ConstantReader stackedApp, ImportResolver importResolver) async {
ConstantReader stackedApp,
ImportResolver importResolver,
) async {
final loggerReader = stackedApp.peek('logger');
final multiLogger = loggerReader?.peek('loggerOutputs')?.listValue;
final logHelperName =
Expand All @@ -16,6 +18,9 @@ class LoggerConfigResolver {
final disableReleaseConsoleOutput =
loggerReader?.peek('disableReleaseConsoleOutput')?.boolValue ?? true;

final disableTestsConsoleOutput =
loggerReader?.peek('disableTestsConsoleOutput')?.boolValue ?? false;

if (loggerReader != null) {
return LoggerConfig(
logHelperName: logHelperName,
Expand All @@ -25,6 +30,7 @@ class LoggerConfigResolver {
),
loggerOutputs: _resolveMultiLogger(multiLogger),
disableReleaseConsoleOutput: disableReleaseConsoleOutput,
disableTestsConsoleOutput: disableTestsConsoleOutput,
);
}

Expand Down
Loading
Loading