Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(412)

Side by Side Diff: mojo/public/dart/third_party/shelf/lib/src/handlers/logger.dart

Issue 1346773002: Stop running pub get at gclient sync time and fix build bugs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library shelf.handlers.logger;
6
7 import 'dart:async';
8
9 import 'package:stack_trace/stack_trace.dart';
10
11 import '../hijack_exception.dart';
12 import '../middleware.dart';
13
14 /// Middleware which prints the time of the request, the elapsed time for the
15 /// inner handlers, the response's status code and the request URI.
16 ///
17 /// [logger] takes two parameters.
18 ///
19 /// `msg` includes the request time, duration, request method, and requested
20 /// path.
21 ///
22 /// For successful requests, `msg` also includes the status code.
23 ///
24 /// When an error is thrown, `isError` is true and `msg` contains the error
25 /// description and stack trace.
26 Middleware logRequests({void logger(String msg, bool isError)}) =>
27 (innerHandler) {
28 if (logger == null) logger = _defaultLogger;
29
30 return (request) {
31 var startTime = new DateTime.now();
32 var watch = new Stopwatch()..start();
33
34 return new Future.sync(() => innerHandler(request)).then((response) {
35 var msg = _getMessage(startTime, response.statusCode,
36 request.requestedUri, request.method, watch.elapsed);
37
38 logger(msg, false);
39
40 return response;
41 }, onError: (error, stackTrace) {
42 if (error is HijackException) throw error;
43
44 var msg = _getErrorMessage(startTime, request.requestedUri,
45 request.method, watch.elapsed, error, stackTrace);
46
47 logger(msg, true);
48
49 throw error;
50 });
51 };
52 };
53
54 String _formatQuery(String query) {
55 return query == '' ? '' : '?$query';
56 }
57
58 String _getMessage(DateTime requestTime, int statusCode, Uri requestedUri,
59 String method, Duration elapsedTime) {
60 return '${requestTime}\t$elapsedTime\t$method\t[${statusCode}]\t'
61 '${requestedUri.path}${_formatQuery(requestedUri.query)}';
62 }
63
64 String _getErrorMessage(DateTime requestTime, Uri requestedUri, String method,
65 Duration elapsedTime, Object error, StackTrace stack) {
66 var chain = new Chain.current();
67 if (stack != null) {
68 chain = new Chain.forTrace(stack)
69 .foldFrames((frame) => frame.isCore || frame.package == 'shelf').terse;
70 }
71
72 var msg = '${requestTime}\t$elapsedTime\t$method\t${requestedUri.path}'
73 '${_formatQuery(requestedUri.query)}\n$error';
74 if (chain == null) return msg;
75
76 return '$msg\n$chain';
77 }
78
79 void _defaultLogger(String msg, bool isError) {
80 if (isError) {
81 print('[ERROR] $msg');
82 } else {
83 print(msg);
84 }
85 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698