# Use logger class

**URL:** <https://www.ruby-forum.com/t/use-logger-class/121891>\
**Category:** Rails\
**Created:** [December 2, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/use-logger-class/121891 "2007-12-02T12:52:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shafi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@shafi](https://www.ruby-forum.com/u/shafi)\
**Post date:** [December 2, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/use-logger-class/121891/1 "2007-12-02T12:52:28Z")

</div>

Hello all,

i wanna use logger to log all errors and exceptions that occurs in run  
time, so i create new file “logfile.log” and put it in /log path in  
rails application and put these lines of code in environment.rb file

file = File.open("#{RAILS\_ROOT}/log/logfile.log", File::WRONLY |  
File::CREAT)  
logger = Logger.new(file)  
logger.level = Logger::ERROR

and inside actions with each part of code that is surrounded with begin  
and rescue i put this line

logger.error “#{e.message}”

ex:

begin  
blah blah  
rescue Exception =\> e

logger.error “#{e.message}”

end

but logfile.log always empty.  
so please anyone help me in this point  
thanks in advanced

---

<div class="post-metadata">

**Author:** ![shafi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@shafi](https://www.ruby-forum.com/u/shafi)\
**Post date:** [December 2, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/use-logger-class/121891/2 "2007-12-02T17:29:59Z")

</div>

Try passing the file path to Logger.new and let it deal w/ creating the  
file. File.open is unnecessary.

-Bill

---

<div class="post-metadata">

**Author:** ![shafi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@shafi](https://www.ruby-forum.com/u/shafi)\
**Post date:** [December 2, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/use-logger-class/121891/3 "2007-12-02T17:39:24Z")

</div>

On 2 Dec 2007, at 16:29, William P. wrote:

> > i wanna use logger to log all errors and exceptions that occurs in  
> > begin  
> > and rescue i put this line
> > 
> > logger.error “#{e.message}”

Are you sure that the logger at this point is the logger you’ve  
created rather than the logger rails creates for you ?  
If you want to change the logger rails creates for you, then in your  
environment.rb you need to set config.logger to the instance of Logger  
you create

Fred
