Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Custom logger #371

Merged
merged 7 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ app.use(fileUpload({

You can set `debug` option to `true` to see some logging about upload process.
In this case middleware uses `console.log` and adds `Express-file-upload` prefix for outputs.
You can set a custom logger having `.log()` method to the `logger` option.

It will show you whether the request is invalid and also common events triggered during upload.
That can be really useful for troubleshooting and ***we recommend attaching debug output to each issue on Github***.
Expand Down Expand Up @@ -121,6 +122,7 @@ useTempFiles | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true<
tempFileDir | <ul><li><code>String</code>&nbsp;**(path)**</li></ul> | Path to store temporary files.<br />Used along with the <code>useTempFiles</code> option. By default this module uses 'tmp' folder in the current working directory.<br />You can use trailing slash, but it is not necessary.
parseNested | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li></ul> | By default, req.body and req.files are flattened like this: <code>{'name': 'John', 'hobbies[0]': 'Cinema', 'hobbies[1]': 'Bike'}</code><br /><br/>When this option is enabled they are parsed in order to be nested like this: <code>{'name': 'John', 'hobbies': ['Cinema', 'Bike']}</code>
debug | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></ul> | Turn on/off upload process logging. Can be useful for troubleshooting.
logger | <ul><li><code>console</code>&nbsp;**(default)**</li><li><code>{log: function(msg: string)}</code></li></ul> | Customizable logger to write debug messages to. Console is default.
uploadTimeout | <ul><li><code>60000</code>&nbsp;**(default)**</li><li><code>Integer</code></ul> | This defines how long to wait for data before aborting. Set to 0 if you want to turn off timeout checks.

# Help Wanted
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const busboy = require('busboy'); // eslint-disable-line no-unused-vars

const DEFAULT_OPTIONS = {
debug: false,
logger: console,
uploadTimeout: 60000,
fileHandler: false,
uriDecodeFileNames: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ let tempCounter = 0;
*/
const debugLog = (options, msg) => {
const opts = options || {};
if (!opts.debug) return false;
console.log(`Express-file-upload: ${msg}`); // eslint-disable-line
if (!opts.debug || !opts.logger || typeof opts.logger.log !== "function") return false;
opts.logger.log(`Express-file-upload: ${msg}`);
return true;
};

Expand Down
18 changes: 15 additions & 3 deletions test/utilities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@ describe('utilities: Test of the utilities functions', function() {
assert.equal(debugLog(null, testMessage), false);
});

it('debugLog returns false if option debug is false', () => {
it('debugLog returns false if option debug is false or logger is not set', () => {
assert.equal(debugLog({debug: false}, testMessage), false);
assert.equal(debugLog({debug: true, logger: undefined}, testMessage), false);
assert.equal(debugLog({debug: true, logger: {}}, testMessage), false);
});

it('debugLog returns true if option debug is true', () => {
assert.equal(debugLog({debug: true}, testMessage), true);
it('debugLog returns true if option debug is true and logger is set', () => {
assert.equal(debugLog({debug: true, logger: console}, testMessage), true);
});

it('supports a custom logger', () => {
const calls = [];
const logger = {
log: (...args) => calls.push(args)
};
debugLog({debug: true, logger}, testMessage);
assert.equal(calls.length, 1);
assert.deepEqual(calls[0], [`Express-file-upload: ${testMessage}`]);
});

});
Expand Down