Skip to content

Conversation

@CookStar
Copy link
Contributor

This fixes the logger's handler remaining after a plugin reload, resulting in a duplicate message.

Test Code.

# ../test/test.py

# Source.Python
#   Commands
from commands.typed import TypedServerCommand
#   Cvars
from cvars import ConVar
#   Loggers
from loggers import LogManager

level = ConVar("test_logging_level", "0", "Test logging level.")
areas = ConVar("test_logging_areas", "9", "Test logging areas.")

test_logger = LogManager(
    "test",
    level,
    areas,
    "test",
    "%(asctime)s - %(name)s - %(message)s",
    "%Y-%m-%d %H:%M:%S"
)

@TypedServerCommand("test")
def on_test(command_info):
    test_logger.log_message("Test Log")

Input:

sp plugin reload test
test

Output(Before):

2020-04-27 11:18:35 - test - Test Log
2020-04-27 11:18:35 - test - Test Log

Output(After):

2020-04-27 11:20:53 - test - Test Log


def _unload_instance(self):
"""Remove the handler from the logger."""
self.logger.removeHandler(self._handler)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to delete the logger itself?

self.logger.manager.loggerDict.pop(self.logger.name, None)

This would ensure handlers registered through test_logger.logger.addHandler(...) directly also get removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's certainly the appropriate way to do it!

@jordanbriere jordanbriere merged commit 97b1bb2 into Source-Python-Dev-Team:master Apr 28, 2020
@CookStar CookStar deleted the logger_fix branch April 28, 2020 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants