Rails日志文件“未打开”

时间:2015-07-28 10:33:47

标签: ruby-on-rails ruby logging ruby-1.9.3 ruby-1.8.7

我正在将我的应用程序从ruby 1.8.7迁移到1.9.3(以及稍后升级Rails)。但是我在登录1.9.3时遇到了一些问题。 通过在控制台或服务器启动时在environment.rb中使用以下代码

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

我得到了

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

AuditLogger定义如下:

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

如果我使用irb手动执行此操作

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

我怀疑这个班级可能已经被另一个班级覆盖了?所以我添加了一些代码来确定当前使用的文件类

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

@file.methods.sort

结果

[:!, :!=, :!~, :<<, :<=>, :==, :===, :=~, :__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]

根据Ruby 1.9.3 File class definition,应该有一些方法,比如'可读?','可写?'在方法的列表中。但他们不是。

这里也是我的rubygems列表,可能会有所帮助

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)

问题:有人知道如何克服这个问题吗?

1 个答案:

答案 0 :(得分:0)

您使用的是旧版本的rails,我不确定当时处理的是什么,但Logger.new应该接受一个简单的文件名作为字符串。

忘记File.open并让它弄清楚如何处理文件:

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