Reputation: 4484
Instead of this:
def log_error(obj):
msg = do_someprocess(obj)
logging.error(msg)
I want to add do_someprocess
as a hook into logging. When I call logging.error(obj)
, which executes do_someprocess
before logging.
Background: The object is an error object
, it contains error id and message, use logging.error(error_obj)
to record the error. do_someprocess
is for formatting error message of the object.
@mrEvgenX's comment gives me the idea that I don't have to make this as part of logging
. so I am going to add def __str__(self)
in my error class.
@Sam's answer also works as a different kind of solution, the Handler is StreamHandler
.
Upvotes: 0
Views: 35
Reputation: 814
You're going to need to create your own handler (create new class that inherits from one of python's handlers) and add it to your logger (this can be done once at initialization using AddHandler).
Please see Python's cookbook for examples.
Upvotes: 1