Skip to content

Commit c6117da

Browse files
committed
use to_unicode instead of to_string when writing to sys.stderr
fixes #186 closes #187
1 parent e61079e commit c6117da

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

elasticapm/handlers/logbook.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logbook
1717

1818
from elasticapm.base import Client
19-
from elasticapm.utils.encoding import to_string
19+
from elasticapm.utils.encoding import to_unicode
2020

2121
LOOKBOOK_LEVELS = {
2222
logbook.DEBUG: 'debug',
@@ -57,15 +57,15 @@ def emit(self, record):
5757

5858
# Avoid typical config issues by overriding loggers behavior
5959
if record.channel.startswith('elasticapm.errors'):
60-
sys.stderr.write(to_string(record.message + '\n'))
60+
sys.stderr.write(to_unicode(record.message + '\n'))
6161
return
6262

6363
try:
6464
return self._emit(record)
6565
except Exception:
6666
sys.stderr.write("Top level ElasticAPM exception caught - failed creating log record.\n")
67-
sys.stderr.write(to_string(record.msg + '\n'))
68-
sys.stderr.write(to_string(traceback.format_exc() + '\n'))
67+
sys.stderr.write(to_unicode(record.msg + '\n'))
68+
sys.stderr.write(to_unicode(traceback.format_exc() + '\n'))
6969

7070
try:
7171
self.client.capture('Exception')

elasticapm/handlers/logging.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from elasticapm.base import Client
2020
from elasticapm.utils import compat
21-
from elasticapm.utils.encoding import to_string
21+
from elasticapm.utils.encoding import to_unicode
2222
from elasticapm.utils.stacks import iter_stack_frames
2323

2424

@@ -49,15 +49,15 @@ def emit(self, record):
4949

5050
# Avoid typical config issues by overriding loggers behavior
5151
if record.name.startswith('elasticapm.errors'):
52-
sys.stderr.write(to_string(record.message) + '\n')
52+
sys.stderr.write(to_unicode(record.message) + '\n')
5353
return
5454

5555
try:
5656
return self._emit(record)
5757
except Exception:
5858
sys.stderr.write("Top level ElasticAPM exception caught - failed creating log record.\n")
59-
sys.stderr.write(to_string(record.msg + '\n'))
60-
sys.stderr.write(to_string(traceback.format_exc() + '\n'))
59+
sys.stderr.write(to_unicode(record.msg + '\n'))
60+
sys.stderr.write(to_unicode(traceback.format_exc() + '\n'))
6161

6262
try:
6363
self.client.capture('Exception')

tests/handlers/logbook/logbook_tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logbook
22
import pytest
3+
from logbook import LogRecord
34

45
from elasticapm.handlers.logbook import LogbookHandler
56

@@ -118,3 +119,19 @@ def test_invalid_first_arg_type():
118119
def test_missing_client_arg():
119120
with pytest.raises(TypeError):
120121
LogbookHandler()
122+
123+
124+
def test_logbook_handler_emit_error(capsys, elasticapm_client):
125+
handler = LogbookHandler(elasticapm_client)
126+
handler._emit = lambda: 1/0
127+
handler.emit(LogRecord('x', 1, 'Oops'))
128+
out, err = capsys.readouterr()
129+
assert 'Top level ElasticAPM exception caught' in err
130+
assert 'Oops' in err
131+
132+
133+
def test_logbook_handler_dont_emit_elasticapm(capsys, elasticapm_client):
134+
handler = LogbookHandler(elasticapm_client)
135+
handler.emit(LogRecord('elasticapm.errors', 1, 'Oops'))
136+
out, err = capsys.readouterr()
137+
assert 'Oops' in err

tests/handlers/logging/logging_tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from logging import LogRecord
23

34
import pytest
45

@@ -189,3 +190,19 @@ def test_logger_setup():
189190
assert client.config.service_name == 'bar'
190191
assert client.config.secret_token == 'baz'
191192
assert handler.level == logging.NOTSET
193+
194+
195+
def test_logging_handler_emit_error(capsys, elasticapm_client):
196+
handler = LoggingHandler(elasticapm_client)
197+
handler._emit = lambda: 1/0
198+
handler.emit(LogRecord('x', 1, '/ab/c/', 10, 'Oops', [], None))
199+
out, err = capsys.readouterr()
200+
assert 'Top level ElasticAPM exception caught' in err
201+
assert 'Oops' in err
202+
203+
204+
def test_logging_handler_dont_emit_elasticapm(capsys, elasticapm_client):
205+
handler = LoggingHandler(elasticapm_client)
206+
handler.emit(LogRecord('elasticapm.errors', 1, '/ab/c/', 10, 'Oops', [], None))
207+
out, err = capsys.readouterr()
208+
assert 'Oops' in err

0 commit comments

Comments
 (0)