Hey Folks, BentoML by default prints the logs in ...
# ask-for-help
a
Hey Folks, BentoML by default prints the logs in string format. What configuration can I change to print the logs in JSON format? I read up this https://docs.bentoml.com/en/v1.1.11/guides/logging.html but couldn't find anything that allows to do this.
c
Hi Arihant, we recommend two options, one is to create your own logger config for BentoML logger, and use something like https://pypi.org/project/python-json-logger/ See https://github.com/bentoml/BentoML/blob/84087808be59005e5885283e2b4bdf74ad800244/docs/source/guides/observability/logging.rst#library-logging
Another option is to use a log daemon that collect and transform the log, which is more commonly seen in production systems
a
I updated the log config as per this example: 5e5885283e2b4bdf74ad800244/docs/source/guides/observability/logging.rst#library-logging. But the bentoml default logs get prints in the string format itself as shown below:
Copy code
2024-05-28T10:11:12+0530 [INFO] [entry_service:Summarization:1] 127.0.0.1:62742 (scheme=http,method=POST,path=/api/v1/summarize,type=application/json,length=96) (status=200,type=application/json,length=322) 4991.659ms (trace=16ebf98b07b0371515f4583a4d31e832,span=77a0d497c24120a4,sampled=0,service.name=Summarization)
How can I change these logs format?
This is the config I updated to:
Copy code
ch = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter()
ch.setFormatter(formatter)

logger = logging.getLogger("bentoml")
logger.addHandler(ch)
logger.setLevel(<http://logging.INFO|logging.INFO>)
c
Oh this is due to an issue that recently got fixed here https://github.com/bentoml/BentoML/pull/4744 You may need to wait for the next release or try install from main branch. currently
bentoml serve
doesn’t allow user to override logging configs. user configs only applicable when using Service code in Python scripts.
x
Hi! So this means that we can not use logging library to make our own log messages? I am using bentoml 1.2.12 and I am trying to log messages using daslog, my own wrapper of logging library. With bentoml 0.13.1 it works ok but I have updated to 1.2.12 and any log is printed and neither stored. How could I use it?
c
you should be able to add custom logging messages if you use the “bentoml” logger, but to change the logger config, e.g. add a logging handler to write logs to file or change log format to json in the BentoML server, you’ll need to wait for the next release which includes the PR linked above
x
Thanks for the info! Do you have an estimate date when this PR is going to be released?
c
cc @Frost Ming when is the next BentoML release coming up?
f
We can make a new release today
👍 1
s
Coincidentally, I am working on enabling bentoml library logging with the following code:
Copy code
logstash_formatter = LogstashFormatter(...)
logstash_handler = AsynchronousLogstashHandler(host, port, None)
logstash_handler.setFormatter(logstash_formatter)

bentoml_logger = logging.getLogger("bentoml")
bentoml_logger.addHandler(logstash_handler)
bentoml_logger.setLevel(logging.INFO)
I am using bentoml
1.1.11
and plan to upgrade to newer version bentoml in the near future. Does that mean the above code won't be able to direct bentoml library logs to our logstash?