OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:stack_trace/stack_trace.dart'; | 7 import 'package:stack_trace/stack_trace.dart'; |
8 | 8 |
9 import '../hijack_exception.dart'; | 9 import '../hijack_exception.dart'; |
10 import '../middleware.dart'; | 10 import '../middleware.dart'; |
11 | 11 |
12 /// Middleware which prints the time of the request, the elapsed time for the | 12 /// Middleware which prints the time of the request, the elapsed time for the |
13 /// inner handlers, the response's status code and the request URI. | 13 /// inner handlers, the response's status code and the request URI. |
14 /// | 14 /// |
15 /// [logger] takes two parameters. | 15 /// If [logger] is passed, it's called for each request. The `msg` parameter is |
| 16 /// a formatted string that includes the request time, duration, request method, |
| 17 /// and requested path. When an exception is thrown, it also includes the |
| 18 /// exception's string and stack trace; otherwise, it includes the status code. |
| 19 /// The `isError` parameter indicates whether the message is caused by an error. |
16 /// | 20 /// |
17 /// `msg` includes the request time, duration, request method, and requested | 21 /// If [logger] is not passed, the message is just passed to [print]. |
18 /// path. | |
19 /// | |
20 /// For successful requests, `msg` also includes the status code. | |
21 /// | |
22 /// When an error is thrown, `isError` is true and `msg` contains the error | |
23 /// description and stack trace. | |
24 Middleware logRequests({void logger(String msg, bool isError)}) => | 22 Middleware logRequests({void logger(String msg, bool isError)}) => |
25 (innerHandler) { | 23 (innerHandler) { |
26 if (logger == null) logger = _defaultLogger; | 24 if (logger == null) logger = _defaultLogger; |
27 | 25 |
28 return (request) { | 26 return (request) { |
29 var startTime = new DateTime.now(); | 27 var startTime = new DateTime.now(); |
30 var watch = new Stopwatch()..start(); | 28 var watch = new Stopwatch()..start(); |
31 | 29 |
32 return new Future.sync(() => innerHandler(request)).then((response) { | 30 return new Future.sync(() => innerHandler(request)).then((response) { |
33 var msg = _getMessage(startTime, response.statusCode, | 31 var msg = _getMessage(startTime, response.statusCode, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 return '$msg\n$chain'; | 72 return '$msg\n$chain'; |
75 } | 73 } |
76 | 74 |
77 void _defaultLogger(String msg, bool isError) { | 75 void _defaultLogger(String msg, bool isError) { |
78 if (isError) { | 76 if (isError) { |
79 print('[ERROR] $msg'); | 77 print('[ERROR] $msg'); |
80 } else { | 78 } else { |
81 print(msg); | 79 print(msg); |
82 } | 80 } |
83 } | 81 } |
OLD | NEW |