Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 49495ae

Browse files
ivarammelzchen
authored andcommitted
Add optional custom properties to logging messages. (#822)
1 parent 75f853d commit 49495ae

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

contrib/opencensus-ext-azure/README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ You can enrich the logs with trace IDs and span IDs by using the `logging integr
7373
logger.warning('In the span')
7474
logger.warning('After the span')
7575
76+
You can also add custom properties to your log messages in the form of key-values.
77+
78+
WARNING: For this feature to work, you need to pass a dictionary as the argument. If you pass arguments of any other type, the logger will ignore them. The solution is to convert these arguments into a dictionary.
79+
80+
.. code:: python
81+
82+
import logging
83+
84+
from opencensus.ext.azure.log_exporter import AzureLogHandler
85+
86+
logger = logging.getLogger(__name__)
87+
logger.addHandler(AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>'))
88+
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value2'})
89+
7690
Metrics
7791
~~~~~~~
7892

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2019, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
17+
from opencensus.ext.azure.log_exporter import AzureLogHandler
18+
19+
logger = logging.getLogger(__name__)
20+
# TODO: you need to specify the instrumentation key in a connection string
21+
# and place it in the APPLICATIONINSIGHTS_CONNECTION_STRING
22+
# environment variable.
23+
logger.addHandler(AzureLogHandler())
24+
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value2'})

contrib/opencensus-ext-azure/opencensus/ext/azure/log_exporter/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ def log_record_to_envelope(self, record):
198198
)
199199
envelope.data = Data(baseData=data, baseType='ExceptionData')
200200
else:
201+
if isinstance(record.args, dict):
202+
properties.update(record.args)
201203
envelope.name = 'Microsoft.ApplicationInsights.Message'
202204
data = Message(
203205
message=self.format(record),

contrib/opencensus-ext-azure/tests/test_azure_log_exporter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,34 @@ def test_log_record_to_envelope(self):
134134
envelope.iKey,
135135
'12345678-1234-5678-abcd-12345678abcd')
136136
handler.close()
137+
138+
@mock.patch('requests.post', return_value=mock.Mock())
139+
def test_log_record_with_custom_properties(self, requests_mock):
140+
logger = logging.getLogger(self.id())
141+
handler = log_exporter.AzureLogHandler(
142+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
143+
storage_path=os.path.join(TEST_FOLDER, self.id()),
144+
)
145+
logger.addHandler(handler)
146+
logger.warning('action', {'key-1': 'value-1', 'key-2': 'value-2'})
147+
handler.close()
148+
post_body = requests_mock.call_args_list[0][1]['data']
149+
self.assertTrue('action' in post_body)
150+
self.assertTrue('key-1' in post_body)
151+
self.assertTrue('key-2' in post_body)
152+
153+
@mock.patch('requests.post', return_value=mock.Mock())
154+
def test_log_with_invalid_custom_properties(self, requests_mock):
155+
logger = logging.getLogger(self.id())
156+
handler = log_exporter.AzureLogHandler(
157+
instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
158+
storage_path=os.path.join(TEST_FOLDER, self.id()),
159+
)
160+
logger.addHandler(handler)
161+
logger.warning('action_1_%s', None)
162+
logger.warning('action_2_%s', 'not_a_dict')
163+
handler.close()
164+
self.assertEqual(len(os.listdir(handler.storage.path)), 0)
165+
post_body = requests_mock.call_args_list[0][1]['data']
166+
self.assertTrue('action_1' in post_body)
167+
self.assertTrue('action_2' in post_body)

0 commit comments

Comments
 (0)