Class: UniqLogger::Base
- Inherits:
-
Object
- Object
- UniqLogger::Base
- Defined in:
- lib/uniq_logger/base.rb
Instance Method Summary collapse
- #config ⇒ Object
- #create(uniq_id, data_to_save = []) ⇒ Object
- #create_local_log_entry(uniq_id, data_to_save) ⇒ Object
- #create_remote_log_entry(uniq_id, data_to_save) ⇒ Object
- #get_current_logger_prefix ⇒ Object
-
#initialize(opts = {}, &block) ⇒ Base
constructor
A new instance of Base.
- #is_uniq_id_in_log_history?(uniq_id, logfile) ⇒ Boolean
Constructor Details
#initialize(opts = {}, &block) ⇒ Base
Returns a new instance of Base.
3 4 5 |
# File 'lib/uniq_logger/base.rb', line 3 def initialize(opts={}, &block) Configuration.configure_with_path end |
Instance Method Details
#config ⇒ Object
7 8 9 |
# File 'lib/uniq_logger/base.rb', line 7 def config Configuration.config end |
#create(uniq_id, data_to_save = []) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/uniq_logger/base.rb', line 11 def create(uniq_id, data_to_save=[]) if config["logfile_destination"] == "local" return create_local_log_entry(uniq_id, data_to_save) elsif config["logfile_destination"] == "remote" return create_remote_log_entry(uniq_id, data_to_save) else puts "logfile_destination is not set to [local,remote]" return false end end |
#create_local_log_entry(uniq_id, data_to_save) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/uniq_logger/base.rb', line 23 def create_local_log_entry(uniq_id, data_to_save) #Use global Logger if config["global_logger"] == true raise "Directory of 'path_to_local_logfiles' '#{config["path_to_local_logfiles"]}' does not exist!" unless File.directory?(config["path_to_local_logfiles"]) logfilename = File.join(config["path_to_local_logfiles"], config["global_log_file_name"]) logfile = File.open( File.(logfilename), "a" ) if is_uniq_id_in_log_history?(uniq_id,logfile) return false end #write Data to Logfile File logfile.puts [uniq_id,data_to_save].flatten.join(config["csv"]["col_sep"]) logfile.close end #Use Log Rotator? if ['day','month','year'].include?(config["log_rotator"]) logger_prefix = get_current_logger_prefix() rotator_logfilename = File.join(config["path_to_local_logfiles"], "#{config['log_rotator_prefix']}#{logger_prefix}.log") rotator_logfile = File.open( File.(rotator_logfilename), "a" ) rotator_logfile.puts [uniq_id,data_to_save].flatten.join(config["csv"]["col_sep"]) rotator_logfile.close end return true end |
#create_remote_log_entry(uniq_id, data_to_save) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/uniq_logger/base.rb', line 75 def create_remote_log_entry(uniq_id, data_to_save) begin auth_token = config["remote"]["auth_token"] server_name = config["remote"]["server"] endpoint = config["remote"]["endpoint"] param_id = config["remote"]["url_param_for_id"] param_data = config["remote"]["url_param_for_data"] uri = URI.parse("#{server_name}#{endpoint}?auth_token=#{auth_token}") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri) if !config["remote"]["basic_auth"]["username"].nil? && !config["remote"]["basic_auth"]["password"].nil? && !config["remote"]["basic_auth"]["username"].empty? && !config["remote"]["basic_auth"]["password"].empty? request.basic_auth(config["remote"]["basic_auth"]["username"], config["remote"]["basic_auth"]["password"]) end request.form_data = { param_id.to_sym => uniq_id, param_data.to_sym => data_to_save.to_json } response = http.request(request) json = JSON.parse response.body if json['response'] == "true" || json['response'] == true return true else return false end rescue puts "Connetion to Server failed" return false end end |
#get_current_logger_prefix ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/uniq_logger/base.rb', line 60 def get_current_logger_prefix case config["log_rotator"] when "day" filename_prefix = Time.now.strftime("%d-%m-%Y") when "month" filename_prefix = Time.now.strftime("%m-%Y") when "year" filename_prefix = Time.now.strftime("%Y") else filename_prefix = "" end return filename_prefix end |
#is_uniq_id_in_log_history?(uniq_id, logfile) ⇒ Boolean
52 53 54 55 56 57 58 |
# File 'lib/uniq_logger/base.rb', line 52 def is_uniq_id_in_log_history?(uniq_id,logfile) if config["validates_uniqness_of_id"] == true regexp_uniq_id = /^#{uniq_id};/ return open(logfile).grep(regexp_uniq_id).any? end return false end |