Skip to content

Commit 2856e23

Browse files
committed
feat(log): Add verbose option from command line
By adding the -v or --verbose parameter to the command line, the log level will be set to the lowest one (will print everithing).
1 parent 4bc5dbc commit 2856e23

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"commit": "git-cz",
3232
"prebuild": "rimraf lib",
3333
"prepublishOnly": "pkg-ok",
34-
"start": "babel-node src/index.js",
34+
"start": "babel-node src/index.js -v",
3535
"start:disk": "babel-node src/index.js --storageType=disk",
3636
"start:prod": "node lib/index.js",
3737
"test": "echo \"Error: no test specified\" && exit 0",

src/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import nodegetopt from 'node-getopt';
2+
import logger, { setLogLevel } from './log';
23
import server from './server';
4+
import pkg from '../package.json';
35

46
const opt = nodegetopt.create([
57
['p', 'port=PORT', 'server port (default 5000)'],
@@ -8,15 +10,27 @@ const opt = nodegetopt.create([
810
['', 'storageType=TYPE', "disk or memory (default 'memory')"],
911
['', 'storagePath=PATH', "where to save files (default '/tmp')"],
1012
['', 'route=flies', "the API starting path (default '/files')"],
13+
['v', 'verbose', 'change log level to lowest'],
1114
]).bindHelp().parseSystem().options;
1215

16+
logger.info('================================');
17+
logger.info('>>> Express REST file server');
18+
logger.info(`>>> version: ${pkg.version}`);
19+
logger.info('================================');
20+
21+
if (opt.verbose) {
22+
setLogLevel('silly');
23+
logger.debug('Command line options', opt);
24+
}
25+
1326
server.run({
1427
port: (opt.port || process.env.PORT || 5000),
1528
chunkNumber: opt.chunknumber,
1629
totalSize: opt.totalsize,
1730
storage: {
18-
type: opt.storageType,
19-
path: opt.storagePath,
31+
type: (opt.storageType || 'memory'),
32+
path: (opt.storageType === 'disk' && opt.storagePath ? opt.storagePath : '/tmp'),
2033
},
2134
route: (opt.route || 'files'),
35+
verbose: (!!opt.verbose),
2236
});

src/log.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import winston, { Logger, transports as _transports } from 'winston';
22

3-
const { config } = winston;
4-
const logger = new (Logger)({
3+
const logger = new Logger({
4+
level: winston.level,
55
transports: [
66
new (_transports.Console)({
77
timestamp() {
88
return Date.now();
99
},
1010
formatter(options) {
11-
const color = config.colorize(options.level, options.level.toUpperCase());
11+
const level = winston.config.colorize(options.level, options.level.toUpperCase().padEnd(6));
1212
const message = options.message ? options.message : '';
1313
const meta = options.meta && Object.keys(options.meta).length ?
14-
`\n\t${JSON.stringify(options.meta)}` :
14+
`\n${JSON.stringify(options.meta, null, 2)}` :
1515
'';
16-
return `[${options.timestamp()}][${color}] ${message} ${meta}`;
16+
return `[${options.timestamp()}][${level}] ${message} ${meta}`;
1717
},
1818
}),
1919
],
2020
});
2121

2222
export default logger;
23+
24+
export function setLogLevel(level) {
25+
logger.transports.console.level = level;
26+
}

src/server.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default {
2929
init(options) {
3030
let storage;
3131
if (options.storage.type === 'disk') {
32-
if (options.storage.path && !existsSync(options.storage.pathh)) {
32+
if (options.storage.path && !existsSync(options.storage.path)) {
3333
logger.error(options.storage.path, 'does not exist');
3434
throw new Error(`${options.storage.path} does not exist`);
3535
}
@@ -43,7 +43,6 @@ export default {
4343
},
4444
});
4545
} else {
46-
logger.info('Using memory storage');
4746
storage = memoryStorage();
4847
}
4948
const upload = multer({ storage });
@@ -98,12 +97,16 @@ export default {
9897
},
9998

10099
run(options) {
101-
logger.info('================================');
102-
logger.info('>>> Express REST file server <<<');
103-
logger.info('================================');
104100
const server = this.init(options);
105101
server.listen(options.port, () => {
106-
logger.info('Listening on', options.port);
102+
logger.info('Server ready. Configuration:');
103+
logger.info(
104+
' * Storage: %s %s', options.storage.type,
105+
((options.storage.type === 'disk' && options.storage.path) || '')
106+
);
107+
logger.info(' * Port:', options.port);
108+
logger.info(' * Routes:', `/${options.route}`);
109+
logger.info(' * Verbose:', options.verbose ? 'yes' : 'no');
107110
});
108111
},
109112
};

0 commit comments

Comments
 (0)