Skip to content

Commit

Permalink
Add consistent logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kunkle committed Dec 14, 2011
1 parent 8b3a7b8 commit c184b26
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
35 changes: 16 additions & 19 deletions lib/cli.js
Expand Up @@ -4,18 +4,15 @@ var config = require('./config');
var fs = require('fs');
var path = require('path');
var async = require('async');
var log = require('./log');

function goUp(db, migration, callback) {
if (verbose) {
console.log('processing migration ' + migration.name);
}
log.info('processing migration', migration.name);
migration.up(db);
}

function goDown(db, migration) {
if (verbose) {
console.log('processing migration ' + migration.name);
}
log.info('processing migration', migration.name);
migration.down(db);
}

Expand Down Expand Up @@ -74,7 +71,7 @@ function doUp(db, dbResults, files, options) {
files = filterUp(files, dbResults, options);

if (files.length < 1) {
console.log('No migrations to run.');
log.info('No migrations to run.');
return;
}

Expand All @@ -97,7 +94,7 @@ function doUp(db, dbResults, files, options) {
db.removeListener('end', onEnd);
db.removeListener('error', onError);
if (errors.length > 0) {
console.error('[FAILED]', errors);
log.error(errors);
iterCallback(errors);
} else {
writeMigrationRecord(db, migration, iterCallback);
Expand All @@ -119,7 +116,7 @@ function doDown(db, dbResults, options) {
dbResults = filterDown(dbResults, options);

if (dbResults.length < 1) {
console.log('No migrations to run.');
log.info('No migrations to run.');
return;
}

Expand All @@ -142,7 +139,7 @@ function doDown(db, dbResults, options) {
db.removeListener('end', onEnd);
db.removeListener('error', onError);
if (errors.length > 0) {
console.error('[FAILED]', errors);
log.error(errors);
iterCallback(errors);
} else {
deleteMigrationRecord(db, migration, iterCallback);
Expand All @@ -164,13 +161,13 @@ exports.up = function(options) {
var env = config.getCurrent();
var driver = require('./driver');
driver.connect(env, function(err, db) {
if (err) { console.error('[FAILED]', err); return; }
if (err) { log.error(err); return; }
db.createMigrationsTable(function(err) {
if (err) { console.error('[FAILED]', err); return; }
if (err) { log.error(err); return; }
db.all('SELECT * FROM migrations ORDER BY name', function(err, dbResults) {
if (err) { console.error('[FAILED]', err); return; }
if (err) { log.error(err); return; }
fs.readdir(options['migrations-dir'], function(err, files) {
if (err) { console.error('[FAILED]', err); return; }
if (err) { log.error(err); return; }
doUp(db, dbResults, files, options);
});
});
Expand All @@ -182,11 +179,11 @@ exports.down = function(options) {
var env = config.getCurrent();
var driver = require('./driver');
driver.connect(env, function(err, db) {
if (err) { console.error('[FAILED]', err); return; }
if (err) { log.error(err); return; }
db.createMigrationsTable(function(err) {
if (err) { console.error('[FAILED]', err); return; }
if (err) { log.error(err); return; }
db.all('SELECT * FROM migrations ORDER BY name DESC', function(err, dbResults) {
if (err) { console.error('[FAILED]', err); return; }
if (err) { log.error(err); return; }
doDown(db, dbResults, options);
});
});
Expand All @@ -197,9 +194,9 @@ exports.create = function(options) {
var migration = new Migration(options.title, new Date());
migration.write(function(err) {
if (err) {
console.error('[FAILED]', err);
log.error(err);
} else {
console.log('[SUCCESS]', util.format('Created migration at %s', migration.path));
log.info(util.format('Created migration at %s', migration.path));
}
});
};
Expand Down
7 changes: 2 additions & 5 deletions lib/driver/base.js
Expand Up @@ -2,6 +2,7 @@ var util = require('util');
var events = require('events');
var type = require('../data_type');
var myUtil = require('../util');
var log = require('../log');

module.exports = Base = myUtil.Class.extend({
init: function() {
Expand Down Expand Up @@ -53,7 +54,7 @@ module.exports = Base = myUtil.Class.extend({
if(typeof(arguments[arguments.length-1]) == "function") {
callback = arguments[arguments.length - 1];
} else {
callback = function(err) { if(err) { console.error(err); } };
callback = function(err) { if(err) { log.error(err); } };
}

tableOptions = tableOptions || {};
Expand All @@ -80,10 +81,6 @@ module.exports = Base = myUtil.Class.extend({
throw new Error('not yet implemented');
},

createColumnDef: function(tableName, newTableName, callback) {
throw new Error('not yet implemented');
},

addColumn: function(tableName, columnName, columnSpec, callback) {
var def = this.createColumnDef(columnName, this.normalizeColumnSpec(columnSpec));
var sql = util.format('ALTER TABLE %s ADD COLUMN %s', tableName, def);
Expand Down
10 changes: 10 additions & 0 deletions lib/log.js
@@ -0,0 +1,10 @@
var myUtil = require('./util');

exports.info = console.info.bind(console, '[INFO]');
exports.warn = console.warn.bind(console, '[WARN]');
exports.error = console.error.bind(console, '[ERROR]');
exports.verbose = function() {
if (global.verbose) {
console.log.bind(console, '[INFO]').apply(console, arguments);
}
};

0 comments on commit c184b26

Please sign in to comment.