Permalink
Browse files

adding logging to main script

  • Loading branch information...
1 parent f29576e commit 37671d91016939576977765f0395d0bf4040ca13 @romand committed Mar 29, 2012
Showing with 30 additions and 6 deletions.
  1. +22 −6 bin/iron_worker_ng
  2. +1 −0 lib/iron_worker_ng.rb
  3. +7 −0 lib/iron_worker_ng/logging.rb
View
28 bin/iron_worker_ng
@@ -19,6 +19,11 @@ if ENV['IRON_IO_TOKEN'].nil? || ENV['IRON_IO_PROJECT_ID'].nil?
exit 1
end
+logger = Logger.new(ENV['IRON_WORKER_LOG_FILE'] || STDERR)
+log_level = ENV['IRON_WORKER_LOG_LEVEL'] || 'INFO'
+logger.level = eval "Logger::#{log_level}"
+logger.datetime_format = "%F %T.%L "
+
client = IronWorkerNG::Client.new(:token => ENV['IRON_IO_TOKEN'], :project_id => ENV['IRON_IO_PROJECT_ID'])
if command == 'codes.create'
@@ -58,23 +63,26 @@ if command == 'codes.create'
begin
opts.parse!
rescue OptionParser::ParseError
- puts $!.to_s
+ logger.fatal $!.report
exit 1
end
if execute_features.size == 0
- puts opts
+ logger.fatal "No features to execute specified\n#{opts}"
exit 1
end
runtime = runtimes[0][:name] if runtime.nil?
+ logger.info 'Creating code bundle'
code = runtimes.find { |r| r[:name] == runtime }[:klass].new(name)
execute_features.each do |f|
+ logger.info "Executing feature #{f[:name]}"
code.send(f[:name], *f[:args])
end
+ logger.info 'Uploading code bundle'
client.codes.create(code)
elsif command == 'tasks.create' || command == 'schedules.create'
name = nil
@@ -140,12 +148,12 @@ elsif command == 'tasks.create' || command == 'schedules.create'
begin
opts.parse!
rescue OptionParser::ParseError
- puts $!.to_s
+ logger.fatal $!.to_s
exit 1
end
if name.nil?
- puts opts
+ logger.fatal "Required option --name is not specified\n#{opts}"
exit 1
end
@@ -165,11 +173,15 @@ elsif command == 'tasks.create' || command == 'schedules.create'
id = nil
if command == 'tasks.create'
+ logger.info 'Creating task'
id = client.tasks.create(name, params, options).id
else
+ logger.info 'Create schedule'
id = client.schedules.create(name, params, options).id
end
+ logger.debug "id = #{id}"
+
print id if print_id
elsif command == 'tasks.log'
task_id = nil
@@ -190,21 +202,23 @@ elsif command == 'tasks.log'
begin
opts.parse!
rescue OptionParser::ParseError
- puts $!.to_s
+ logger.fatal $!.to_s
exit 1
end
if task_id.nil?
- puts opts
+ logger.fatal "Required option --task-id is not specified\n#{opts}"
exit 1
end
log = ''
+ logger.info 'Fetching task log'
if live
begin
log = client.tasks.log(task_id)
rescue IronWorkerNG::APIClientError
+ logger.warn $!.report
end
else
log = client.tasks.log(task_id)
@@ -220,6 +234,7 @@ elsif command == 'tasks.log'
print next_log[log.length .. - 1]
log = next_log
rescue IronWorkerNG::APIClientError
+ logger.warn $!.report
end
end
end
@@ -228,6 +243,7 @@ elsif command == 'tasks.log'
next_log = client.tasks.log(task_id)
print next_log[log.length .. - 1]
rescue IronWorkerNG::APIClientError
+ logger.warn $!.report
end
end
end
View
1 lib/iron_worker_ng.rb
@@ -1,6 +1,7 @@
require_relative 'iron_worker_ng/version'
require_relative 'iron_worker_ng/logger'
require_relative 'iron_worker_ng/client'
+require_relative 'iron_worker_ng/logging'
require_relative 'iron_worker_ng/code/base'
require_relative 'iron_worker_ng/code/ruby'
require_relative 'iron_worker_ng/code/java'
View
7 lib/iron_worker_ng/logging.rb
@@ -0,0 +1,7 @@
+require 'logger'
+
+class StandardError
+ def report
+ %{#{self.class}: #{message}\n#{backtrace.join("\n")}}
+ end
+end

0 comments on commit 37671d9

Please sign in to comment.