# Configure logger, reopen class

**URL:** <https://www.ruby-forum.com/t/configure-logger-reopen-class/202564>\
**Category:** Rails\
**Created:** [February 1, 2011, 6:29pm UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564 "2011-02-01T18:29:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 1, 2011, 6:29pm UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/1 "2011-02-01T18:29:56Z")

</div>

Hello,  
I whish configure logger, adding a prefix in all “logger.error(…)”, a  
prefix like “ERROR------------------\>”, to be able, then to search in  
log file, easily.

And to do that I had the idea of reopen Logger class, like this:

- In a file placed in \initializers\ I put

But this doesn’t do anything, ¿ where do I have to put this code ?

- And if I put in config/environments/development.rb

raises an error: [super: no superclass method `error’ for  
#Logger:0x3d7bae8]

Could someone guide me for the right way? I’m absolutly lost.

What does usually people do to look for in the “great” production.log  
file?

I know, if I put config.log\_level = :warn, this file would be thin, but  
I supose than this “info” messages are important to understand the whole  
error.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 1, 2011, 11:28pm UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/2 "2011-02-01T23:28:17Z")

</div>

2011/2/1 Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I whish configure logger, adding a prefix in all “logger.error(..)”, a  
> prefix like “ERROR------------------\>”, to be able, then to search in  
> log file, easily.

> Could someone guide me for the right way? I’m absolutly lost.

I don’t know about the “right” way 🙂 but I wanted timestamps for one  
of my apps, so put this in my application\_controller.rb :

class ActiveSupport::BufferedLogger  
SEVERITIES = { 0 =\> ‘DEBUG’, 1 =\> ‘INFO’, 2 =\> ‘WARN’, 3 =\>  
‘ERROR’, 4 =\> ‘FATAL’, 5 =\> ‘UNKNOWN’ } unless defined?(SEVERITIES)

```
def add(severity, message = nil, progname = nil, &block)
  return if @level > severity
  message = (message || (block && block.call) || progname).to_s
  message = "#{Time.current.getlocal.strftime('%F-%H:%M:%S')}

```

#{SEVERITIES[severity]} #{message}\n" unless message[-1] == ?\n  
buffer \<\< message  
auto\_flush  
message  
end  
end

There certainly could be other ways, but this works.

## HTH!

Hassan S. ------------------------ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 2, 2011, 10:30am UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/3 "2011-02-02T10:30:09Z")

</div>

¿ Is this solution accessible from Models ? “logger (”…") in a model,  
¿reach this code ( class ActiveSupport::BufferedLogger)?

thanks you

I like this solution ([#56 The Logger - RailsCasts](http://railscasts.com/episodes/56-the-logger))

in environment.rb:

class Logger  
def format\_message(level, time, progname, msg)  
“#{time.to\_s(:db)} #{level} – #{msg}\n”  
end  
end

It seems easy , but I can’t do it working for Rails 3.

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 2, 2011, 7:47pm UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/4 "2011-02-02T19:47:56Z")

</div>

2011/2/2 Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Is this solution accessible from Models ? “logger (”…") in a model,  
> reach this code ( class ActiveSupport::BufferedLogger)?

I have logging in models, yes, and it works fine (at least on 2.3.x).

> I like this solution ([http://railscasts.com/episodes/56-the-logger](http://railscasts.com/episodes/56-the-logger))

> It seems easy , but I can’t do it working for Rails 3.

Sorry, haven’t tried to convert this app yet 🙂

–  
Hassan S. ------------------------ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 3, 2011, 8:24am UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/5 "2011-02-03T08:24:47Z")

</div>

2011/2/1 Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> super(args)  
> end

# you want to put it in as a class method

class \<\< self

> def error(msg)  
> super(“ERROR------------\>”+msg)  
> end

end # class methods Object.method\_name

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> –  
> You received this message because you are subscribed to the Google G. “Ruby  
> on Rails: Talk” group.  
> To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> To unsubscribe from this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

## – make haste slowly festina lente \

mobile +1\_415\_632\_6001  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://robotarmyma.de](http://robotarmyma.de)

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 3, 2011, 8:32am UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/6 "2011-02-03T08:32:18Z")

</div>

2011/2/1 Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> super(args)  
> end  
> def error(msg)

#also - this may give you an error

# i remember this as

msg = %%ERROR#{‘-’\*5}\>#{msg}%  
super

> raises an error: [super: no superclass method `error’ for

## – make haste slowly festina lente \

mobile +1\_415\_632\_6001  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://robotarmyma.de](http://robotarmyma.de)

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 3, 2011, 11:57am UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/7 "2011-02-03T11:57:56Z")

</div>

Curtis j Schofield wrote in post #979342:

> 2011/2/1 Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > super(args)  
> > end  
> > def error(msg)
> 
> #also - this may give you an error
> 
> # i remember this as
> 
> msg = %%ERROR#{‘-’\*5}\>#{msg}%  
> super
> 
> > raises an error: [super: no superclass method `error’ for

What I did, exactly is:  
class Logger  
def initialize(args=nil)  
super(args)  
end  
def error(msg)  
super(“ERROR------------\>”+msg)  
end  
end

this is in a file.rb in config/initializers/

and raises the error I said

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 3, 2011, 12:36pm UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/8 "2011-02-03T12:36:31Z")

</div>

On Feb 3, 10:57am, Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > raises an error: [super: no superclass method `error’ for
> 
> this is in a file.rb in config/initializers/
> 
> and raises the error I said

This fails because the superclass of Logger doesn’t have an error  
method - it’s Logger itself that provides it. Either have your class  
subclass from Logger and set your application to use that logger  
class, or alias the error method and instead of calling the super call  
the aliased name (depending on how the class is setup there’s also a  
trick you can play with modules)

Fred

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 3, 2011, 5:01pm UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/9 "2011-02-03T17:01:07Z")

</div>

On Feb 3, 2011, at 2:57 AM, Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> ```
> super("ERROR------------>"+msg)
> 
> ```
> 
> end  
> end
> 
> this is in a file.rb in config/initializers/
> 
> and raises the error I said

Did you read my other response about class methods?

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 4, 2011, 4:49pm UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/10 "2011-02-04T16:49:51Z")

