summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjessib <jessib@riseup.net>2013-09-30 11:02:35 -0700
committerjessib <jessib@riseup.net>2013-09-30 11:02:35 -0700
commit7b2b5472ffc306fff2cdd49cf8415a035093eb5d (patch)
treef556e921c1211b1b55f4eeefeef08c11920684cf
parent83c323fbe10249a54d6383189fd9279d82c7d010 (diff)
parent27ad1b6d833213cf50edb1d3dab60abe243be123 (diff)
Merge pull request #1 from azul/feature/logging
log to file or syslog instead of STDOUT
-rw-r--r--Gemfile.lock2
-rwxr-xr-xbin/tapicero1
-rw-r--r--config/default.yaml5
-rw-r--r--lib/tapicero.rb7
-rw-r--r--lib/tapicero/config.rb31
-rw-r--r--lib/tapicero/couch_changes.rb9
-rw-r--r--lib/tapicero/user_database.rb4
-rw-r--r--lib/tapicero_daemon.rb6
-rw-r--r--tapicero.gemspec1
9 files changed, 51 insertions, 15 deletions
diff --git a/Gemfile.lock b/Gemfile.lock
index f0ad9ca..f9bfda6 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -4,6 +4,7 @@ PATH
tapicero (0.1.0)
couchrest (~> 1.1.3)
daemons
+ syslog_logger (~> 2.0.0)
yajl-ruby
GEM
@@ -24,6 +25,7 @@ GEM
rake (10.1.0)
rest-client (1.6.7)
mime-types (>= 1.16)
+ syslog_logger (2.0.1)
yajl-ruby (1.1.0)
PLATFORMS
diff --git a/bin/tapicero b/bin/tapicero
index d3b9d78..9a5a7ee 100755
--- a/bin/tapicero
+++ b/bin/tapicero
@@ -26,6 +26,7 @@ end
# Graceful Ctrl-C
Signal.trap("SIGINT") do
+ Tapicero.logger.warn "Received SIGINT - stopping tapicero"
puts "\nQuit - leaving tapicero"
exit
end
diff --git a/config/default.yaml b/config/default.yaml
index e1ba45b..cb6a2be 100644
--- a/config/default.yaml
+++ b/config/default.yaml
@@ -30,5 +30,6 @@ security:
# a restart:
seq_file: "/var/log/leap/tapicero.seq"
-# log file - Once we use a logger rather than writing to stdout we'll use this:
-# log_file: "/var/log/leap/tapicero.log"
+# Configure log_file like this if you want to log to a file instead of syslog:
+# log_file: "/var/leap/log/tapicero.log"
+log_level: info
diff --git a/lib/tapicero.rb b/lib/tapicero.rb
index 967a9a7..fd66030 100644
--- a/lib/tapicero.rb
+++ b/lib/tapicero.rb
@@ -5,6 +5,13 @@ unless defined? LEAP_CA_CONFIG
LEAP_CA_CONFIG = '/etc/leap/tapicero.yaml'
end
+module Tapicero
+ class <<self
+ attr_accessor :logger
+ end
+end
+
+
#
# Load Config
# this must come first, because CouchRest needs the connection defined before the models are defined.
diff --git a/lib/tapicero/config.rb b/lib/tapicero/config.rb
index ad8076a..7d7d0e1 100644
--- a/lib/tapicero/config.rb
+++ b/lib/tapicero/config.rb
@@ -10,11 +10,15 @@ module Tapicero
attr_accessor :security
attr_accessor :seq_file
attr_accessor :log_file
+ attr_accessor :log_level
def self.load(base_dir, *configs)
- configs.each do |file_path|
- load_config find_file(base_dir, file_path)
+ loaded = configs.collect do |file_path|
+ file = find_file(base_dir, file_path)
+ load_config(file)
end
+ init_logger
+ log_loaded_configs(loaded.compact)
end
def couch_host(conf = nil)
@@ -30,12 +34,25 @@ module Tapicero
private
+ def init_logger
+ if log_file
+ require 'logger'
+ Tapicero.logger = Logger.new(log_file)
+ else
+ require 'syslog/logger'
+ Tapicero.logger = Syslog::Logger.new('tapicero')
+ end
+ Tapicero.logger.level = Logger.const_get(log_level.upcase)
+ end
+
def load_config(file_path)
return unless file_path
- puts " * Loading configuration #{file_path}"
load_settings YAML.load(File.read(file_path))
+ return file_path
rescue NoMethodError => exc
- STDERR.puts "ERROR in file #{file_path}"
+ init_logger
+ Tapicero.logger.fatal "Error in file #{file_path}"
+ Tapicero.logger.fatal exc
exit(1)
end
@@ -72,5 +89,11 @@ module Tapicero
return File.expand_path(file_path, base_dir) if File.exists?(File.expand_path(file_path, base_dir))
return nil
end
+
+ def log_loaded_configs(files)
+ files.each do |file|
+ Tapicero.logger.info "Loaded config from #{file} ."
+ end
+ end
end
end
diff --git a/lib/tapicero/couch_changes.rb b/lib/tapicero/couch_changes.rb
index 55a5489..376eb11 100644
--- a/lib/tapicero/couch_changes.rb
+++ b/lib/tapicero/couch_changes.rb
@@ -21,8 +21,8 @@ module Tapicero
end
def listen
- puts "listening..."
- puts "Starting at sequence #{since}"
+ Tapicero.logger.info "listening..."
+ Tapicero.logger.debug "Starting at sequence #{since}"
db.changes :feed => :continuous, :since => since, :heartbeat => 1000 do |hash|
callbacks(hash)
end
@@ -44,13 +44,14 @@ module Tapicero
end
def read_seq(seq_filename)
+ Tapicero.logger.debug "Looking up sequence here: #{seq_filename}"
FileUtils.touch(seq_filename)
unless File.writable?(seq_filename)
raise StandardError.new("Can't access sequence file")
end
@since = File.read(seq_filename)
rescue Errno::ENOENT => e
- puts "No sequence file found. Starting from scratch"
+ Tapicero.logger.warn "No sequence file found. Starting from scratch"
end
def store_seq(seq)
@@ -62,7 +63,7 @@ module Tapicero
#
def fetch_last_seq
hash = db.changes :limit => 1, :descending => true
- puts "starting at seq: " + hash["last_seq"]
+ Tapicero.logger.info "starting at seq: " + hash["last_seq"]
return hash["last_seq"]
end
diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb
index fcdd272..84ed300 100644
--- a/lib/tapicero/user_database.rb
+++ b/lib/tapicero/user_database.rb
@@ -19,8 +19,8 @@ module Tapicero
def secure(security)
# let's not overwrite if we have a security doc already
return if secured?
- puts security.to_json
- puts "-> #{security_url}"
+ Tapicero.logger.info "Writing Security to #{security_url}"
+ Tapicero.logger.debug security.to_json
CouchRest.put security_url, security
end
diff --git a/lib/tapicero_daemon.rb b/lib/tapicero_daemon.rb
index a5e41eb..e38a4ad 100644
--- a/lib/tapicero_daemon.rb
+++ b/lib/tapicero_daemon.rb
@@ -8,14 +8,14 @@
require 'tapicero'
module Tapicero
- puts " * Observing #{Config.couch_host_without_password}"
- puts " * Tracking #{Config.users_db_name}"
+ Tapicero.logger.info "Observing #{Config.couch_host_without_password}"
+ Tapicero.logger.info "Tracking #{Config.users_db_name}"
# stream = CouchStream.new(Config.couch_host + '/' + Config.users_db_name)
db = CouchRest.new(Config.couch_host).database(Config.users_db_name)
users = CouchChanges.new(db, Config.seq_file)
users.created do |hash|
- puts "Created user " + hash['id']
+ Tapicero.logger.debug "Created user " + hash['id']
db = UserDatabase.new(Config.couch_host, Config.db_prefix + hash['id'])
db.create
db.secure(Config.security)
diff --git a/tapicero.gemspec b/tapicero.gemspec
index 352e1a8..dd3a4ca 100644
--- a/tapicero.gemspec
+++ b/tapicero.gemspec
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.add_dependency "couchrest", "~> 1.1.3"
s.add_dependency "daemons"
s.add_dependency "yajl-ruby"
+ s.add_dependency "syslog_logger", "~> 2.0.0"
s.add_development_dependency "minitest", "~> 3.2.0"
s.add_development_dependency "mocha"
s.add_development_dependency "rake"