From ff65e13c7556ed04a2c6663d879caf1c442cec35 Mon Sep 17 00:00:00 2001 From: thomashii Date: Sun, 20 Feb 2022 09:48:12 +0800 Subject: [PATCH] Added default logging --- .../angel_container_generator/CHANGELOG.md | 12 ++++ packages/framework/lib/src/core/driver.dart | 72 ++++++------------- .../lib/src/core/response_context.dart | 5 +- packages/framework/lib/src/core/routable.dart | 5 ++ packages/framework/lib/src/core/server.dart | 64 ++++++++--------- .../framework/lib/src/http/angel_http.dart | 5 -- .../lib/src/http/http_request_context.dart | 42 ++--------- .../lib/src/http/http_response_context.dart | 8 +-- .../framework/lib/src/http2/angel_http2.dart | 4 +- .../lib/src/http2/http2_response_context.dart | 8 +-- .../framework/test/encoders_buffer_test.dart | 1 - packages/framework/test/streaming_test.dart | 3 +- 12 files changed, 85 insertions(+), 144 deletions(-) diff --git a/packages/container/angel_container_generator/CHANGELOG.md b/packages/container/angel_container_generator/CHANGELOG.md index d46070a6..05db9bf7 100644 --- a/packages/container/angel_container_generator/CHANGELOG.md +++ b/packages/container/angel_container_generator/CHANGELOG.md @@ -1,5 +1,17 @@ # Change Log +## 6.0.0 + +* Updated to min SDK 2.15.x + +## 5.0.0 + +* Skipped release + +## 4.0.0 + +* Skipped release + ## 3.0.1 * Updated `package:angel3_container` diff --git a/packages/framework/lib/src/core/driver.dart b/packages/framework/lib/src/core/driver.dart index 9ebdc003..470532d2 100644 --- a/packages/framework/lib/src/core/driver.dart +++ b/packages/framework/lib/src/core/driver.dart @@ -1,6 +1,6 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:io' show stderr, Cookie; +import 'dart:io' show Cookie; import 'package:angel3_http_exception/angel3_http_exception.dart'; import 'package:angel3_route/angel3_route.dart'; import 'package:belatuk_combinator/belatuk_combinator.dart'; @@ -20,10 +20,6 @@ abstract class Driver< final Angel app; final bool useZone; bool _closed = false; - late Server _server; - - // TODO: Ugly fix - bool isServerInitialised = false; StreamSubscription? _sub; //final log = Logger('Driver'); @@ -37,14 +33,7 @@ abstract class Driver< Uri get uri; /// The native server running this instance. - Server? get server { - // TODO: Ugly fix - if (isServerInitialised) { - return _server; - } else { - return null; - } - } + Server? server; Future generateServer(address, int port) => serverGenerator(address, port); @@ -53,24 +42,21 @@ abstract class Driver< Future startServer([address, int port = 0]) { var host = address ?? '127.0.0.1'; return generateServer(host, port).then((server) { - _server = server; - - // TODO: Ugly fix - isServerInitialised = true; + this.server = server; return Future.wait(app.startupHooks.map(app.configure)).then((_) { app.optimizeForProduction(); - _sub = server.listen((request) { + _sub = this.server?.listen((request) { var stream = createResponseStreamFromRawRequest(request); stream.listen((response) { // TODO: To be revisited handleRawRequest(request, response); }); }); - return Future.value(_server); + return Future.value(this.server!); }); }).catchError((error) { - app.logger?.severe('Failed to create server', error); + app.logger.severe('Failed to create server', error); throw ArgumentError('[Driver]Failed to create server'); }); } @@ -163,7 +149,7 @@ abstract class Driver< ..registerSingleton?>(tuple.item3) ..registerSingleton(tuple.item3); - if (!app.environment.isProduction && app.logger != null) { + if (!app.environment.isProduction) { req.container?.registerSingleton(Stopwatch()..start()); } @@ -204,22 +190,16 @@ abstract class Driver< message: ee?.toString() ?? '500 Internal Server Error'); } - if (app.logger != null) { - var error = e.error ?? e; - var trace = Trace.from(StackTrace.current).terse; - app.logger?.severe(e.message, error, trace); - } + var error = e.error ?? e; + var trace = Trace.from(StackTrace.current).terse; + app.logger.severe(e.message, error, trace); return handleAngelHttpException(e, st, req, res, request, response); }); } else { var zoneSpec = ZoneSpecification( print: (self, parent, zone, line) { - if (app.logger != null) { - app.logger?.info(line); - } else { - parent.print(zone, line); - } + app.logger.info(line); }, handleUncaughtError: (self, parent, zone, error, stackTrace) { var trace = Trace.from(stackTrace).terse; @@ -237,9 +217,7 @@ abstract class Driver< stackTrace: stackTrace, message: error.toString()); } - if (app.logger != null) { - app.logger?.severe(e.message, error, trace); - } + app.logger.severe(e.message, error, trace); return handleAngelHttpException( e, trace, req, res, request, response); @@ -248,23 +226,15 @@ abstract class Driver< closeResponse(response); // Ideally, we won't be in a position where an absolutely fatal error occurs, // but if so, we'll need to log it. - if (app.logger != null) { - app.logger!.severe( - 'Fatal error occurred when processing $uri.', e, trace); - } else { - stderr - ..writeln('Fatal error occurred when processing ' - '${req.uri}:') - ..writeln(e) - ..writeln(trace); - } + app.logger.severe( + 'Fatal error occurred when processing $uri.', e, trace); }); }, ); var zone = Zone.current.fork(specification: zoneSpec); - req.container!.registerSingleton(zone); - req.container!.registerSingleton(zoneSpec); + req.container?.registerSingleton(zone); + req.container?.registerSingleton(zoneSpec); // If a synchronous error is thrown, it's not caught by `zone.run`, // so use a try/catch, and recover when need be. @@ -291,12 +261,12 @@ abstract class Driver< {bool ignoreFinalizers = false}) { if (req == null || res == null) { try { - app.logger?.severe('500 Internal Server Error', e, st); + app.logger.severe('500 Internal Server Error', e, st); setStatusCode(response, 500); writeStringToResponse(response, '500 Internal Server Error'); closeResponse(response); } catch (e) { - app.logger?.severe('500 Internal Server Error', e); + app.logger.severe('500 Internal Server Error', e); } return Future.value(); } @@ -322,11 +292,9 @@ abstract class Driver< ResponseContext res, {bool ignoreFinalizers = false}) { Future _cleanup(_) { - if (!app.environment.isProduction && - app.logger != null && - req.container!.has()) { + if (!app.environment.isProduction && req.container!.has()) { var sw = req.container!.make(); - app.logger?.info( + app.logger.info( "${res.statusCode} ${req.method} ${req.uri} (${sw.elapsedMilliseconds} ms)"); } return req.close(); diff --git a/packages/framework/lib/src/core/response_context.dart b/packages/framework/lib/src/core/response_context.dart index 25d97c5d..6fbdfd95 100644 --- a/packages/framework/lib/src/core/response_context.dart +++ b/packages/framework/lib/src/core/response_context.dart @@ -9,7 +9,6 @@ import 'dart:typed_data'; import 'package:angel3_route/angel3_route.dart'; import 'package:file/file.dart'; import 'package:http_parser/http_parser.dart'; -import 'package:logging/logging.dart'; import 'package:mime/mime.dart'; import 'controller.dart'; @@ -25,7 +24,7 @@ abstract class ResponseContext final CaseInsensitiveMap _headers = CaseInsensitiveMap.from( {'content-type': 'text/plain', 'server': 'Angel3'}); - final log = Logger('ResponseContext'); + //final log = Logger('ResponseContext'); Completer? _done; int _statusCode = 200; @@ -351,7 +350,7 @@ abstract class ResponseContext if (stackTrace != null) { Zone.current.handleUncaughtError(error, stackTrace); } else { - log.warning('[ResponseContext] stackTrace is null'); + app?.logger.warning('[ResponseContext] stackTrace is null'); } } } diff --git a/packages/framework/lib/src/core/routable.dart b/packages/framework/lib/src/core/routable.dart index 09e8464e..5543f1ab 100644 --- a/packages/framework/lib/src/core/routable.dart +++ b/packages/framework/lib/src/core/routable.dart @@ -46,6 +46,11 @@ RequestHandler chain(Iterable handlers) { class Routable extends Router { final Map _services = {}; final Map _serviceLookups = {}; + + /// A [Map] of application-specific data that can be accessed. + /// + /// Packages like `package:angel3_configuration` populate this map + /// for you. final Map configuration = {}; final Container _container; diff --git a/packages/framework/lib/src/core/server.dart b/packages/framework/lib/src/core/server.dart index 45d6f44f..dbd77772 100644 --- a/packages/framework/lib/src/core/server.dart +++ b/packages/framework/lib/src/core/server.dart @@ -55,6 +55,18 @@ Future _defaultErrorHandler( } } +Logger _defaultLogger() { + Logger logger = Logger('SERVER') + //..level = Level.WARNING + ..onRecord.listen((rec) { + print(rec); + if (rec.error != null) print(rec.error); + if (rec.stackTrace != null) print(rec.stackTrace); + }); + + return logger; +} + /// A powerful real-time/REST/MVC server class. class Angel extends Routable { static Future _noViewEngineConfigured(String view, [Map? data]) => @@ -99,19 +111,6 @@ class Angel extends Routable { /// A set of [Controller] objects that have been loaded into the application. Map get controllers => _controllers; - /// Now *deprecated*, in favor of [AngelEnv] and [angelEnv]. Use `app.environment.isProduction` - /// instead. - /// - /// Indicates whether the application is running in a production environment. - /// - /// The criteria for this is the `ANGEL_ENV` environment variable being set to - /// `'production'`. - /// - /// This value is memoized the first time you call it, so do not change environment - /// configuration at runtime! - //@deprecated - //bool get isProduction => environment.isProduction; - /// The [AngelEnvironment] in which the application is running. /// /// By default, it is automatically inferred. @@ -121,7 +120,7 @@ class Angel extends Routable { Angel? get parent => _parent; /// Outputs diagnostics and debug messages. - Logger? logger; + Logger logger = _defaultLogger(); /// Plug-ins to be called right before server startup. /// @@ -138,14 +137,6 @@ class Angel extends Routable { /// These will only not run if a response's `willCloseItself` is set to `true`. final List responseFinalizers = []; - /// A [Map] of application-specific data that can be accessed by any - /// piece of code that can see this [Angel] instance. - /// - /// Packages like `package:angel3_configuration` populate this map - /// for you. - @override - final Map configuration = {}; - /// A function that renders views. /// /// Called by [ResponseContext]@`render`. @@ -158,11 +149,10 @@ class Angel extends Routable { Route addRoute( String method, String path, RequestHandler handler, {Iterable middleware = const []}) { - //middleware ??= []; if (_flattened != null) { - logger?.warning( + logger.warning( 'WARNING: You added a route ($method $path) to the router, after it had been optimized.'); - logger?.warning( + logger.warning( 'This route will be ignored, and no requests will ever reach it.'); } @@ -173,9 +163,9 @@ class Angel extends Routable { SymlinkRoute mount( String path, Router router) { if (_flattened != null) { - logger?.warning( + logger.warning( 'WARNING: You added mounted a child router ($path) on the router, after it had been optimized.'); - logger?.warning( + logger.warning( 'This route will be ignored, and no requests will ever reach it.'); } @@ -202,7 +192,7 @@ class Angel extends Routable { /// /// The server will be **COMPLETELY DEFUNCT** after this operation! @override - Future close() { + Future close() { Future.forEach(services.values, (Service service) { service.close(); }); @@ -212,14 +202,13 @@ class Angel extends Routable { _preContained.clear(); handlerCache.clear(); encoders.clear(); - //_serializer = json.encode; _children.clear(); - _parent = null; - logger = null; + //_parent = null; + //logger = null; + //_flattened = null; startupHooks.clear(); shutdownHooks.clear(); responseFinalizers.clear(); - _flattened = null; return Future.value(); } @@ -312,7 +301,7 @@ class Angel extends Routable { void optimizeForProduction({bool force = false}) { if (environment.isProduction || force == true) { _flattened ??= flatten(this); - logger?.info('Angel is running in production mode.'); + logger.info('Angel is running in production mode.'); } } @@ -391,16 +380,21 @@ class Angel extends Routable { {Reflector reflector = const ThrowingReflector(errorMessage: _reflectionErrorMessage), this.environment = angelEnv, - this.logger, + Logger? logger, this.allowMethodOverrides = true, this.serializer, this.viewGenerator}) : super(reflector) { + // Override default logger + if (logger != null) { + this.logger = logger; + } + if (reflector is EmptyReflector || reflector is ThrowingReflector) { var msg = 'No `reflector` was passed to the Angel constructor, so reflection will not be available.\n' + _reflectionInfo; - logger?.warning(msg); + this.logger.warning(msg); } bootstrapContainer(); diff --git a/packages/framework/lib/src/http/angel_http.dart b/packages/framework/lib/src/http/angel_http.dart index dd389634..f56dc5e0 100644 --- a/packages/framework/lib/src/http/angel_http.dart +++ b/packages/framework/lib/src/http/angel_http.dart @@ -103,15 +103,10 @@ class AngelHttp extends Driver createResponseContext( HttpRequest request, HttpResponse response, [HttpRequestContext? correspondingRequest]) { - // TODO: Refactored to overcome NNBD migration error var context = HttpResponseContext(response, app, correspondingRequest); context.serializer = (app.serializer ?? json.encode); context.encoders.addAll(app.encoders); return Future.value(context); -// return Future.value( -// HttpResponseContext(response, app, correspondingRequest) -// ..serializer = (app.serializer ?? json.encode) -// ..encoders.addAll(app.encoders ?? {})); } @override diff --git a/packages/framework/lib/src/http/http_request_context.dart b/packages/framework/lib/src/http/http_request_context.dart index cafaa391..26797dc8 100644 --- a/packages/framework/lib/src/http/http_request_context.dart +++ b/packages/framework/lib/src/http/http_request_context.dart @@ -24,7 +24,7 @@ class HttpRequestContext extends RequestContext { @override List get cookies { - return rawRequest!.cookies; + return rawRequest?.cookies ?? []; } @override @@ -51,7 +51,7 @@ class HttpRequestContext extends RequestContext { @override String get originalMethod { - return rawRequest!.method; + return rawRequest?.method ?? ''; } @override @@ -61,17 +61,18 @@ class HttpRequestContext extends RequestContext { @override InternetAddress get remoteAddress { - return rawRequest!.connectionInfo!.remoteAddress; + return rawRequest?.connectionInfo?.remoteAddress ?? + InternetAddress("127.0.0.1"); } @override - HttpSession get session { - return rawRequest!.session; + HttpSession? get session { + return rawRequest?.session; } @override Uri get uri { - return rawRequest!.uri; + return rawRequest?.uri ?? Uri(); } /// Magically transforms an [HttpRequest] into a [RequestContext]. @@ -92,35 +93,6 @@ class HttpRequestContext extends RequestContext { ? MediaType('text', 'plain') : MediaType.parse(request.headers.contentType.toString()); ctx._override = override; - - /* - // Faster way to get path - List _path = []; - - // Go up until we reach a ? - for (int ch in request.uri.toString().codeUnits) { - if (ch != $question) - _path.add(ch); - else - break; - } - - // Remove trailing slashes - int lastSlash = -1; - - for (int i = _path.length - 1; i >= 0; i--) { - if (_path[i] == $slash) - lastSlash = i; - else - break; - } - - if (lastSlash > -1) - ctx._path = String.fromCharCodes(_path.take(lastSlash)); - else - ctx._path = String.fromCharCodes(_path); - */ - ctx._path = path; ctx._io = request; diff --git a/packages/framework/lib/src/http/http_response_context.dart b/packages/framework/lib/src/http/http_response_context.dart index e7195481..b310061d 100644 --- a/packages/framework/lib/src/http/http_response_context.dart +++ b/packages/framework/lib/src/http/http_response_context.dart @@ -13,15 +13,15 @@ class HttpResponseContext extends ResponseContext { @override final HttpResponse rawResponse; - @override - Angel? app; - LockableBytesBuilder? _buffer; final HttpRequestContext? _correspondingRequest; bool _isDetached = false, _isClosed = false, _streamInitialized = false; - HttpResponseContext(this.rawResponse, this.app, [this._correspondingRequest]); + HttpResponseContext(this.rawResponse, Angel? app, + [this._correspondingRequest]) { + this.app = app; + } @override HttpResponse detach() { diff --git a/packages/framework/lib/src/http2/angel_http2.dart b/packages/framework/lib/src/http2/angel_http2.dart index 30c79b35..67f35bd0 100644 --- a/packages/framework/lib/src/http2/angel_http2.dart +++ b/packages/framework/lib/src/http2/angel_http2.dart @@ -80,7 +80,7 @@ class AngelHttp2 extends Driver close() async { - await _artificial!.close(); + await _artificial?.close(); await _http.close(); return await super.close(); } @@ -211,7 +211,7 @@ class _AngelHttp2ServerSocket extends Stream }, onDone: _ctrl.close, onError: (e, st) { - driver.app.logger!.warning( + driver.app.logger.warning( 'HTTP/2 incoming connection failure: ', e, st as StackTrace); }, ); diff --git a/packages/framework/lib/src/http2/http2_response_context.dart b/packages/framework/lib/src/http2/http2_response_context.dart index 36c01e40..28ec3420 100644 --- a/packages/framework/lib/src/http2/http2_response_context.dart +++ b/packages/framework/lib/src/http2/http2_response_context.dart @@ -6,9 +6,6 @@ import 'package:http2/transport.dart'; import 'http2_request_context.dart'; class Http2ResponseContext extends ResponseContext { - @override - final Angel? app; - final ServerTransportStream stream; @override @@ -25,8 +22,9 @@ class Http2ResponseContext extends ResponseContext { Uri? _targetUri; - Http2ResponseContext(this.app, this.stream, this._req) { - _targetUri = _req!.uri; + Http2ResponseContext(Angel? app, this.stream, this._req) { + this.app = app; + _targetUri = _req?.uri; } final List _pushes = []; diff --git a/packages/framework/test/encoders_buffer_test.dart b/packages/framework/test/encoders_buffer_test.dart index 0b116e38..7b1e3a2b 100644 --- a/packages/framework/test/encoders_buffer_test.dart +++ b/packages/framework/test/encoders_buffer_test.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:io' show BytesBuilder; import 'dart:io'; import 'package:angel3_container/mirrors.dart'; diff --git a/packages/framework/test/streaming_test.dart b/packages/framework/test/streaming_test.dart index bfa435d1..b31a4ab6 100644 --- a/packages/framework/test/streaming_test.dart +++ b/packages/framework/test/streaming_test.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:io' show stderr; import 'dart:io'; import 'package:angel3_container/mirrors.dart'; @@ -92,7 +91,7 @@ void main() { test('cannot write after close', () async { try { var rq = MockHttpRequest('GET', Uri.parse('/overwrite')); - await (rq.close()); + await rq.close(); await http.handleRequest(rq); var body = await rq.response.transform(utf8.decoder).join();