You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UnicodeEncodeErrors with celery built-in console logging #427
Colored celery console logger fails to display unicode strings containing non-ascii letters (e.g. django's sql query log with non-ascii data):
UnicodeEncodeError: 'ascii' codec can't encode characters in position 91-95: ordinal not in range(128)
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py", line 776, in emit
msg = self.format(record)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py", line 654, in format
return fmt.format(record)
File "/Users/kmike/envs/zina/lib/python2.6/site-packages/celery/log.py", line 56, in format
t = logging.Formatter.format(self, record)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py", line 439, in format
s = self._fmt % record.__dict__
File "/Users/kmike/envs/zina/lib/python2.6/site-packages/celery/utils/term.py", line 62, in __str__
return prefix + str(reduce(self._add, self.s)) + suffix
As a workaround I set CELERYD_HIJACK_ROOT_LOGGER = False and then add 'logging.StreamHandler' to 'celery' logger (it handles non-ascii data).
Please excuse me for not being clear: celery.log.ColorFormatter and celery.utils.term.colored wasn't able to handle unicode data, logging.FileHandler works fine. Only console output causes troubles and switching to a less fancy default StreamHandler helps in my case.
I think, it is still present. It is reproducible if you have for example a Django TextField that stores JSON string with unicode. A part of the SQL query from Postgres log:
UPDATE ...
SET
...,
"invoice_name" = 'Faktúra č. 11300007',
"app_data" = '{"address": "Lipovn\u00edk 123"}'
WHERE ...
And the same in celery log:
[2013-02-25 11:47:35,143: DEBUG/MainProcess] <Unrepresentable <type 'str'>: UnicodeDecodeError('ascii', '\x1b[1;34m(0.001) UPDATE ... SET ..., "invoice_name" = \'Fakt\xc3\xbara \xc4\x8d. 11300007\', "app_data" = \'{"address": "Lipovn\\u00edk 123"}\' WHERE ... ; args=(..., u\'Fakt\\xfara \\u010d. 11300007\', \'{"address": "Lipovn\\\\u00edk 123"}\', ...)>
Traceback (most recent call last):
File "/home/bruce/.virtualenv/expreso/local/lib/python2.7/site-packages/celery/utils/log.py", line 90, in format
record.msg = safe_str(str_t(color(record.msg)))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 219: ordinal not in range(128)
Hi, on celery 3.0.16 i have the same problem.
When trying to log a unicode string i get:
<Unrepresentable<type'str'>: UnicodeDecodeError('ascii', "\x1b[1;33moption2:<type 'unicode'>Super krasser Full Name mit \xc3\xa4\xc3\xa4\xc3\xbc\xc3\x9f\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e\x1b[0m", 59, 60, 'ordinal not in range(128)')>Traceback (mostrecentcalllast):
File"/home/buster/projects/nomadenv/local/lib/python2.7/site-packages/celery/utils/log.py", line90, informatrecord.msg=safe_str(str_t(color(record.msg)))
UnicodeDecodeError: 'ascii'codeccan'tdecodebyte0xc3inposition59: ordinalnotinrange(128)
[2013-10-20 20:24:19,845: DEBUG/MainProcess] : UnicodeDecodeError('ascii', "\x1b[1;34m(0.001) SELECT * FROM $TABLE_NAME WHERE $TABLE_NAME.`title` = '\xe8\xb1\xa1\xe5\x8e\x82\xe5\x96\x9c\xe5\x89\xa7' ; args=(u'\\u8c61\\u5382\\u559c\\u5267',)\x1b[0m", 722, 723, 'ordinal not in range(128)')>
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/utils/log.py", line 96, in format
record.msg = str_t(color(safe_str(msg)))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 722: ordinal not in range(128)
Well, this is a mess, but I'm not sure how this can be solved in a pretty way. I used to dream of succinct and beautiful code, then reality kicked in with platform workarounds, posix and users with their special cases, so this shouldn't be news to me :)
You cannot really know the encoding until you actually write to a specific file, so now it will maintain a "default output file" that the workers set when it configures logging. If it logs to a stream it will take encoding information from that stream, if it writes to a file it takes encoding information from that file.
It seems that logging can handle unicode already though, so maybe this is not necessary anymore. If that is the case we must find out what version that was added in.
In my case, I fix it by set export LANG=en_US.UTF-8, I already set it in .zshrc, but need to set it again, because the working environment does not respect settings in .zshrc.
@ask
Yeah - I had another issue a while back with unicode and nginx - thats all working now.
This issue just relates to celery.
Here is a stacktrace as requested.
Traceback (most recent call last):
File "/home/webapps/.virtualenvs/crowdstaff/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/webapps/.virtualenvs/crowdstaff/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/webapps/apps/crowdstaff/lib/smsverify/views.py", line 57, in verify_form
return profile_complete_redir(request)
File "/home/webapps/apps/crowdstaff/project/users/decorators.py", line 26, in func
return view_func(request, *args, **kwargs)
File "/home/webapps/apps/crowdstaff/project/workers/views/profile.py", line 445, in profile_complete_redir
if request.user.signup_complete and profile_complete_and_user_verified(request):
File "/home/webapps/apps/crowdstaff/project/users/decorators.py", line 26, in func
return view_func(request, *args, **kwargs)
File "/home/webapps/apps/crowdstaff/project/workers/views/profile.py", line 499, in profile_complete_and_user_verified
return create_matches_for_signup_applications(request)
File "/home/webapps/apps/crowdstaff/project/users/decorators.py", line 26, in func
return view_func(request, *args, **kwargs)
File "/home/webapps/apps/crowdstaff/project/workers/views/profile.py", line 523, in create_matches_for_signup_applications
score = score_worker_for_job(worker, job, external_applicant=True)
File "/home/webapps/apps/crowdstaff/project/jobs/process.py", line 490, in score_worker_for_job
print '%s - Assessing %s-%s' % (worker, job.id, job.name)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
Any assistance would be greatly appreciated.
Thanks
@ask so it is... wrong stacktrace. Sorry about that
I have uploaded the stacktrace as an image as Opbeat won't let me copy and paste the stacktrace as one might expect.
Opbeat also gives me these extra details:
args []
task <@task: jobs.process.process_jobs of default:0x7f2dd278d0d0 (v2 compatible)>
task_id 4b84ff8f-3ecc-4708-92b8-fa67e4a62390
kwargs {}
this is the same function as the previous incorrect stacktrace but this time is called as an @task
The worker redirects output to stdout to the logger, but having unicode in there works for me.
In that case the traceback would also be different (occurring somewhere in celery.log)
Your problem is in your code, and could beseveral things, job.id or job.name could be objects that are stringified with str instead of unicode on Python2, or you have disabled the redirection.
@ask You were right - good thinking.
I had a unicode method on the Worker model returning a string, rather than a unicode object.
Fixed. Thanks for your help
Activity
ask commentedon Jul 18, 2011
Are you saying
logging.FileHandlerdoesn't handle unicode data? Wonder how we could fix that, and what makes it work withStreamHandlerkmike commentedon Jul 18, 2011
Hi ask,
Please excuse me for not being clear: celery.log.ColorFormatter and celery.utils.term.colored wasn't able to handle unicode data, logging.FileHandler works fine. Only console output causes troubles and switching to a less fancy default StreamHandler helps in my case.
ask commentedon Sep 2, 2011
Does this patch help?
tkloc commentedon Dec 27, 2011
No, it doesn't.
while my standard logger saves&displays message correctly, i'm still getting these exceptions in celery's console
victorbstan commentedon Jan 12, 2012
Getting same error <Unrepresentable <type 'unicode'>: UnicodeDecodeError('ascii', '\x1b[1;34m(0.000)
ask commentedon Jan 13, 2012
Could you give me the source code for a task that is able to reproduce this issue?
victorbstan commentedon Jan 13, 2012
I guess any task that outputs to the console any odd unicode characters? Say, if they are in the name of the object that is passed as a parameter?
brockhaywood commentedon Feb 12, 2013
Has this issue been addressed? It's closed but seems to not be resolved in celery==3.0.11
ask commentedon Feb 13, 2013
Having print(unicode) in a task works for me on 3.0.15
mtirsel commentedon Feb 25, 2013
I think, it is still present. It is reproducible if you have for example a Django TextField that stores JSON string with unicode. A part of the SQL query from Postgres log:
And the same in celery log:
You can see double escapes on the JSON string.
buster commentedon Mar 13, 2013
Hi, on celery 3.0.16 i have the same problem.
When trying to log a unicode string i get:
ask commentedon Mar 13, 2013
@buster Can you please show me an example task that reproduces this?
buster commentedon Mar 14, 2013
Hi,
the simplest test i have found to work:
And in a celeryshell:
And in the worker output:
I did find that this works when i disable colored log output...
13 remaining items
JoshuaChi commentedon Oct 19, 2013
ask commentedon Oct 19, 2013
Btw, people, when it happens are you logging to a console or to a file?
@JoshuaChi, that is strange, what is the traceback you have?
JoshuaChi commentedon Oct 20, 2013
Here is piece of traceback:
[2013-10-20 20:24:19,845: DEBUG/MainProcess] : UnicodeDecodeError('ascii', "\x1b[1;34m(0.001) SELECT * FROM $TABLE_NAME WHERE $TABLE_NAME.`title` = '\xe8\xb1\xa1\xe5\x8e\x82\xe5\x96\x9c\xe5\x89\xa7' ; args=(u'\\u8c61\\u5382\\u559c\\u5267',)\x1b[0m", 722, 723, 'ordinal not in range(128)')> Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/utils/log.py", line 96, in format record.msg = str_t(color(safe_str(msg))) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 722: ordinal not in range(128)Workaround for Unicode errors (Issue #427)
ask commentedon Oct 23, 2013
Well, this is a mess, but I'm not sure how this can be solved in a pretty way. I used to dream of succinct and beautiful code, then reality kicked in with platform workarounds, posix and users with their special cases, so this shouldn't be news to me :)
You cannot really know the encoding until you actually write to a specific file, so now it will maintain a "default output file" that the workers set when it configures logging. If it logs to a stream it will take encoding information from that stream, if it writes to a file it takes encoding information from that file.
It seems that logging can handle unicode already though, so maybe this is not necessary anymore. If that is the case we must find out what version that was added in.
JoshuaChi commentedon Oct 29, 2013
At least it works for me. :-)
flyingfoxlee commentedon Jul 7, 2014
In my case, I fix it by set
export LANG=en_US.UTF-8, I already set it in.zshrc, but need to set it again, because the working environment does not respect settings in.zshrc.lukeaus commentedon Oct 23, 2015
I am getting a UnicodeDecodeError from Celery
I have this set in my /etc/default/celeryd
export LANG=en_US.UTF-8
I have celery==3.1.18 and django-celery==3.1.17 installed.
However celery throwing a UnicodeDecodeError on this print statement
print '%s - Assessing %s-%s' % (user_name, foo.id, foo.name)
where user_name = foobar don\xe7a
As a side note, nginx handles unicode fine
Do I need to set:
export LANG=en_US.UTF-8
anywhere else?
ask commentedon Oct 27, 2015
@lukeaus do you have a traceback?
nginx is not written in Python 2 ;)
lukeaus commentedon Oct 27, 2015
@ask
Yeah - I had another issue a while back with unicode and nginx - thats all working now.
This issue just relates to celery.
Here is a stacktrace as requested.
Traceback (most recent call last): File "/home/webapps/.virtualenvs/crowdstaff/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/webapps/.virtualenvs/crowdstaff/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view return view_func(request, *args, **kwargs) File "/home/webapps/apps/crowdstaff/lib/smsverify/views.py", line 57, in verify_form return profile_complete_redir(request) File "/home/webapps/apps/crowdstaff/project/users/decorators.py", line 26, in func return view_func(request, *args, **kwargs) File "/home/webapps/apps/crowdstaff/project/workers/views/profile.py", line 445, in profile_complete_redir if request.user.signup_complete and profile_complete_and_user_verified(request): File "/home/webapps/apps/crowdstaff/project/users/decorators.py", line 26, in func return view_func(request, *args, **kwargs) File "/home/webapps/apps/crowdstaff/project/workers/views/profile.py", line 499, in profile_complete_and_user_verified return create_matches_for_signup_applications(request) File "/home/webapps/apps/crowdstaff/project/users/decorators.py", line 26, in func return view_func(request, *args, **kwargs) File "/home/webapps/apps/crowdstaff/project/workers/views/profile.py", line 523, in create_matches_for_signup_applications score = score_worker_for_job(worker, job, external_applicant=True) File "/home/webapps/apps/crowdstaff/project/jobs/process.py", line 490, in score_worker_for_job print '%s - Assessing %s-%s' % (worker, job.id, job.name) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)Any assistance would be greatly appreciated.
Thanks
ask commentedon Oct 28, 2015
But this traceback seems to be from a Django view? What are you running exactly when this error occurs?
lukeaus commentedon Oct 28, 2015
@ask so it is... wrong stacktrace. Sorry about that
I have uploaded the stacktrace as an image as Opbeat won't let me copy and paste the stacktrace as one might expect.
Opbeat also gives me these extra details:
args []
task <@task: jobs.process.process_jobs of default:0x7f2dd278d0d0 (v2 compatible)>
task_id 4b84ff8f-3ecc-4708-92b8-fa67e4a62390
kwargs {}
this is the same function as the previous incorrect stacktrace but this time is called as an @task
ask commentedon Oct 30, 2015
The worker redirects output to stdout to the logger, but having unicode in there works for me.
In that case the traceback would also be different (occurring somewhere in celery.log)
Your problem is in your code, and could beseveral things, job.id or job.name could be objects that are stringified with str instead of unicode on Python2, or you have disabled the redirection.
May be fixed by using
safe_str:Or you will get a better traceback
lukeaus commentedon Nov 10, 2015
@ask You were right - good thinking.
I had a unicode method on the Worker model returning a string, rather than a unicode object.
Fixed. Thanks for your help