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.
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
# you want to put it in as a class method
class << self
def error(msg)
super("ERROR------------>"+msg)
end
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)
#also - this may give you an error
# i remember this as
msg = %%ERROR#{'-'*5}>#{msg}%
super
> #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
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)
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•"