Skip to content
Closed
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
6 changes: 5 additions & 1 deletion lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = class File extends TransportStream {

this.dirname = options.dirname || path.dirname(options.filename);
this.options = options.options || { flags: 'a' };
if (options.mode != null) this.options.mode = options.mode;
} else if (options.stream) {
// eslint-disable-next-line no-console
console.warn('options.stream will be removed in winston@4. Use winston.transports.Stream');
Expand All @@ -80,6 +81,7 @@ module.exports = class File extends TransportStream {
this.eol = (typeof options.eol === 'string') ? options.eol : os.EOL;
this.tailable = options.tailable || false;
this.lazy = options.lazy || false;
this.dirMode = options.dirMode || null;

// Internal state variables representing the number of files this instance
// has created and the current size (in bytes) of the current logfile.
Expand Down Expand Up @@ -788,7 +790,9 @@ module.exports = class File extends TransportStream {
_createLogDirIfNotExist(dirPath) {
/* eslint-disable no-sync */
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
const mkdirOpts = { recursive: true };
if (this.dirMode != null) mkdirOpts.mode = this.dirMode;
fs.mkdirSync(dirPath, mkdirOpts);
}
/* eslint-enable no-sync */
}
Expand Down
2 changes: 2 additions & 0 deletions lib/winston/transports/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ declare namespace winston {
eol?: string;
tailable?: boolean;
lazy?: boolean;
mode?: number;
dirMode?: number;
}

interface FileTransportInstance extends Transport {
Expand Down
42 changes: 42 additions & 0 deletions test/unit/winston/transports/file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,48 @@ describe('File Transport', function () {
});


describe('Mode option', function () {
it('should pass mode to the write stream options', function () {
const transport = new winston.transports.File({
filename: 'testmode.log',
dirname: testLogFixturesPath,
mode: 0o640
});

assert.strictEqual(transport.options.mode, 0o640);
});

it('should not set mode on stream options when mode is not provided', function () {
const transport = new winston.transports.File({
filename: 'testmode-default.log',
dirname: testLogFixturesPath
});

assert.strictEqual(transport.options.mode, undefined);
});
});

describe('DirMode option', function () {
it('should store dirMode when provided', function () {
const transport = new winston.transports.File({
filename: 'testdirmode.log',
dirname: testLogFixturesPath,
dirMode: 0o750
});

assert.strictEqual(transport.dirMode, 0o750);
});

it('should default dirMode to null when not provided', function () {
const transport = new winston.transports.File({
filename: 'testdirmode-default.log',
dirname: testLogFixturesPath
});

assert.strictEqual(transport.dirMode, null);
});
});

// TODO: Reintroduce these tests
//
// "Error object in metadata #610": {
Expand Down