Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Logfiles "not opened for writing"

I am in a process of migrating my application from ruby 1.8.7 to 1.9.3 (and later upgrading Rails). However I geht some problems with logging in 1.9.3. By using the following code in environment.rb when console or server starts up

logfile = File.open(File.join(RAILS_ROOT,'/log/call_log.log'), 'a')
CALL_LOGGER = AuditLogger.new(logfile)
CALL_LOGGER.info "CALL_LOGGER: Server started."

I get

/usr/local/rvm/scripts/irbrc.rb:32:in `write': not opened for writing (IOError)

AuditLogger is defined as follows:

class AuditLogger < Logger
  def format_message(severity, timestamp, progname, msg)
      "#{timestamp.strftime("%Y-%m-%d %H:%M:%S")} #{progname} #{severity} #{msg}\n" 
  end 
end

If I do this manually with irb, this works

1.9.3-p551 :006 > logfile = File.open(File.join('/var/my_app/log/call_log.log'), 'a')
=> #<File:/var/my_app/log/call_log.log>
 => #<AuditLogger:0x00000001ad95e8 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x00000001ad95c0 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x00000001ad9570 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/var/my_app/log/call_log.log>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x00000001ad9548 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x00000001ad94f8>>>>
1.9.3-p551 :011 > CALL_LOGGER.info "CALL_LOGGER: Server started."
 => true

I suspect that the class may have been overriden by another class? So I added some code to determine the class of the file which was currently used

@file.class.ancestors
[File, IO, File::Constants, Enumerable, Object, ActiveSupport::Dependencies::Loadable, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]

@file.methods.sort

results in

[:!, :!=, :!~, :<<, :<=>, :==, :===, :=~, :__id__, :__send__, :`, :acts_like?, :advise, :all?, :any?, :as_json, :atime, :autoclose=, :autoclose?, :binmode, :binmode?, :blank?, :breakpoint, :bytes, :chars, :chmod, :chown, :chunk, :class, :class_eval, :clone, :close, :close_on_exec=, :close_on_exec?, :close_read, :close_write, :closed?, :codepoints, :collect, :collect_concat, :copy_instance_variables_from, :count, :ctime, :cycle, :daemonize, :dclone, :debugger, :define_singleton_method, :detect, :display, :drop, :drop_while, :dup, :duplicable?, :each, :each_byte, :each_char, :each_codepoint, :each_cons, :each_entry, :each_line, :each_slice, :each_with_index, :each_with_object, :enable_warnings, :entries, :enum_for, :eof, :eof?, :eql?, :equal?, :exclude?, :extend, :extend_with_included_modules_from, :extended_by, :external_encoding, :fcntl, :fdatasync, :fileno, :find, :find_all, :find_index, :first, :flat_map, :flock, :flush, :freeze, :frozen?, :fsync, :getbyte, :getc, :gets, :grep, :group_by, :hash, :html_safe?, :include?, :index_by, :initialize_clone, :initialize_dup, :inject, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_values, :instance_variable_defined?, :instance_variable_get, :instance_variable_names, :instance_variable_set, :instance_variables, :internal_encoding, :ioctl, :is_a?, :isatty, :kind_of?, :lineno, :lineno=, :lines, :load_with_new_constant_marking, :lstat, :many?, :map, :max, :max_by, :member?, :metaclass, :metaclass_with_deprecation, :metaclass_without_deprecation, :method, :methods, :min, :min_by, :minmax, :minmax_by, :mtime, :nil?, :none?, :object_id, :one?, :partition, :path, :pid, :pos, :pos=, :presence, :present?, :pretty_inspect, :pretty_print, :pretty_print_cycle, :pretty_print_inspect, :pretty_print_instance_variables, :print, :printf, :private_methods, :protected_methods, :psych_to_yaml, :psych_y, :public_method, :public_methods, :public_send, :putc, :puts, :read, :read_nonblock, :readbyte, :readchar, :readline, :readlines, :readpartial, :reduce, :reject, :remove_subclasses_of, :reopen, :require, :require_association, :require_dependency, :require_library_or_gem, :require_or_load, :respond_to?, :respond_to_missing?, :returning, :reverse_each, :rewind, :seek, :select, :send, :set_encoding, :silence_stderr, :silence_stream, :silence_warnings, :singleton_class, :singleton_methods, :size, :slice_before, :sort, :sort_by, :stat, :subclasses_of, :sum, :suppress, :syck_to_yaml, :sync, :sync=, :sysread, :sysseek, :systemu, :syswrite, :taguri, :taguri=, :taint, :tainted?, :take, :take_while, :tap, :tell, :to_a, :to_enum, :to_hash, :to_i, :to_io, :to_json, :to_param, :to_path, :to_query, :to_s, :to_set, :to_yaml, :to_yaml_properties, :to_yaml_style, :truncate, :trust, :try, :tty?, :ungetbyte, :ungetc, :unloadable, :untaint, :untrust, :untrusted?, :with_options, :write, :write_nonblock, :zip]

