2

I have written a custom handler for an DL model using torch-serve and am trying to understand how to add manual log messages to the handler. I know that I can simply print any messages and it will show them within the MODEL_LOG logger at level INFO.

What if I want to add a custom message at DEBUG level or ERROR level? When I try to initialise a logger using logger = logging.getLogger('model_log') and add a message within the handler using logger.error I see the output on screen but at INFO level.

What is the best way to create log messages within a handler at a chosen log-level?

2 Answers 2

1

I am trying to do something similar. I found example logger usage in base_handler.py, where the logger is initialized on line 23 as: logger = logging.getLogger(__name__) and used in several places in the rest of the source file, typically as: logger.debug() or logger.warning(), etc. I am assuming that these messages end up in the relevant files under the logs/ folder of TorchServe. I hope it is useful for your particular use case.

Sign up to request clarification or add additional context in comments.

Comments

0

I was running into the same problem. I found the solution in the Logging Torchserve Doc. If you printed to stdout torchserve will create a INFO level logging message. If you print to stderr torchserve will create a WARN level logging message. I implemented a logger where only the error creates a WARN by torchserve and the code looks like follows:

import logging

stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(logging.ERROR)
stderr_handler.setFormatter(logging.Formatter("%(name)s - %(message)s"))
logger = logging.getLogger("HandlerName")
logger.addHandler(stderr_handler)
        

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.