Archived

This forum has been archived. Please start a new discussion on GitHub.

Logging in Python

I would like to know how would i implement a logger in python? More specifically, after a response is dispatched to the client i would like to log some information to confirm that the response has just been processed and sent.

Comments

  • mes
    mes California
    Hi,

    If you want to use the Ice logger, a Python program can call
    communicator.getLogger().trace("MyApp", "My message")
    
    If you want to replace Ice's default logger implementation, define your class and pass an instance of it when initializing a communicator:
    class MyLogger(object):
        def trace(self, category, message):
            # Log the message
    
        ...
    
    id = Ice.InitializationData()
    id.logger = MyLogger()
    communicator = Ice.initialize(id)
    
    Hope that helps,
    Mark
  • Thank you for your quick reply. In addition is there a way that i can place this call in a central place so that the trace is performed on all exposed service calls?
  • mes
    mes California
    On the client side, the ice_sent callback notifies the application that Ice has successfully handed a message off to the transport layer, which is about as much of a guarantee as you can get that a request has been sent. However, no such mechanism exists for a server to be notified when a response message has been successfully sent.

    Even if you simply want to log a message after a servant has completed an operation and aren't concerned about network-level guarantees, there are still some hurdles to overcome in order to centralize this logging. You could use a servant locator as Bernard suggested in another thread; the locator's finished method would be the appropriate place to log such a message. The problem is that the locator doesn't know whether the operation completed successfully or raised an exception. You could work around this limitation, for example by wrapping the servant returned by locate in a special Python object that intercepts the dispatched operation and keeps track of whether the servant raises an exception. However, that may be more trouble than it's worth.

    Dispatch interceptors (if they were implemented in Python) would help, but only to a certain degree. A dispatch interceptor knows whether a synchronous invocation completed successfully or raised an exception, but does not know the status of an invocation dispatched asynchronously.

    Take care,
    Mark
  • That's cool, but the problem becomes how to create a servant locator in ICE? I did look into that at first, but there is no Ice.ServantLocator to even descend from.
  • mes
    mes California
    The Ice.ServantLocator type is generated from the corresponding Slice definition. You'll find it defined in python/Ice_ServantLocator_ice.py in your Ice installation. If you have a source distribution of Ice, you can also use the test case in py/test/Ice/servantLocator as an example.

    Regards,
    Mark
  • Thank you, i will definitely try to work with this.