|
22 | 22 | from google.cloud.logging_v2.entries import StructEntry |
23 | 23 | from google.cloud.logging_v2.entries import TextEntry |
24 | 24 | from google.cloud.logging_v2.resource import Resource |
| 25 | +from google.cloud.logging_v2.handlers._monitored_resources import detect_resource |
25 | 26 |
|
26 | 27 | import google.protobuf.message |
27 | 28 |
|
@@ -51,19 +52,23 @@ class Logger(object): |
51 | 52 | See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.logs |
52 | 53 | """ |
53 | 54 |
|
54 | | - def __init__(self, name, client, *, labels=None, resource=_GLOBAL_RESOURCE): |
| 55 | + def __init__(self, name, client, *, labels=None, resource=None): |
55 | 56 | """ |
56 | 57 | Args: |
57 | 58 | name (str): The name of the logger. |
58 | 59 | client (~logging_v2.client.Client): |
59 | 60 | A client which holds credentials and project configuration |
60 | 61 | for the logger (which requires a project). |
61 | | - resource (~logging_v2.Resource): a monitored resource object |
62 | | - representing the resource the code was run on. |
| 62 | + resource (Optional[~logging_v2.Resource]): a monitored resource object |
| 63 | + representing the resource the code was run on. If not given, will |
| 64 | + be inferred from the environment. |
63 | 65 | labels (Optional[dict]): Mapping of default labels for entries written |
64 | 66 | via this logger. |
65 | 67 |
|
66 | 68 | """ |
| 69 | + if not resource: |
| 70 | + # infer the correct monitored resource from the local environment |
| 71 | + resource = detect_resource(client.project) |
67 | 72 | self.name = name |
68 | 73 | self._client = client |
69 | 74 | self.labels = labels |
|
0 commit comments