Use debug module for logging

This commit is contained in:
Raymond Feng 2014-03-04 09:42:55 -08:00
parent ed7e2e2ada
commit d14c58775d
2 changed files with 33 additions and 23 deletions

View File

@ -13,6 +13,12 @@ var util = require('util');
var assert = require('assert'); var assert = require('assert');
var async = require('async'); var async = require('async');
if (process.env.DEBUG === 'loopback') {
// For back-compatibility
process.env.DEBUG = 'loopback:*';
}
var debug = require('debug')('loopback:datasource');
/** /**
* Export public API * Export public API
*/ */
@ -239,11 +245,10 @@ DataSource.prototype.setup = function (name, settings) {
// just save everything we get // just save everything we get
this.settings = settings || {}; this.settings = settings || {};
// Check the debug env settings this.settings.debug = this.settings.debug || debug.enabled;
var debugEnv = process.env.DEBUG || process.env.NODE_DEBUG || '';
var debugModules = debugEnv.split(/[\s,]+/); if(this.settings.debug) {
if (debugModules.indexOf('loopback') !== -1) { debug('Settings: %j', this.settings);
this.settings.debug = true;
} }
// Disconnected by default // Disconnected by default
@ -673,7 +678,7 @@ DataSource.prototype.defineProperty = function (model, prop, params) {
this.modelBuilder.defineProperty(model, prop, params); this.modelBuilder.defineProperty(model, prop, params);
var resolvedProp = this.getModelDefinition(model).properties[prop]; var resolvedProp = this.getModelDefinition(model).properties[prop];
if (this.connector.defineProperty) { if (this.connector && this.connector.defineProperty) {
this.connector.defineProperty(model, prop, resolvedProp); this.connector.defineProperty(model, prop, resolvedProp);
} }
}; };
@ -740,7 +745,7 @@ DataSource.prototype.discoverModelDefinitions = function (options, cb) {
if (this.connector.discoverModelDefinitions) { if (this.connector.discoverModelDefinitions) {
this.connector.discoverModelDefinitions(options, cb); this.connector.discoverModelDefinitions(options, cb);
} else if (cb) { } else if (cb) {
cb(); process.nextTick(cb);
} }
}; };
@ -785,7 +790,7 @@ DataSource.prototype.discoverModelProperties = function (modelName, options, cb)
if (this.connector.discoverModelProperties) { if (this.connector.discoverModelProperties) {
this.connector.discoverModelProperties(modelName, options, cb); this.connector.discoverModelProperties(modelName, options, cb);
} else if (cb) { } else if (cb) {
cb(); process.nextTick(cb);
} }
}; };
@ -829,7 +834,7 @@ DataSource.prototype.discoverPrimaryKeys = function (modelName, options, cb) {
if (this.connector.discoverPrimaryKeys) { if (this.connector.discoverPrimaryKeys) {
this.connector.discoverPrimaryKeys(modelName, options, cb); this.connector.discoverPrimaryKeys(modelName, options, cb);
} else if (cb) { } else if (cb) {
cb(); process.nextTick(cb);
} }
}; };
@ -876,7 +881,7 @@ DataSource.prototype.discoverForeignKeys = function (modelName, options, cb) {
if (this.connector.discoverForeignKeys) { if (this.connector.discoverForeignKeys) {
this.connector.discoverForeignKeys(modelName, options, cb); this.connector.discoverForeignKeys(modelName, options, cb);
} else if (cb) { } else if (cb) {
cb(); process.nextTick(cb);
} }
}; };
@ -893,7 +898,7 @@ DataSource.prototype.discoverForeignKeysSync = function (modelName, options) {
return this.connector.discoverForeignKeysSync(modelName, options); return this.connector.discoverForeignKeysSync(modelName, options);
} }
return null; return null;
} };
/** /**
* Retrieves a description of the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table). * Retrieves a description of the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table).
@ -924,7 +929,7 @@ DataSource.prototype.discoverExportedForeignKeys = function (modelName, options,
if (this.connector.discoverExportedForeignKeys) { if (this.connector.discoverExportedForeignKeys) {
this.connector.discoverExportedForeignKeys(modelName, options, cb); this.connector.discoverExportedForeignKeys(modelName, options, cb);
} else if (cb) { } else if (cb) {
cb(); process.nextTick(cb);
} }
}; };
@ -989,7 +994,7 @@ DataSource.prototype.discoverSchema = function (modelName, options, cb) {
return; return;
} }
}); });
} };
/** /**
* Discover schema from a given modelName/view * Discover schema from a given modelName/view
@ -1046,7 +1051,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
}); });
if (self.settings.debug) { if (self.settings.debug) {
console.log('Primary keys: ', pks); debug('Primary keys: ', pks);
} }
var schema = { var schema = {
@ -1093,7 +1098,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
var schemaKey = columns[0].owner + '.' + modelName; var schemaKey = columns[0].owner + '.' + modelName;
if (!options.visited.hasOwnProperty(schemaKey)) { if (!options.visited.hasOwnProperty(schemaKey)) {
if (self.settings.debug) { if (self.settings.debug) {
console.log('Adding schema for ' + schemaKey); debug('Adding schema for ' + schemaKey);
} }
options.visited[schemaKey] = schema; options.visited[schemaKey] = schema;
} }
@ -1118,7 +1123,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
}); });
if (self.settings.debug) { if (self.settings.debug) {
console.log('Foreign keys: ', fks); debug('Foreign keys: ', fks);
} }
schema.options.relations = {}; schema.options.relations = {};
@ -1143,7 +1148,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
var moreTasks = []; var moreTasks = [];
for (var t in otherTables) { for (var t in otherTables) {
if (self.settings.debug) { if (self.settings.debug) {
console.log('Discovering related schema for ' + schemaKey); debug('Discovering related schema for ' + schemaKey);
} }
var newOptions = {}; var newOptions = {};
for (var key in options) { for (var key in options) {
@ -1191,7 +1196,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
}); });
if (self.settings.debug) { if (self.settings.debug) {
console.log('Primary keys: ', pks); debug('Primary keys: ', pks);
} }
var schema = { var schema = {
@ -1238,7 +1243,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
var schemaKey = columns[0].owner + '.' + modelName; var schemaKey = columns[0].owner + '.' + modelName;
if (!options.visited.hasOwnProperty(schemaKey)) { if (!options.visited.hasOwnProperty(schemaKey)) {
if (self.settings.debug) { if (self.settings.debug) {
console.log('Adding schema for ' + schemaKey); debug('Adding schema for ' + schemaKey);
} }
options.visited[schemaKey] = schema; options.visited[schemaKey] = schema;
} }
@ -1264,7 +1269,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
}); });
if (self.settings.debug) { if (self.settings.debug) {
console.log('Foreign keys: ', fks); debug('Foreign keys: ', fks);
} }
schema.options.relations = {}; schema.options.relations = {};
@ -1289,7 +1294,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
var moreTasks = []; var moreTasks = [];
for (var t in otherTables) { for (var t in otherTables) {
if (self.settings.debug) { if (self.settings.debug) {
console.log('Discovering related schema for ' + schemaKey); debug('Discovering related schema for ' + schemaKey);
} }
var newOptions = {}; var newOptions = {};
for (var key in options) { for (var key in options) {
@ -1395,6 +1400,7 @@ DataSource.prototype.isActual = function (models, cb) {
* @private used by connectors * @private used by connectors
*/ */
DataSource.prototype.log = function (sql, t) { DataSource.prototype.log = function (sql, t) {
debug(sql, t);
this.emit('log', sql, t); this.emit('log', sql, t);
}; };
@ -1402,13 +1408,16 @@ DataSource.prototype.log = function (sql, t) {
* Freeze dataSource. Behavior depends on connector * Freeze dataSource. Behavior depends on connector
*/ */
DataSource.prototype.freeze = function freeze() { DataSource.prototype.freeze = function freeze() {
if(!this.connector) {
throw new Error('The connector has not been initialized.');
}
if (this.connector.freezeDataSource) { if (this.connector.freezeDataSource) {
this.connector.freezeDataSource(); this.connector.freezeDataSource();
} }
if (this.connector.freezeSchema) { if (this.connector.freezeSchema) {
this.connector.freezeSchema(); this.connector.freezeSchema();
} }
} };
/** /**
* Return table name for specified `modelName` * Return table name for specified `modelName`

View File

@ -30,7 +30,8 @@
"async": "~0.2.10", "async": "~0.2.10",
"inflection": "~1.3.5", "inflection": "~1.3.5",
"traverse": "~0.6.6", "traverse": "~0.6.6",
"qs": "~0.6.6" "qs": "~0.6.6",
"debug": "~0.7.4"
}, },
"license": { "license": {
"name": "Dual MIT/StrongLoop", "name": "Dual MIT/StrongLoop",