handle method

  1. @override
IDoxRequest handle(
  1. IDoxRequest req
)
override

Implementation

@override
IDoxRequest handle(IDoxRequest req) {
  if (enabled == false) {
    return req;
  }
  // skip the work when the logger would drop the line. With a filter the
  // status can change, so only the `enabled` check is safe there.
  LoggerConfig logger = Dox().config.logger;
  if (!logger.enabled) return req;
  if (filter == null && !Logger.shouldLog('info', logger.level)) return req;
  Map<String, dynamic> payload = <String, dynamic>{
    'request': _redact(req.toJson()),
  };

  if (withHeader) {
    payload['headers'] = _redact(req.headers);
  }

  String? requestId = req is DoxRequest ? req.requestId : null;
  Map<String, dynamic> text = <String, dynamic>{
    'status': 'info',
    'message': '${req.method} ${req.uri.path}',
    'network': <String, dynamic>{
      'client': <String, dynamic>{'ip': req.ip()},
    },
    'http': <String, dynamic>{
      'method': req.method,
      'url': req.uri.path,
      'request_id': ?requestId,
    },
    'payload': payload,
  };
  if (filter != null) {
    // a copy: the filter may return a map that can not change
    text = Map<String, dynamic>.of(filter!(text));
  }
  // the logger config decides if the line is written, and how
  String status = '${text.remove('status') ?? 'info'}';
  String message = '${text.remove('message') ?? ''}';
  Logger.log(status, message, text, 'request');
  return req;
}