diff options
author | jessib <jessib@riseup.net> | 2013-09-30 11:02:35 -0700 |
---|---|---|
committer | jessib <jessib@riseup.net> | 2013-09-30 11:02:35 -0700 |
commit | 7b2b5472ffc306fff2cdd49cf8415a035093eb5d (patch) | |
tree | f556e921c1211b1b55f4eeefeef08c11920684cf /lib | |
parent | 83c323fbe10249a54d6383189fd9279d82c7d010 (diff) | |
parent | 27ad1b6d833213cf50edb1d3dab60abe243be123 (diff) |
Merge pull request #1 from azul/feature/logging
log to file or syslog instead of STDOUT
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tapicero.rb | 7 | ||||
-rw-r--r-- | lib/tapicero/config.rb | 31 | ||||
-rw-r--r-- | lib/tapicero/couch_changes.rb | 9 | ||||
-rw-r--r-- | lib/tapicero/user_database.rb | 4 | ||||
-rw-r--r-- | lib/tapicero_daemon.rb | 6 |
5 files changed, 44 insertions, 13 deletions
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) |