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.

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!

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

thanks you

I like this solution (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.

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

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

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

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

end # class methods Object.method_name

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

Curtis j Schofield wrote in post #979342:

   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

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

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

Curtis j Schofield wrote in post #979342:

  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

Did you read my other response about class methods?

Curtis wrote in post #979429:

    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

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•"

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)