« index

Coverage for /Users/yunong/workspace/node-restify/lib/plugins/bunyan.js : 66%

45 lines | 30 run | 15 missing | 0 partial | 7 blocks | 0 blocks run | 7 blocks missing

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

  // Copyright 2012 Mark Cavage, Inc.  All rights reserved.
  
  var assert = require('assert-plus');
  
  var shallowCopy = require('../utils').shallowCopy;
  
  
  ///--- API
  
  function requestLogger(options) {
      assert.optionalObject(options);
      options = options || {};
  
      var props;
      if (options.properties) {
          props = shallowCopy(options.properties);
      } else {
          props = {};
      }
      if (options.serializers)
          props.serializers = options.serializers;
  
      function bunyan(req, res, next) {
          if (!req.log && !options.log) {
              next();
              return;
          }
  
          var log = req.log || options.log;
  
          props.req_id = req.getId();
          req.log = log.child(props, props.serializers ? false : true);
          if (props.req_id)
              delete props.req_id;
  
          next();
      }
  
      return (bunyan);
  }
  
  
  ///--- Exports
  
  module.exports = requestLogger;
« index | cover.io