12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- 'use strict';
- function maxFileSizeUnitTransform(maxLogSize) {
- if (typeof maxLogSize === 'number' && Number.isInteger(maxLogSize)) {
- return maxLogSize;
- }
- const units = {
- K: 1024,
- M: 1024 * 1024,
- G: 1024 * 1024 * 1024,
- };
- const validUnit = Object.keys(units);
- const unit = maxLogSize.substr(maxLogSize.length - 1).toLocaleUpperCase();
- const value = maxLogSize.substring(0, maxLogSize.length - 1).trim();
- if (validUnit.indexOf(unit) < 0 || !Number.isInteger(Number(value))) {
- throw Error(`maxLogSize: "${maxLogSize}" is invalid`);
- } else {
- return value * units[unit];
- }
- }
- function adapter(configAdapter, config) {
- const newConfig = Object.assign({}, config);
- Object.keys(configAdapter).forEach((key) => {
- if (newConfig[key]) {
- newConfig[key] = configAdapter[key](config[key]);
- }
- });
- return newConfig;
- }
- function fileAppenderAdapter(config) {
- const configAdapter = {
- maxLogSize: maxFileSizeUnitTransform
- };
- return adapter(configAdapter, config);
- }
- const adapters = {
- file: fileAppenderAdapter,
- fileSync: fileAppenderAdapter
- };
- module.exports.modifyConfig = config => (adapters[config.type] ? adapters[config.type](config) : config);
|