Jim Gallacher <[EMAIL PROTECTED]> writes:

> There is a typo in your code: s/logging.debug/logging.DEBUG/

Got me. It ran for me though...


> Beyond that it still segfaults for me. 

I can't get it to segfault.

Can you share your test?


> The other problem is that you are not removing the handler instance
> from the logging instance so you still have a memory leak. 100k
> requests would result in 100k handler instances. Oh, and there might
> be a bit of a performance issue when emit() gets fired 100k
> times. ;) 

Good point... here's another one:


  # A log handler for mod-python

  import logging
  from mod_python import apache


  class ApacheLogHandler(logging.Handler):
      """A handler class which sends all logging to Apache."""

      def __init__(self, request):
          """
          Initialize the handler (does nothing)
          """
          logging.Handler.__init__(self)
          self.request = request

          def cleanup(data = None):
              self.request = None
              logging.getLogger().removeHandler(self)

          request.register_cleanup(cleanup)

          # Set up the thing
          self.level_mapping = { }

          self.level_mapping[logging.CRITICAL] = apache.APLOG_CRIT
          self.level_mapping[logging.ERROR] = apache.APLOG_ERR
          self.level_mapping[logging.WARNING] = apache.APLOG_WARNING
          self.level_mapping[logging.INFO] = apache.APLOG_INFO
          self.level_mapping[logging.DEBUG] = apache.APLOG_DEBUG

      def emit(self, record):
          """Emit a record."""
          msg = self.format(record)
          if self.request != None:
              self.request.log_error(msg, self.level_mapping[record.levelno])

          # We *could* log to apache here:
          #else:
          #    apache.log(msg)


  # End


Nic said:

>> The traditional approach to this is to create your logger with a
>> unique name, eg:
>> 
>>    logger = logging.getLogger("webapp" + thread_id)
>
> It may be better to use the interpreter name.

Yeah... thread_id was just to intimate that it was unique for some
multi-programming concept.


>> But clearly we'd be relying on users to do that.
>
> Those darn users again!
>
>> I could come up with a logging factory to mitigate that.
>
> I'm not sure what you have in mind, but would the usage be the same as 
> for the standard logging module? One of the advantages of following the 
> standard is that people can write familiar looking code and things just 
> work they expect.

Well, most of the advantage of doing this is that you can interface
logging code to mod_python without having to change it. Just have your
handler set up the logging glue, the rest just works.

So having to use another factory method instead of the normal logging
factory method in your handler is not necessarily a big deal.

But in fact, the unique ness isn't such a big deal. There is a lock
inside the handlers so they shouldn't trample on each other. Needs
testing clearly.


Nic

Reply via email to