Skip to content

Commit

Permalink
prevent filename starting with a leading . with prepend option (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattberther committed Jun 5, 2016
1 parent e181f18 commit d17c0d8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var DailyRotateFile = module.exports = function (options) {
this.prettyPrint = options.prettyPrint || false;
this.showLevel = options.showLevel === undefined ? true : options.showLevel;
this.timestamp = options.timestamp ? options.timestamp : true;
this.datePattern = options.datePattern ? options.datePattern : '.yyyy-MM-dd';
this.datePattern = options.datePattern ? options.datePattern : 'yyyy-MM-dd';
this.depth = options.depth || null;
this.eol = options.eol || os.EOL;
this.maxRetries = options.maxRetries || 2;
Expand Down Expand Up @@ -601,10 +601,10 @@ DailyRotateFile.prototype._getFilename = function () {
var formattedDate = this.getFormattedDate();

if (this.prepend) {
return formattedDate + this._basename;
return [formattedDate, this._basename].join('.');
}

return this._basename + formattedDate;
return [this._basename, formattedDate].join('.');
};

//
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"eslint-config-xo-space": "0.13.0",
"mkdirp": "0.5.1",
"mocha": "2.4.5",
"moment": "2.13.0",
"rimraf": "2.5.2"
},
"eslintConfig": {
Expand Down
29 changes: 25 additions & 4 deletions test/simple.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var expect = require('chai').expect;
var winston = require('winston');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var moment = require('moment');
var MemoryStream = require('./memory-stream');

var DailyRotateFile = require('../');
Expand All @@ -17,18 +18,39 @@ mkdirp(fixturesDir);
var transports = {
'file': new DailyRotateFile({
filename: path.join(fixturesDir, 'testfilename.log'),
datePattern: '.yyyy-MM-dd'
prepend: false
}),
'stream': new DailyRotateFile({stream: new MemoryStream()}),
'prepended file': new DailyRotateFile({
filename: path.join(fixturesDir, 'testfilename.log'),
datePattern: 'yyyy-MM-dd_',
prepend: true
})
};

describe('winston/transports/daily-rotate-file', function () {
describe('an instance of the transport', function () {
describe('with default datePatterns', function () {
it('should have a proper filename when prepend option is false', function () {
var now = moment().format('YYYY-MM-DD');
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'prepend-false.log'),
prepend: false
});

expect(transport._getFilename()).to.equal('prepend-false.log.' + now);
});

it('should have a proper filename when prepend options is true', function () {
var now = moment().format('YYYY-MM-DD');
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'prepend-true.log'),
prepend: true
});

expect(transport._getFilename()).to.equal(now + '.prepend-true.log');
});
});

Object.keys(transports).forEach(function (t) {
describe('when passed a valid ' + t, function () {
var transport;
Expand Down Expand Up @@ -82,8 +104,7 @@ describe('winston/transports/daily-rotate-file', function () {

beforeEach(function () {
transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'invalid', 'testfilename.log'),
datePattern: '.yyyy-MM-dd'
filename: path.join(fixturesDir, 'invalid', 'testfilename.log')
});
});

Expand Down

0 comments on commit d17c0d8

Please sign in to comment.