diff --git a/packages/serverpod/lib/src/server/log_manager/log_manager.dart b/packages/serverpod/lib/src/server/log_manager/log_manager.dart index c7c557a9d6..d7d5a444bb 100644 --- a/packages/serverpod/lib/src/server/log_manager/log_manager.dart +++ b/packages/serverpod/lib/src/server/log_manager/log_manager.dart @@ -244,7 +244,7 @@ class SessionLogManager { } catch (exception, stackTrace) { stderr .writeln('${DateTime.now().toUtc()} FAILED TO LOG STREAMING $type'); - stderr.write('ENDPOINT: ${_endpointForSession(session)}'); + stderr.write('ENDPOINT: ${session.endpointName}'); stderr.writeln('CALL error: $exception'); stderr.writeln('$stackTrace'); } @@ -283,8 +283,8 @@ class SessionLogManager { serverId: _serverId, time: now, touched: now, - endpoint: _endpointForSession(session), - method: _methodForSession(session), + endpoint: session.endpointName, + method: session.methodName, isOpen: true, ); @@ -343,8 +343,8 @@ class SessionLogManager { serverId: _serverId, time: now, touched: now, - endpoint: _endpointForSession(session), - method: _methodForSession(session), + endpoint: session.endpointName, + method: session.methodName, duration: duration.inMicroseconds / 1000000.0, numQueries: cachedEntry.numQueries, slow: isSlow, @@ -365,9 +365,9 @@ class SessionLogManager { } } catch (e, logStackTrace) { stderr.writeln('${DateTime.now().toUtc()} FAILED TO LOG SESSION'); - if (_methodForSession(session) != null) { + if (session.methodName != null) { stderr.writeln( - 'CALL: ${_endpointForSession(session)}.${_methodForSession(session)} duration: ${duration.inMilliseconds}ms numQueries: ${cachedEntry.queries.length} authenticatedUser: $authenticatedUserId'); + 'CALL: ${session.endpointName}.${session.methodName} duration: ${duration.inMilliseconds}ms numQueries: ${cachedEntry.queries.length} authenticatedUser: $authenticatedUserId'); } stderr.writeln('CALL error: $exception'); stderr.writeln('$logStackTrace'); @@ -419,26 +419,3 @@ class LogManager { return logEntry; } } - -String? _methodForSession(Session session) { - var localSession = session; - - if (localSession is MethodCallSession) return localSession.methodName; - if (localSession is MethodStreamSession) return localSession.methodName; - if (localSession is FutureCallSession) return localSession.futureCallName; - if (localSession is InternalSession) return null; - if (localSession is StreamingSession) return null; - - throw Exception('Unknown session type: $session'); -} - -String _endpointForSession(Session session) { - var localSession = session; - if (localSession is MethodCallSession) return localSession.endpointName; - if (localSession is MethodStreamSession) return localSession.endpointName; - if (localSession is FutureCallSession) return 'FutureCallSession'; - if (localSession is InternalSession) return 'InternalSession'; - if (localSession is StreamingSession) return 'StreamingSession'; - - throw Exception('Unknown session type: $session'); -} diff --git a/packages/serverpod/lib/src/server/log_manager/log_settings.dart b/packages/serverpod/lib/src/server/log_manager/log_settings.dart index 884d3740e0..b13689b978 100644 --- a/packages/serverpod/lib/src/server/log_manager/log_settings.dart +++ b/packages/serverpod/lib/src/server/log_manager/log_settings.dart @@ -22,71 +22,19 @@ class LogSettingsManager { } } - /// Gets the log settings for a [MethodCallSession]. - LogSettings _getLogSettingsForMethodCallSession( - String endpoint, - String method, - ) { - var settings = _methodOverrides['$endpoint.$method']; - if (settings != null) return settings; - - settings = _endpointOverrides[endpoint]; - if (settings != null) return settings; - - return _runtimeSettings.logSettings; - } - - /// Gets the log settings for a [InternalSession]. - LogSettings _getLogSettingsForInternalSession() { - return _runtimeSettings.logSettings; - } - - /// Gets the log settings for a [StreamingSession]. - LogSettings _getLogSettingsForStreamingSession({required String endpoint}) { - var settings = _endpointOverrides[endpoint]; - if (settings != null) return settings; - - return _runtimeSettings.logSettings; - } - - /// Gets the log settings for a [FutureCallSession]. - LogSettings _getLogSettingsForFutureCallSession(String call) { - return _runtimeSettings.logSettings; - } - /// Returns the [LogSettings] for a specific session. LogSettings getLogSettingsForSession(Session session) { - var localSession = session; - if (localSession is MethodCallSession) { - return _getLogSettingsForMethodCallSession( - localSession.endpointName, - localSession.methodName, - ); - } + var endpoint = session.endpointName; + var method = session.methodName; - if (localSession is StreamingSession) { - return _getLogSettingsForStreamingSession( - endpoint: localSession.endpointName, - ); - } + LogSettings? settings; - if (localSession is InternalSession) { - return _getLogSettingsForInternalSession(); - } - - if (localSession is FutureCallSession) { - return _getLogSettingsForFutureCallSession( - localSession.futureCallName, - ); - } + if (method != null) settings = _methodOverrides['$endpoint.$method']; + if (settings != null) return settings; - if (localSession is MethodStreamSession) { - return _getLogSettingsForMethodCallSession( - localSession.endpointName, - localSession.methodName, - ); - } + settings = _endpointOverrides[endpoint]; + if (settings != null) return settings; - throw Exception('Unknown session type: $session'); + return _runtimeSettings.logSettings; } } diff --git a/packages/serverpod/lib/src/server/session.dart b/packages/serverpod/lib/src/server/session.dart index 90255920f1..27bb8530b9 100644 --- a/packages/serverpod/lib/src/server/session.dart +++ b/packages/serverpod/lib/src/server/session.dart @@ -98,6 +98,12 @@ abstract class Session { late final SessionLogManager? _logManager; + /// Endpoint that triggered this session. + final String endpointName; + + /// Method that triggered this session, if any. + final String? methodName; + /// Creates a new session. This is typically done internally by the [Server]. Session({ UuidValue? sessionId, @@ -106,6 +112,8 @@ abstract class Session { HttpRequest? httpRequest, WebSocket? webSocket, required this.enableLogging, + required this.endpointName, + this.methodName, }) : _authenticationKey = authenticationKey, sessionId = sessionId ?? const Uuid().v4obj() { _startTime = DateTime.now(); @@ -232,7 +240,7 @@ class InternalSession extends Session { InternalSession({ required super.server, super.enableLogging = true, - }); + }) : super(endpointName: 'InternalSession'); } /// When a call is made to the [Server] a [MethodCallSession] object is created. @@ -249,10 +257,16 @@ class MethodCallSession extends Session { late final Map queryParameters; /// The name of the called [Endpoint]. - late final String endpointName; + late final String _endpointName; + + @override + String get endpointName => _endpointName; /// The name of the method that is being called. - late final String methodName; + late final String _methodName; + + @override + String get methodName => _methodName; /// The [HttpRequest] associated with the call. final HttpRequest httpRequest; @@ -266,7 +280,7 @@ class MethodCallSession extends Session { required this.httpRequest, String? authenticationKey, super.enableLogging = true, - }) { + }) : super(endpointName: 'MethodCall') { // Read query parameters var queryParameters = {}; if (body != '' && body != 'null') { @@ -280,16 +294,16 @@ class MethodCallSession extends Session { if (path.contains('/')) { // Using the new path format (for OpenAPI) var pathComponents = path.split('/'); - endpointName = pathComponents[0]; - methodName = pathComponents[1]; + _endpointName = pathComponents[0]; + _methodName = pathComponents[1]; } else { // Using the standard format with query parameters - endpointName = path; + _endpointName = path; var methodName = queryParameters['method']; if (methodName == null && path == 'webserver') { - this.methodName = ''; + _methodName = ''; } else if (methodName != null) { - this.methodName = methodName; + _methodName = methodName; } else { throw FormatException( 'No method name specified in call to $endpointName', @@ -307,24 +321,24 @@ class MethodCallSession extends Session { /// associated with the current connection and provides easy access to the /// database. class MethodStreamSession extends Session { - /// The name of the method that is being called. - final String methodName; - - /// The name of the endpoint that is being called. - final String endpointName; - /// The connection id that uniquely identifies the stream. final UuidValue connectionId; + final String _methodName; + + @override + String get methodName => _methodName; + /// Creates a new [MethodStreamSession]. MethodStreamSession({ required super.server, required super.enableLogging, required super.authenticationKey, - required this.endpointName, - required this.methodName, + required super.endpointName, + required String methodName, required this.connectionId, - }); + }) : _methodName = methodName, + super(methodName: methodName); } /// When a web socket connection is opened to the [Server] a [StreamingSession] @@ -346,22 +360,27 @@ class StreamingSession extends Session { /// Set if there is an open session log. int? sessionLogId; - /// The endpoint that is currently being processed. - String endpointName; - /// The id of the current incoming message being processed. Increments by 1 /// for each message passed to an endpoint for processing. int currentMessageId = 0; + String _endpointName; + + /// The name of the endpoint that is being called. + set endpointName(String endpointName) => _endpointName = endpointName; + + @override + String get endpointName => _endpointName; + /// Creates a new [Session] for the web socket stream. StreamingSession({ required super.server, required this.uri, required this.httpRequest, required this.webSocket, - this.endpointName = 'StreamingSession', + super.endpointName = 'StreamingSession', super.enableLogging = true, - }) { + }) : _endpointName = endpointName { // Read query parameters var queryParameters = {}; queryParameters.addAll(uri.queryParameters); @@ -390,7 +409,7 @@ class FutureCallSession extends Session { required super.server, required this.futureCallName, super.enableLogging = true, - }); + }) : super(endpointName: 'FutureCall', methodName: futureCallName); } /// Collects methods for accessing cloud storage.