Home Help Center

Logging in Python

Netmaster0000Netmaster0000 Member Neel EdwardsOrganization: Home(Student)Project: Server based application wide security.
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

  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    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
  • Netmaster0000Netmaster0000 Member Neel EdwardsOrganization: Home(Student)Project: Server based application wide security.
    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?
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    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
  • Netmaster0000Netmaster0000 Member Neel EdwardsOrganization: Home(Student)Project: Server based application wide security.
    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.
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    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
  • Netmaster0000Netmaster0000 Member Neel EdwardsOrganization: Home(Student)Project: Server based application wide security.
    Thank you, i will definitely try to work with this.
Sign In or Register to comment.