ruby - Writing error messages in log file -


i have question using logger library in writing error messages log file.

i trying establish db connection, , want print success or failure message in log file. example, if user enters incorrect db port, db connection cannot established. in case, want print "nativeexception: java.sql.sqlexception: network adapter not establish connection" error in log file.

below piece of code doing it:

url = "jdbc:oracle:thin:#{host}:#{port}/#{db}" log = logger.new('log.txt')     log.level = logger::warn  con_props = java.util.properties.new     con_props.setproperty("user", usr)     con_props.setproperty("password", pwd)      conn=java::oracle.jdbc::oracledriver.new.connect("#{url}", con_props)     log.error "#{conn}" 

this creates log file, not write message in it. can me this?

logfile looks this:

# logfile created on 2014-03-10 12:07:30 -0700 logger.rb/v1.2.7 

if understand correctly, want print error when connection fails. if that's case - need catch exception:

url = "jdbc:oracle:thin:#{host}:#{port}/#{db}" log = logger.new('log.txt') log.level = logger::info  con_props = java.util.properties.new con_props.setproperty("user", usr) con_props.setproperty("password", pwd)  begin   conn=java::oracle.jdbc::oracledriver.new.connect("#{url}", con_props)   log.info "connection established!" rescue => e   log.error "connection failed - #{e}"   raise e end 

to create logger appends existing file read documentation:

create logger specified file.

file = file.open('foo.log', file::wronly | file::append) # create new (and remove old) logfile, add file::creat like; #   file = open('foo.log', file::wronly | file::append | file::creat) logger = logger.new(file) 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -