不要怂,就是干,撸起袖子干!

Commit f7c9be39 by Sascha Depold

Don't depend on winston

1 parent 04fdd411
Showing with 16 additions and 96 deletions
......@@ -494,44 +494,16 @@ module.exports = (function() {
return transaction
}
Object.defineProperty(Sequelize.prototype, 'logger', {
get: function() {
if (!this._logger) {
if (Sequelize.Utils.isHash(this.options.logging)) {
this._logger = new winston.Logger(this.options.logging)
} else {
this._logger = winston
}
}
Sequelize.prototype.log = function() {
var args = [].slice.call(arguments)
return this._logger
}
})
Sequelize.prototype.log = function(typeOrMessage) {
if (this.options.logging) {
var args = []
if (arguments.length === 0) {
// No arguments have been passed. Log an empty string.
args.push('info')
args.push('')
} else if (arguments.length === 1) {
// Only one argument has been passed.
// Use the default type and treat the argument as message.
args.push('info')
args.push(typeOrMessage)
} else {
// There have been more than one passed arguments.
// Use them as arguments for winston.
args = [].slice.call(arguments)
if (this.options.logging === true) {
console.log('DEPRECATION WARNING: The logging-option should be either a function or false. Default: console.log')
this.options.logging = console.log
}
if ((typeof this.options.logging === 'function') && (this.options.logging != console.log)) {
this.options.logging.apply(null, args.slice(1))
} else {
this.logger.log.apply(this.logger, args)
}
this.options.logging.apply(null, args)
}
}
......
......@@ -15,60 +15,45 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
describe('log', function() {
describe("with disabled logging", function() {
beforeEach(function() {
this.sequelize = new Support.Sequelize('db', 'user', 'pw', { logging: false })
this.loggerMock = sinon.mock(this.sequelize.logger)
})
afterEach(function() {
this.loggerMock.verify()
this.sequelize = new Support.Sequelize('db', 'user', 'pw', { logging: false })
this.spy = sinon.spy(console.log)
})
it("does not call the log method of the logger", function() {
this.loggerMock.expects("log").never()
this.sequelize.log()
expect(this.spy.calledOnce).to.be.false
})
})
describe('with default logging options', function() {
beforeEach(function() {
this.sequelize = new Support.Sequelize('db', 'user', 'pw')
this.loggerMock = sinon.mock(this.sequelize.logger)
})
afterEach(function() {
this.loggerMock.verify()
this.sequelize = new Support.Sequelize('db', 'user', 'pw')
this.spy = sinon.spy(console.log)
})
describe("called with no arguments", function() {
it('calls the log method', function() {
this.loggerMock.expects("log").once()
this.sequelize.log()
expect(this.spy.calledOnce).to.be.false
})
it('logs an empty string as info event', function() {
this.loggerMock.expects("log").withArgs('info', '').once()
this.sequelize.log()
expect(this.spy.withArgs('').calledOnce).to.be.false
})
})
describe("called with one argument", function() {
it('logs the passed string as info event', function() {
this.loggerMock.expects("log").withArgs('info', 'my message').once()
this.sequelize.log('my message')
})
})
describe("called with two arguments", function() {
it("uses the first argument as event name and the second as message", function() {
this.loggerMock.expects("log").withArgs('error', 'my message')
this.sequelize.log('error', 'my message')
expect(this.spy.withArgs('my message').calledOnce).to.be.false
})
})
describe("called with more than two arguments", function() {
it("uses the first argument as event name and passes the others to the logger", function() {
this.loggerMock.expects("log").withArgs('error', 'my message', 1, { a: 1 })
it("passes the arguments to the logger", function() {
this.sequelize.log('error', 'my message', 1, { a: 1 })
expect(this.spy.withArgs('error', 'my message', 1, { a: 1 }).calledOnce).to.be.false
})
})
})
......@@ -84,42 +69,5 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
expect(this.spy.calledOnce).to.be.true
})
})
describe("with custom winston options", function() {
beforeEach(function() {
this.logFile = path.normalize(__dirname + '/../tmp/sequelize.log')
if (fs.existsSync(this.logFile)) {
fs.unlinkSync(this.logFile)
}
this.spy = sinon.spy()
this.sequelize = new Support.Sequelize('db', 'user', 'pw', {
logging: {
transports: [
new winston.transports.File({ filename: this.logFile })
]
}
})
})
afterEach(function() {
if (fs.existsSync(this.logFile)) {
fs.unlinkSync(this.logFile)
}
})
it("calls the custom logger method", function(done) {
var self = this
expect(fs.existsSync(this.logFile)).to.be.false
this.sequelize.log('om nom')
setTimeout(function() {
expect(fs.existsSync(self.logFile)).to.be.true
done()
}, 100)
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!