</div>

On Feb 3, 2011, at 2:16 PM, Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 
> > 
> > def error(msg)  
> > super(“ERROR------------\>”+msg)  
> > end  
> > end # class methods Object.method\_name
> 
> and where do I have to put this code?
> 
> Thanks for patience

Hey no worries - so

class A  
def self.error(msg)  
#comment - class method ‘error’  
end  
end

your first post had the right idea - only error is a method that  
belongs to the Logger instance - not the dynamic instance.

In A I have a method ‘error’ that belongs to the A instance.

A.new.error

Would result in a NoMethod on the execution of the code.

class A  
def error  
end  
end

A.new.error

Will work now.

You can try this in irb.

You original code had a method that would go on a new instance of  
Logger - what you needed was a method ‘error’ on the Logger instance  
( refered to in object oriented programming as a the Logger Class)

When the error mentioned that the method could not be found - it is  
because the ‘super’ version of the method is actually in Logger the  
class method error .

Logger.info “assuming we are talking about the same logger”

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 3, 2011, 11:16pm UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/11 "2011-02-03T23:16:17Z")

</div>

Curtis wrote in post #979429:

> On Feb 3, 2011, at 2:57 AM, Albert C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > 
> > 
> > ```
> > super("ERROR------------>"+msg)
> > 
> > ```
> > 
> > end  
> > end
> > 
> > this is in a file.rb in config/initializers/
> > 
> > and raises the error I said
> 
> Did you read my other response about class methods?

Yes but I’m still truing to do that, what do you mean? with

class \<\< self

> def error(msg)  
> super(“ERROR------------\>”+msg)  
> end  
> end # class methods Object.method\_name

and where do I have to put this code?

Thanks for patience

---

<div class="post-metadata">

**Author:** ![xarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df788c/32.png) [@xarc](https://www.ruby-forum.com/u/xarc)\
**Post date:** [February 6, 2011, 1:22am UTC](https://www.ruby-forum.com/t/configure-logger-reopen-class/202564/12 "2011-02-06T01:22:15Z")

</div>

Thanks for your time, but i am not able to make this working.

Where can I see the code, and the files (if conig/application.rb. or  
where ever is), than explain how to customize the logger?

thanks again

(I am new in Ruby but not in Visual Foxpro, if somebody needs my help, I  
can exchange knowledge)
