Skip to content

Commit ec39bd4

Browse files
committed
Merge branch 'docs/logging_dev' into chore_improve_logs_api
2 parents e602395 + cf51e29 commit ec39bd4

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

docs/Logs.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# How to use the logging framework
2+
Al our services runs in GCP. To get the best out of the log traces, we should use a common pattern acrosss all functions and services. This documents describes how to use the shared logging functions.
3+
4+
## Python functions
5+
6+
### Logging level
7+
The logging level is by default [INFO](https://github.com/MobilityData/mobility-feed-api/blob/a857ca794b5991aa8b6e7ecedb197914ae1eca04/api/src/shared/common/logging_utils.py#L5).
8+
The logging level can be changed at runtime by setting the function variable `LOGGING_LEVEL` to the desired value, example DEBUG
9+
10+
### Initialize logging
11+
On the main file initialize the logging client by calling the following function:
12+
```
13+
init_logger()
14+
```
15+
The [init_logger](https://github.com/MobilityData/mobility-feed-api/blob/a857ca794b5991aa8b6e7ecedb197914ae1eca04/functions-python/helpers/logger.py#L49) will set the right logging level and initialize the GCP cloud client if it's not running in local environment.
16+
17+
### Logging messages
18+
There are two ways to log messages:
19+
20+
1. Logs messages without specific stable_id.
21+
22+
```
23+
import logging
24+
25+
logging.info("Total feeds: %s", total_feeds)
26+
27+
```
28+
29+
2. Using a stable id
30+
```
31+
import logging
32+
33+
logger = logging.get_logger("my logger name", "mdb-001")
34+
logging.info("Total feeds: %s", total_feeds) # This will output the following format for total_feeds equals to 10: [mdb-001] Total feeds: 10
35+
36+
```
37+
38+
Use log parameters instead of string interpolation. String interpolation always creates a string concatenation even if the log is not send due to the environment log level settings.
39+
Example:
40+
```
41+
# Avoid string interpolation.
42+
logging.info(f"Total of feeds: {total}")
43+
44+
# Use this optimized version
45+
logging.info(f"Total of feeds: %s", total)
46+
```
47+
48+

0 commit comments

Comments
 (0)