Skip to content

Classname in action incorrect on base class - uses classname from first concrete instance created #75

@davidhallam4-nhs

Description

@davidhallam4-nhs

When there is a class hierarchy and a log_action() is on a method in the base class, if two different instances of different subclasses of that base class are created and the base class function is invoked on each, the logger will log the subclass name of the instance that was invoked first on all invocations of the function on either instance.

Expected behaviour: the logger should log the correct classname appropriate for each subclass.

from nhs_context_logging import log_action
from nhs_context_logging.fixtures import *


class BaseClass:
    @log_action()
    def base_function(self):
        print(self.__class__, "base_function()")


class SubClassA(BaseClass):
    ...


class SubClassB(BaseClass):
    ...


def test_logging(log_capture):
    stdout, _ = log_capture

    a = SubClassA()
    a.base_function()

    b = SubClassB()
    b.base_function()

    for line in stdout:
        print("Action:", line["action"])

Results in

<class 'testtest.SubClassA'> base_function()
<class 'testtest.SubClassB'> base_function()
Action: SubClassA.base_function
Action: SubClassA.base_function

The final line should be for SubClassB, but is using action from the initial call to SubClassA.

Expected

<class 'testtest.SubClassA'> base_function()
<class 'testtest.SubClassB'> base_function()
Action: SubClassA.base_function
Action: SubClassB.base_function

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions