All files index.js

100% Statements 23/23
100% Branches 6/6
100% Functions 1/1
100% Lines 23/23
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67          1x 1x           1x           1x           32x 16x     16x         16x 8x     16x   16x 13x 13x 13x   13x 6x   6x     7x         7x   7x     16x 6x     16x    
 
/**
 * Module dependencies.
 */
 
import bunyan from 'bunyan';
import debug from 'debug';
 
/**
 * Loggers.
 */
 
const loggers = Object.create(null);
 
/**
 * Default level.
 */
 
const level = bunyan.FATAL + 1;
 
/**
 * Export `debugnyan`.
 */
 
export default function debugnyan(name, options, config) {
  const components = name.split(':');
  const [root] = components;
 
  config = Object.assign({
    prefix: 'sub',
    suffix: 'component'
  }, config);
 
  if (!loggers[root]) {
    loggers[root] = bunyan.createLogger(Object.assign({}, options, { level, name: root }));
  }
 
  let child = loggers[root];
 
  for (let i = 1; i < components.length; i++) {
    const current = components[i];
    const next = loggers[components.slice(0, i).join(':')];
    const childName = components.slice(0, i + 1).join(':');
 
    if (loggers[childName]) {
      child = loggers[childName];
 
      continue;
    }
 
    options = Object.assign({}, options, {
      level,
      [`${config.prefix.repeat(i - 1)}${config.suffix}`]: current
    });
 
    child = next.child(options, true);
 
    loggers[childName] = child;
  }
 
  if (debug.enabled(name)) {
    child.level(bunyan.DEBUG);
  }
 
  return loggers[name];
}