Configure logger, reopen class

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

    def initialize(args=nil)
    super(args)
    end
    def error(msg)
    super(“ERROR------------>”+msg)
    end
    end

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

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

    config.logger = Logger.new(STDOUT)

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.

2011/2/1 Albert C. [email protected]:

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 :slight_smile: 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. ------------------------ [email protected]
twitter: @hassan

¿ 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)

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.

2011/2/2 Albert C. [email protected]:

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 (#56 The Logger - RailsCasts)

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

Sorry, haven’t tried to convert this app yet :slight_smile:


Hassan S. ------------------------ [email protected]
twitter: @hassan

2011/2/1 Albert C. [email protected]:

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/.


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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


make haste slowly
festina lente \

mobile +1_415_632_6001
[email protected]
http://robotarmyma.de

2011/2/1 Albert C. [email protected]:

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
[email protected]
http://robotarmyma.de

Curtis j Schofield wrote in post #979342:

2011/2/1 Albert C. [email protected]:

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

On Feb 3, 10:57am, Albert C. [email protected] 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

On Feb 3, 2011, at 2:57 AM, Albert C. [email protected] 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?

On Feb 3, 2011, at 2:16 PM, Albert C. [email protected] 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”

Curtis wrote in post #979429:

On Feb 3, 2011, at 2:57 AM, Albert C. [email protected] 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

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)