diff --git a/packages/serverpod/lib/server.dart b/packages/serverpod/lib/server.dart index 064c603281..a0775a5632 100644 --- a/packages/serverpod/lib/server.dart +++ b/packages/serverpod/lib/server.dart @@ -12,3 +12,4 @@ export 'src/server/run_mode.dart'; export 'src/server/server.dart'; export 'src/server/serverpod.dart' hide ServerpodInternalMethods; export 'src/server/session.dart' hide SessionInternalMethods; +export 'src/server/command_line_args.dart' show ServerpodLoggingMode; diff --git a/packages/serverpod/lib/src/server/session.dart b/packages/serverpod/lib/src/server/session.dart index 234f9750c7..5684306ddb 100644 --- a/packages/serverpod/lib/src/server/session.dart +++ b/packages/serverpod/lib/src/server/session.dart @@ -236,7 +236,7 @@ abstract class Session implements DatabaseAccessor { try { if (_logManager == null && error != null) { - serverpod.logVerbose(error); + serverpod.logVerbose(error.toString()); if (stackTrace != null) { serverpod.logVerbose(stackTrace.toString()); } diff --git a/packages/serverpod_test/lib/src/test_serverpod.dart b/packages/serverpod_test/lib/src/test_serverpod.dart index bd382cd725..35d5fc8984 100644 --- a/packages/serverpod_test/lib/src/test_serverpod.dart +++ b/packages/serverpod_test/lib/src/test_serverpod.dart @@ -76,11 +76,17 @@ class InternalServerpodSession extends Session { } } -List _getServerpodStartUpArgs(String? runMode, bool? applyMigrations) => +List _getServerpodStartUpArgs({ + String? runMode, + bool? applyMigrations, + ServerpodLoggingMode? loggingMode, +}) => [ '-m', runMode ?? ServerpodRunMode.test, if (applyMigrations ?? true) '--apply-migrations', + '--logging', + loggingMode?.name ?? ServerpodLoggingMode.normal.name, ]; /// A facade for the real Serverpod instance. @@ -95,12 +101,13 @@ class TestServerpod { /// Creates a new test serverpod instance. TestServerpod({ - bool? applyMigrations, + required bool? applyMigrations, required EndpointDispatch endpoints, required SerializationManagerServer serializationManager, required this.isDatabaseEnabled, required this.testEndpoints, - String? runMode, + required ServerpodLoggingMode? serverpodLoggingMode, + required String? runMode, }) { // Ignore output from the Serverpod constructor to avoid spamming the console. // Should be changed when a proper logger is implemented. @@ -109,8 +116,9 @@ class TestServerpod { () { _serverpod = Serverpod( _getServerpodStartUpArgs( - runMode, - applyMigrations, + runMode: runMode, + applyMigrations: applyMigrations, + loggingMode: serverpodLoggingMode, ), serializationManager, endpoints, diff --git a/tests/serverpod_test_module/serverpod_test_module_server/test/integration/test_tools/serverpod_test_tools.dart b/tests/serverpod_test_module/serverpod_test_module_server/test/integration/test_tools/serverpod_test_tools.dart index b64fe421b5..68e417b20b 100644 --- a/tests/serverpod_test_module/serverpod_test_module_server/test/integration/test_tools/serverpod_test_tools.dart +++ b/tests/serverpod_test_module/serverpod_test_module_server/test/integration/test_tools/serverpod_test_tools.dart @@ -26,6 +26,7 @@ void withServerpod( _i1.TestClosure testClosure, { String? runMode, bool? enableSessionLogging, + _i2.ServerpodLoggingMode? serverpodLoggingMode, List? testGroupTagsOverride, _i1.RollbackDatabase? rollbackDatabase, bool? applyMigrations, @@ -39,6 +40,7 @@ void withServerpod( runMode: runMode, applyMigrations: applyMigrations, isDatabaseEnabled: true, + serverpodLoggingMode: serverpodLoggingMode, ), maybeRollbackDatabase: rollbackDatabase, maybeEnableSessionLogging: enableSessionLogging, diff --git a/tests/serverpod_test_server/test_integration/logging/session_close_test.dart b/tests/serverpod_test_server/test_integration/logging/session_close_test.dart new file mode 100644 index 0000000000..d2560bcf81 --- /dev/null +++ b/tests/serverpod_test_server/test_integration/logging/session_close_test.dart @@ -0,0 +1,58 @@ +import 'dart:io'; + +import 'package:serverpod/serverpod.dart'; +import 'package:serverpod_test_server/test_util/mock_stdout.dart'; +import 'package:test/test.dart'; + +import '../test_tools/serverpod_test_tools.dart'; + +class TestError extends Error { + @override + String toString() { + return 'TestError'; + } +} + +void main() { + withServerpod( + 'Given an existing session with session logging disabled', + enableSessionLogging: false, + serverpodLoggingMode: ServerpodLoggingMode.verbose, + (sessionBuilder, endpoints) { + late MockStdout record; + + setUp(() async { + record = MockStdout(); + }); + + test( + 'when closing session with error, the error should be logged and the session should be closed', + () async { + await IOOverrides.runZoned( + () async { + await sessionBuilder.build().close(error: TestError()); + }, + stdout: () => record, + ); + + expect(record.output, contains('TestError')); + }); + + test( + 'when closing session with error and stackTrace, the error should be logged and the session should be closed', + () async { + await IOOverrides.runZoned( + () async { + await sessionBuilder.build().close( + error: TestError(), + stackTrace: StackTrace.fromString('TestStackTrace')); + }, + stdout: () => record, + ); + + expect(record.output, contains('TestError')); + expect(record.output, contains('TestStackTrace')); + }); + }, + ); +} diff --git a/tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart b/tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart index b6db830d5b..e5428d75df 100644 --- a/tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart +++ b/tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart @@ -50,6 +50,7 @@ void withServerpod( _i1.TestClosure testClosure, { String? runMode, bool? enableSessionLogging, + _i2.ServerpodLoggingMode? serverpodLoggingMode, List? testGroupTagsOverride, _i1.RollbackDatabase? rollbackDatabase, bool? applyMigrations, @@ -63,6 +64,7 @@ void withServerpod( runMode: runMode, applyMigrations: applyMigrations, isDatabaseEnabled: true, + serverpodLoggingMode: serverpodLoggingMode, ), maybeRollbackDatabase: rollbackDatabase, maybeEnableSessionLogging: enableSessionLogging, diff --git a/tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart b/tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart index c7e9a762be..e364ba9f1b 100644 --- a/tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart +++ b/tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart @@ -404,6 +404,10 @@ class ServerTestToolsGenerator { ..name = 'enableSessionLogging' ..named = true ..type = refer('bool?')), + Parameter((p) => p + ..name = 'serverpodLoggingMode' + ..named = true + ..type = refer('ServerpodLoggingMode?', serverpodUrl(true))), Parameter((p) => p ..name = 'testGroupTagsOverride' ..named = true @@ -439,6 +443,7 @@ class ServerTestToolsGenerator { 'isDatabaseEnabled': literalBool( config.isFeatureEnabled(ServerpodFeature.database), ), + 'serverpodLoggingMode': refer('serverpodLoggingMode'), }, ), ], diff --git a/tools/serverpod_cli/test/generator/dart/server_code_generator/test_tools_test.dart b/tools/serverpod_cli/test/generator/dart/server_code_generator/test_tools_test.dart index ae12e419b5..89886f2710 100644 --- a/tools/serverpod_cli/test/generator/dart/server_code_generator/test_tools_test.dart +++ b/tools/serverpod_cli/test/generator/dart/server_code_generator/test_tools_test.dart @@ -71,6 +71,7 @@ void main() { r' _i\d\.TestClosure testClosure, \{\n' r' String\? runMode,\n' r' bool\? enableSessionLogging,\n' + r' _i\d\.ServerpodLoggingMode\? serverpodLoggingMode,\n' r' List\? testGroupTagsOverride,\n' r' _i\d\.RollbackDatabase\? rollbackDatabase,\n' r' bool\? applyMigrations,\n' @@ -143,6 +144,7 @@ void main() { r' _i\d\.TestClosure testClosure, \{\n' r' String\? runMode,\n' r' bool\? enableSessionLogging,\n' + r' _i\d\.ServerpodLoggingMode\? serverpodLoggingMode,\n' r' List\? testGroupTagsOverride,\n' r'\}\)', )); @@ -188,6 +190,7 @@ void main() { r' _i\d\.TestClosure testClosure, \{\n' r' String\? runMode,\n' r' bool\? enableSessionLogging,\n' + r' _i\d\.ServerpodLoggingMode\? serverpodLoggingMode,\n' r' List\? testGroupTagsOverride,\n' r' _i\d\.RollbackDatabase\? rollbackDatabase,\n' r' bool\? applyMigrations,\n' @@ -268,6 +271,7 @@ void main() { r' _i\d\.TestClosure testClosure, \{\n' r' String\? runMode,\n' r' bool\? enableSessionLogging,\n' + r' _i\d\.ServerpodLoggingMode\? serverpodLoggingMode,\n' r' List\? testGroupTagsOverride,\n' r' _i\d\.RollbackDatabase\? rollbackDatabase,\n' r' bool\? applyMigrations,\n'