Skip to content

Commit

Permalink
improved log generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Feb 12, 2019
1 parent bcda81d commit dc0fbfa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
27 changes: 17 additions & 10 deletions lib/nice_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def initialize(attribute)
class << self
attr_accessor :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port,
:last_request, :last_response, :request_id, :use_mocks, :connections,
:active, :auto_redirect
:active, :auto_redirect, :log_files
end

######################################################
Expand All @@ -77,6 +77,7 @@ def self.reset!
@connections = []
@active = 0
@auto_redirect = true
@log_files = []
end
reset!

Expand Down Expand Up @@ -193,22 +194,27 @@ def initialize(args = {})
end

begin
#only the first connection in the run will be deleting
if self.class.last_request.nil?
mode = "w"
else
mode = "a"
end
mode = "a"
log_filename =''
if @log.kind_of?(String)
f = File.new(@log, mode)
#only the first connection in the run will be deleting the log file
log_filename = @log
mode = "w" unless self.class.log_files.include?(log_filename)
f = File.new(log_filename, mode)
f.sync = true
@logger = Logger.new f
elsif @log == :fix_file
f = File.new("nice_http.log", mode)
#only the first connection in the run will be deleting the log file
log_filename = 'nice_http.log'
mode = "w" unless self.class.log_files.include?(log_filename)
f = File.new(log_filename, mode)
f.sync = true
@logger = Logger.new f
elsif @log == :file
f = File.new("nice_http_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.log", mode)
log_filename = "nice_http_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.log"
#only the first connection in the run will be deleting the log file
mode = "w" unless self.class.log_files.include?(log_filename)
f = File.new(log_filename, mode)
f.sync = true
@logger = Logger.new f
elsif @log == :screen
Expand All @@ -219,6 +225,7 @@ def initialize(args = {})
raise InfoMissing, :log
end
@logger.level = Logger::INFO
self.class.log_files << log_filename if mode == 'w'
rescue Exception => stack
raise InfoMissing, :log
@logger = Logger.new nil
Expand Down
2 changes: 1 addition & 1 deletion nice_http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_http'
s.version = '1.3.4'
s.version = '1.3.5'
s.summary = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources."
s.description = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Manage different hosts on the fly. Easily get the value you want from the JSON strings. Use hashes on your requests."
s.authors = ["Mario Ruiz"]
Expand Down

0 comments on commit dc0fbfa

Please sign in to comment.