Skip to content

Commit

Permalink
Merge pull request sequelize#1566 from sequelize/feature/logging
Browse files Browse the repository at this point in the history
Refactored logging
  • Loading branch information
sdepold committed Apr 13, 2014
2 parents bf087b1 + e8ad0ec commit 0675005
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 30 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
- [BUG] Fix a case where createdAt timestamp would not be set when updatedAt was disabled Thanks to @fixe [#1543](https://github.com/sequelize/sequelize/pull/1543)
- [BUG] Fix a case where timestamps were not being write protected in `set` when underscored=true. janmeier [#1523](https://github.com/sequelize/sequelize/pull/1523)
- [FEATURE/BUG] Prefetching/includes now fully support schemas
- [FEATURE] Centralize logging. [#1566](https://github.com/sequelize/sequelize/pull/1566)
- [FEATURE/BUG] hstore values are now parsed on find/findAll. Thanks to @nunofgs [#1560](https://github.com/sequelize/sequelize/pull/1560)
- [FEATURE] Read cli options from a file. Thanks to @codeinvain [#1540](https://github.com/sequelize/sequelize/pull/1540)

Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/mariadb/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = (function() {
this.sql = sql

if (this.options.logging !== false) {
this.options.logging('Executing (' + this.options.uuid + '): ' + this.sql)
this.sequelize.log('Executing (' + this.options.uuid + '): ' + this.sql)
}

var resultSet = [],
Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/mysql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = (function() {
this.sql = sql

if (this.options.logging !== false) {
this.options.logging('Executing (' + this.options.uuid + '): ' + this.sql)
this.sequelize.log('Executing (' + this.options.uuid + '): ' + this.sql)
}

this.client.query(this.sql, function(err, results, fields) {
Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/postgres/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = (function() {
, rows = []

if (this.options.logging !== false) {
this.options.logging('Executing (' + this.options.uuid + '): ' + this.sql)
this.sequelize.log('Executing (' + this.options.uuid + '): ' + this.sql)
}

query.on('row', function(row) {
Expand Down
6 changes: 3 additions & 3 deletions lib/dialects/sqlite/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = (function() {
this.sql = sql

if (this.options.logging !== false) {
this.options.logging('Executing (' + this.options.uuid + '): ' + this.sql)
this.sequelize.log('Executing (' + this.options.uuid + '): ' + this.sql)
}

var columnTypes = {}
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = (function() {
executeSql()
} else {
var execute = Utils._.after(tableNames.length, executeSql)

tableNames.forEach(function (tableName) {
if (tableName !== 'sqlite_master') {
// get the column types
Expand Down Expand Up @@ -174,7 +174,7 @@ module.exports = (function() {
} else if (this.sql.indexOf('PRAGMA foreign_keys') !== -1) {
result = results
} else if ([QueryTypes.BULKUPDATE, QueryTypes.BULKDELETE].indexOf(this.options.type) !== -1) {
result = metaData.changes
result = metaData.changes
}

this.emit('success', result)
Expand Down
26 changes: 7 additions & 19 deletions lib/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ module.exports = (function() {
logging: console.log,
filesFilter: /\.js$/
}, options || {})

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 (this.options.logging == console.log) {
// using just console.log will break in node < 0.6
this.options.logging = function(s) { console.log(s) }
}
}

Object.defineProperty(Migrator.prototype, "queryInterface", {
Expand Down Expand Up @@ -53,29 +43,25 @@ module.exports = (function() {
}

if (migrations.length === 0) {
self.options.logging("There are no pending migrations.")
self.sequelize.log("There are no pending migrations.")
} else {
self.options.logging("Running migrations...")
self.sequelize.log("Running migrations...")
}

migrations.forEach(function(migration) {
var migrationTime

chainer.add(migration, 'execute', [options], {
before: function(migration) {
if (self.options.logging !== false) {
self.options.logging(migration.filename)
}
self.sequelize.log(migration.filename)
migrationTime = process.hrtime()
},

after: function(migration) {
migrationTime = process.hrtime(migrationTime)
migrationTime = Math.round( (migrationTime[0] * 1000) + (migrationTime[1] / 1000000));

if (self.options.logging !== false) {
self.options.logging('Completed in ' + migrationTime + 'ms')
}
self.sequelize.log('Completed in ' + migrationTime + 'ms')
},

success: function(migration, callback) {
Expand Down Expand Up @@ -193,8 +179,10 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer()
var addMigration = function(filename) {
self.options.logging('Adding migration script at ' + filename)
var migration = new Migration(self, filename)

self.sequelize.log('Adding migration script at ' + filename)

chainer.add(migration, 'execute', [{ method: 'up' }], {
before: function(migration) {
if (options && Utils._.isFunction(options.before)) {
Expand Down
15 changes: 14 additions & 1 deletion lib/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ module.exports = (function() {

self.daoFactoryManager.forEachDAO(function(dao) {
if (dao) {
chainer.add(dao, 'drop', [options])
chainer.add(dao, 'drop', [options])
}
}, { reverse: false})

Expand Down Expand Up @@ -493,5 +493,18 @@ module.exports = (function() {
return transaction
}

Sequelize.prototype.log = function() {
var args = [].slice.call(arguments)

if (this.options.logging) {
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
}

this.options.logging.apply(null, args)
}
}

return Sequelize
})()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"istanbul": "~0.1.45",
"coveralls": "~2.7.1",
"async": "~0.2.10",
"coffee-script": "~1.7.1"
"coffee-script": "~1.7.1",
"sinon-chai": "~2.5.0"
},
"keywords": [
"mysql",
Expand Down
6 changes: 3 additions & 3 deletions test/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,11 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
, localInstanceMethod = sinon.spy()
, sequelize = Support.createSequelizeInstance({
define: {
classMethods : {
classMethods : {
globalClassMethod : function() {},
overrideMe: globalClassMethod
},
instanceMethods : {
instanceMethods : {
globalInstanceMethod : function() {},
overrideMe: globalInstanceMethod
}
Expand All @@ -445,7 +445,7 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {


DAO = sequelize.define('foo', {bar: DataTypes.STRING}, {
classMethods : {
classMethods : {
overrideMe : localClassMethod
},
instanceMethods: {
Expand Down
78 changes: 78 additions & 0 deletions test/sequelize/log.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var chai = require('chai')
, sinonChai = require("sinon-chai")
, sinon = require('sinon')
, fs = require('fs')
, path = require('path')
, expect = chai.expect
, assert = chai.assert
, Support = require(__dirname + '/../support')

chai.use(sinonChai)
chai.config.includeStack = true

describe(Support.getTestDialectTeaser("Sequelize"), function () {
describe('log', function() {
beforeEach(function() {
this.spy = sinon.spy(console, 'log')
})

afterEach(function() {
console.log.restore()
})

describe("with disabled logging", function() {
beforeEach(function() {
this.sequelize = new Support.Sequelize('db', 'user', 'pw', { logging: false })
})

it("does not call the log method of the logger", function() {
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')
})

describe("called with no arguments", function() {
it('calls the log method', function() {
this.sequelize.log()
expect(this.spy.calledOnce).to.be.true
})

it('logs an empty string as info event', function() {
this.sequelize.log()
expect(this.spy.calledOnce).to.be.true
})
})

describe("called with one argument", function() {
it('logs the passed string as info event', function() {
this.sequelize.log('my message')
expect(this.spy.withArgs('my message').calledOnce).to.be.true
})
})

describe("called with more than two arguments", function() {
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.true
})
})
})

describe("with a custom function for logging", function() {
beforeEach(function() {
this.spy = sinon.spy()
this.sequelize = new Support.Sequelize('db', 'user', 'pw', { logging: this.spy })
})

it("calls the custom logger method", function() {
this.sequelize.log('om nom')
expect(this.spy.calledOnce).to.be.true
})
})
})
})

0 comments on commit 0675005

Please sign in to comment.