Last active
October 16, 2022 10:01
-
-
Save PFython/0aa317a7c2e050cb8ae6406299764c93 to your computer and use it in GitHub Desktop.
Create a logger for every instance of a class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Create individual logs for every INSTANCE of a class | |
""" | |
from log2d import Log | |
class MyClass: | |
def __init__(self, name): | |
params = {"fmt": Log.presets["name_and_time"]} | |
self.log = Log.index.get(name) or Log(name, **params) | |
def method_1(self): | |
# Do something | |
self.log("method_1 did something!") | |
x = MyClass("Instance 1") | |
x.method_1() | |
y = MyClass("Instance 2") | |
y.method_1() | |
x.log("This message was logged directly") | |
y.log("Likewise, but different instance") | |
""" | |
OUTPUT: | |
Instance 1|2022-10-16T08:50:29+0100|method_1 did something! | |
Instance 2|2022-10-16T08:29:05+0100|method_1 did something! | |
Instance 1|2022-10-16T08:53:17+0100|This message was logged directly | |
Instance 2|2022-10-16T08:54:06+0100|Likewise, but different instance | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment