'use strict';

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _ms = require('ms');

var _ms2 = _interopRequireDefault(_ms);

var _winston = require('winston');

var _winston2 = _interopRequireDefault(_winston);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _util = require('../util');

var _dbTransport = require('./db-transport');

var _dbTransport2 = _interopRequireDefault(_dbTransport);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * @typedef {Function} Logger~MessageLogger
 * @param {String} message Message to log
 * @param {...Object} objects Additional objects to log
 */

/**
 * @class Logger
 * @prop {Logger~MessageLogger} debug Logs message for debugging purposes
 * (hidden in production by default, see {@link Botfile} to change this)
 * @prop {Logger~MessageLogger} info Logs informative messages (shown in production)
 * @prop {Logger~MessageLogger} warn Logs warning messages (shown in production)
 * @prop {Logger~MessageLogger} error Logs error messages (shown in production)
 */

module.exports = logConfig => {
  let _db = null;

  const logger = new _winston2.default.Logger({
    level: process.env.LOG_LEVEL ? process.env.LOG_LEVEL.toLowerCase() : _util.isDeveloping ? 'debug' : 'info',
    transports: [new _winston2.default.transports.Console({
      prettyPrint: true,
      colorize: true,
      timestamp: () => (0, _moment2.default)().format('HH:mm:ss')
    })]
  });

  logger.enableDbStorageIfNeeded = ({ db, janitor }) => {
    if (logConfig.enabled) {
      _db = db;
      const ttl = (0, _ms2.default)(`${logConfig.keepDays || 30}days`);
      logger.add(_dbTransport2.default, { ttl, db, janitor });
    }
  };

  logger.queryDb = (limit, order) => {
    if (!logConfig.enabled) {
      return _bluebird2.default.resolve([]);
    }
    return _dbTransport2.default._query(_db, limit, order);
  };

  return logger;
};
//# sourceMappingURL=index.js.map