mrsarm / mongotail

Command line tool to log all MongoDB queries in a "tail"able way
https://pypi.org/project/mongotail/
GNU General Public License v3.0
192 stars 17 forks source link

Error: 'utf8' codec can't decode byte 0x80 (binary field) #27

Closed Signorini closed 6 years ago

Signorini commented 6 years ago

When i try to connect mongo tail, explode some erros:

PS: One field is a binary value

Traceback (most recent call last): File "/usr/local/bin/mongotail", line 11, in <module> load_entry_point('mongotail==2.2.0', 'console_scripts', 'mongotail')() File "/usr/local/lib/python2.7/site-packages/mongotail-2.2.0-py2.7.egg/mongotail/mongotail.py", line 238, in main tail(client, db, args.n, args.follow, args.verbose, args.metadata) File "/usr/local/lib/python2.7/site-packages/mongotail-2.2.0-py2.7.egg/mongotail/mongotail.py", line 83, in tail print_obj(result, verbose, metadata, server_version) File "/usr/local/lib/python2.7/site-packages/mongotail-2.2.0-py2.7.egg/mongotail/out.py", line 65, in print_obj query += ', ' + json_encoder.encode(obj['updateobj']) File "/usr/local/lib/python2.7/site-packages/mongotail-2.2.0-py2.7.egg/mongotail/jsondec.py", line 60, in encode result = super(JSONEncoder, self).encode(o) File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode return _iterencode(o, 0) UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 0: invalid start byte

mrsarm commented 6 years ago

:thinking: I'll try to reproduce the error. Do you have a query example?

mrsarm commented 6 years ago

@Signorini,

I tried to reproduce the error without success. I activated the profiler level of a database with:

$ mongotail test -l 2

Then execute mongotail in folow mode with:

$ mongotail test -f

And then in another console executed this Python script:

from pymongo import MongoClient

client = MongoClient()
client.test.test_collection.insert_one({
    'test_field': '\u0080проверка'.encode('utf-8'),
})

And the first console shows:

2018-05-24 01:17:53.006 INSERT    [test_collection] : {"test_field": BinData(0,"0L/RgNC+0LLQtdGA0LrQsA=="), "_id": ObjectId("5b0612c528d4321763ae436e")}. 1 inserted.

Anyway, I forgot to mention that specially when you have binary data, it's better to install Mongotail with Python 3.x (instead of Python 2.x). If you use mongotail with Python 2, it will show the binary data with a different enconding, like this:

2018-05-24 01:16:37.028 INSERT    [test_collection] : {"test_field": "\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430", "_id": ObjectId("5b0612c528d4321763ae436e")}. 1 inserted.

So, try to uninstall mongotail from Python 2 with:

$ sudo pip uninstall mongotail 

And then install with Py3 with:

$ pip3 install mongotail

On the other hand, what version of MongoDB are you using? which OS?

Signorini commented 6 years ago

Good morning, thanks to answer very fast.

I fork the project and try to simulate the error, in my windows with python 3 works perfect, but in macOS with python 2 explode these errors.

Using pip3 works perfectly.

thanks