According to Ruby 1.9.3 File class definition there should be some methods like 'readable?', 'writable?' in the method's list. But they aren't.

Here is also my list of rubygems which may be helpful

actionmailer (2.3.18)
actionpack (2.3.18)
activerecord (2.3.18)
activeresource (2.3.18)
activesupport (2.3.18)
archive-tar-minitar (0.5.2)
bigdecimal (1.1.0)
bundler (1.7.6)
bundler-unload (1.0.2)
cgi_multipart_eof_fix (2.5.0)
charlock_holmes (0.6.9)
columnize (0.9.0)
daemons (1.0.10)
debugger-ruby_core_source (1.3.8)
eventmachine (1.0.3)
executable-hooks (1.3.2)
fastercsv (1.5.5)
fastthread (1.0.7)
gem-wrappers (1.2.7)
gem_plugin (0.2.3)
geoip-c (0.9.1)
hoe (2.7.0)
hpricot (0.8.6)
httpclient (2.3.4.1)
image_size (1.2.0)
imagesize (0.1.1)
io-console (0.3)
io-tail (0.0.3)
json (1.8.1, 1.5.5)
json_pure (1.8.2, 1.8.1)
juggernaut (2.1.1)
linecache19 (0.5.12)
macaddr (1.6.7)
mechanize (2.0.1)
memcache-client (1.8.5)
mime-types (1.16)
mini_portile (0.6.2, 0.5.2)
minitest (2.5.1)
mongrel (1.2.0.pre2)
mysql (2.9.1)
net-http-digest_auth (1.4)
net-http-persistent (1.9)
nokogiri (1.6.6.2, 1.5.0)
oj (2.5.5)
rack (1.1.6)
rails (2.3.18)
rake (10.1.1, 0.9.2.2)
rbx-require-relative (0.0.9)
rcov (0.9.11)
rdoc (4.1.1, 3.9.5)
redis (3.2.1, 3.0.7)
ruby-debug-base19 (0.11.25)
ruby-debug-base19x (0.11.31)
ruby-debug-ide (0.4.22)
ruby-ole (1.2.11.8)
ruby_core_source (0.1.5)
rubyforge (2.0.4)
rubygems-bundler (1.4.4)
rubyzip (1.1.7)
rvm (1.11.3.9)
soap4r (1.5.8)
spreadsheet (0.9.7)
svg-graph (1.0.5)
systemu (2.6.5, 2.6.4)
thin (1.6.2)
tins (0.13.2)
uuid (2.3.7)
vpim (13.11.11)
webrobots (0.1.1)
xmpp4r (0.5)

Question: Anybody knows how to overcome this issue?

like image 859
peter247 Avatar asked Jul 28 '15 10:07

peter247


1 Answers

You're using an old version of rails, I'm not sure how things were handled back then, but Logger.new should accept a simple filename as a string.

Forget File.open and let it figure out how to handle the file:

logfile = File.join(Rails.root, '/log/call_log.log')
CALL_LOGGER = AuditLogger.new(logfile)
CALL_LOGGER.info "CALL_LOGGER: Server started."
like image 171
tompave Avatar answered Nov 03 '22 00:11

tompave