From 8cf5237c9dab5fd2391342e14a80ff8ccf912881 Mon Sep 17 00:00:00 2001 From: Juan Edi Date: Sat, 2 Jul 2016 10:06:30 -0300 Subject: [PATCH] Use logger instead of puts in non-CLI code --- src/micrate.cr | 20 ++++++++++++++++---- src/micrate/cli.cr | 29 ++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/micrate.cr b/src/micrate.cr index 718dad9..6e05281 100644 --- a/src/micrate.cr +++ b/src/micrate.cr @@ -1,6 +1,8 @@ require "./micrate/*" module Micrate + @@logger : Logger? + def self.db_dir "db" end @@ -93,11 +95,11 @@ module Micrate plan = migration_plan(status, current, target, direction) if plan.empty? - puts "No migrations to run. current version: #{current}" + logger.info "No migrations to run. current version: #{current}" return end - puts "Migrating db, current version: #{current}, target: #{target}" + logger.info "Migrating db, current version: #{current}, target: #{target}" plan.each do |version| migration = all_migrations[version] @@ -108,9 +110,9 @@ module Micrate DB.record_migration(migration, direction, db) - puts "OK #{migration.name}" + logger.info "OK #{migration.name}" rescue e : Exception - puts "An error ocurred executing migration #{migration.version}. Error message is: #{e.message}" + logger.info "An error ocurred executing migration #{migration.version}. Error message is: #{e.message}" return end end @@ -193,6 +195,16 @@ module Micrate return 0 end + def self.logger + @@logger ||= Logger.new(STDOUT).tap do |l| + l.level = Logger::UNKNOWN + end + end + + def self.logger=(logger) + @@logger = logger + end + class UnorderedMigrationsException < Exception getter :versions diff --git a/src/micrate/cli.cr b/src/micrate/cli.cr index b2152a8..3eaec3a 100644 --- a/src/micrate/cli.cr +++ b/src/micrate/cli.cr @@ -1,3 +1,5 @@ +require "logger" + module Micrate module Cli def self.run_up @@ -53,13 +55,13 @@ module Micrate conflicting.each do |version| puts " #{Migration.from_version(version).name}" end - puts - puts "Micrate will not run these migrations because they may have been written with an older database model in mind." - puts "You should probably check if they need to be updated and rename them so they are considered a newer version." + puts " +Micrate will not run these migrations because they may have been written with an older database model in mind. +You should probably check if they need to be updated and rename them so they are considered a newer version." end - def self.help - "micrate is a database migration management system for Crystal projects, *heavily* inspired by Goose (https://bitbucket.org/liamstask/goose/). + def self.print_help + puts "micrate is a database migration management system for Crystal projects, *heavily* inspired by Goose (https://bitbucket.org/liamstask/goose/). Usage: micrate [options] [subcommand options] @@ -70,13 +72,14 @@ Commands: redo Re-run the latest migration status dump the migration status for the current DB create Create the scaffolding for a new migration - dbversion Print the current version of the database - " + dbversion Print the current version of the database" end def self.run + setup_logger + if ARGV.empty? - puts help + print_help return end @@ -95,7 +98,7 @@ Commands: when "dbversion" run_dbversion else - puts help + print_help end rescue e: UnorderedMigrationsException report_unordered_migrations(e.versions) @@ -104,7 +107,15 @@ Commands: puts e.message exit 1 end + end + def self.setup_logger + Micrate.logger = Logger.new(STDOUT).tap do |l| + l.level = Logger::INFO + l.formatter = Logger::Formatter.new do |severity, datetime, progname, message, io| + io << message + end + end end end end