Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to customize log level in enable #150

Merged
merged 2 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased (will be 0.11.8)

- Allow to specify and endpoint to upload telemetry to.
- Add `level` argument to `logging.enable` to configure telemetry verbosity.
- Add optional queue persistence to prevent telemetry loss in case of application crash.
- Add support for using `NullSender` with `AsynchronousQueue`.

Expand Down
6 changes: 5 additions & 1 deletion applicationinsights/logging/LoggingHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ def enable(instrumentation_key, *args, **kwargs):
Args:
instrumentation_key (str). the instrumentation key to use while sending telemetry to the service.

Keyword Args:
level (Union[int, str]): The level to set for the logger. Defaults to INFO.

Returns:
:class:`ApplicationInsightsHandler`. the newly created or existing handler.
"""
if not instrumentation_key:
raise Exception('Instrumentation key was required but not provided')
if instrumentation_key in enabled_instrumentation_keys:
logging.getLogger().removeHandler(enabled_instrumentation_keys[instrumentation_key])
log_level = kwargs.pop('level', logging.INFO)
handler = LoggingHandler(instrumentation_key, *args, **kwargs)
handler.setLevel(logging.INFO)
handler.setLevel(log_level)
enabled_instrumentation_keys[instrumentation_key] = handler
logging.getLogger().addHandler(handler)
return handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def test_enable(self):
pylogging.getLogger().removeHandler(handler2)
pylogging.getLogger().removeHandler(handler3)

def test_enable_with_level(self):
handler = logging.enable('foo', level='DEBUG')
self.assertIsNotNone(handler)
self.assertEqual(handler.level, pylogging.DEBUG)
pylogging.getLogger().removeHandler(handler)

def test_enable_raises_exception_on_no_instrumentation_key(self):
self.assertRaises(Exception, logging.enable, None)

Expand Down