Python function runtime logging
Cloud Functions automatically captures a Python application's standard output streams and sends them to the centralized logging system available in Yandex Cloud. In addition to the application run history, the system logs request execution events.
Additional messages are logged using standard language constructs:
print
: Outputs a message to the standard output stream,stdout
.logging
: Outputs a message in the set format to the selected output stream.
Note
Multiline messages must be separated by \r
(carriage return) and not by \n
(line feed). When using a line feed, each line is sent as a separate message and displayed separately in the log.
System messages
When processing each function call, the system also logs START
, END
, and REPORT
system messages:
START RequestID: <request_ID> Version: <function_version_ID>
END RequestID: <request_ID>
REPORT RequestID: <request_ID>
Duration: 236.606 ms
Billed Duration: 300 ms
Memory Size: 128 MB
Max Memory Used: 22 MB
Queuing Duration: 0.027 ms
Function Init Duration: 225.298 ms
All lines contain the ID of the request (RequestID
), which is generated automatically when the function is invoked.
The REPORT
line provides a report on the function run. It contains additional information about the resources consumed:
Duration
: Time spent invoking the function. It includes theQueuing Duration
andFunction Init Duration
parameters.Billed Duration
: Time billed based on the pricing policy.Memory Size
: Amount of memory specified at version creation, MB.Max Memory Used
: Memory used when the request starts running.Queuing Duration
: Time spent by the request in the internal queue. If this time increases, it may indicate a lack of function instances. The maximum number of instances is determined by the quotas.Function Init Duration
: Time spent initializing the runtime and loading the function code.
Custom messages
Before running a Cloud Functions function, configures the following for the root logger
- A handler for logging data to the
stdout
output stream. - A formatter that adds to the message its creation timestamp, the request ID, and the logging level.
Warning
You can't change the root logger settings using the logging.basicConfig()
function.
By default, the root logger level is Warning
, all logs with a lower level are ignored. You can change the logging level using the setLevel
method for:
- The entire app.
logging.getLogger().setLevel(logging.DEBUG)
- Any logger except the root one.
logging.getLogger('requests.packages.urllib3').setLevel(logging.DEBUG) logging.getLogger('myapp').setLevel(logging.DEBUG)
You can change the log format as follows:
root_handler = logging.getLogger().handlers[0]
root_handler.setFormatter(logging.Formatter(
'[%(levelname)s]\t%(name)s\t%(request_id)s\t%(message)s\n'
))
The example shows the logger name output instead of the message creation timestamp.
Learn more about how to configure logging in the Python documentation