How to remove timestamps from celery pprint output?

2024/10/7 6:43:50

When running the celery worker then each line of the output of the pprint is always prefixed by the timestamp and also is being stripped. This makes it quite unreadable:

[2015-11-05 16:01:12,122: WARNING/Worker-2] {
[2015-11-05 16:01:12,122: WARNING/Worker-2] u'key1'
[2015-11-05 16:01:12,122: WARNING/Worker-2] :
[2015-11-05 16:01:12,122: WARNING/Worker-2] 'value1'
[2015-11-05 16:01:12,122: WARNING/Worker-2] ,
u'_id':
[2015-11-05 16:01:12,122: WARNING/Worker-2] ObjectId('55fff3b74322c53d18ae4687')
...

Is there a way how to tell celery not to format the output of pprint?

UPDATE:

the question was placed a bit wrong. The desired output would look something like this:

[2015-11-05 16:01:12,122: WARNING/Worker-2] 
{u'key1': 'value1',u'_id': ObjectId('55fff3b74322c53d18ae4687'),...
Answer

As @xbirkettx mentioned the output format is deffined in the CELERYD_LOG_FORMAT setting, which defaults to [%(asctime)s: %(levelname)s/%(processName)s] %(message)s.

So, in your settings:

CELERYD_LOG_FORMAT = '[%(levelname)s/%(processName)s] %(message)s'

There's also a special setting for a in-task logger, which defaults to:

CELERYD_TASK_LOG_FORMAT = [%(asctime)s: %(levelname)s/%(processName)s]
[%(task_name)s(%(task_id)s)] %(message)s

Remove the asctime key to get rid of the timestamp. Docs on CELERYD_TASK_LOG_FORMAT.

Update

From the docs:

You can also use print(), as anything written to standard out/-errwill be redirected to the logging system (you can disable this, seeCELERY_REDIRECT_STDOUTS).

So, rather than calling pprint.pprint, it is better to format a string with pprint.pformat and then log it.

@periodic_task(run_every=timedelta(seconds=10))
def pprint_dict2():import pprintvalues = {u'key1': 'value1',u'_id1': "ObjectId('55fff3b74322c53d18ae4687')",u'key2': 'value2',u'_id2': "ObjectId('55fff3b74322c53d18ae4687')",u'key3': 'value3',u'_id3': "ObjectId('55fff3b74322c53d18ae4687')",u'key4': 'value4',u'_id3': "ObjectId('55fff3b74322c53d18ae4687')",}s = pprint.pformat(values, width=1)print(s)  # or even better logger.info(...)

Output:

[WARNING/Beat] {u'_id1': "ObjectId('55fff3b74322c53d18ae4687')",u'_id2': "ObjectId('55fff3b74322c53d18ae4687')",u'_id3': "ObjectId('55fff3b74322c53d18ae4687')",u'key1': 'value1',u'key2': 'value2',u'key3': 'value3',u'key4': 'value4'}
https://en.xdnf.cn/q/70271.html

Related Q&A

How to get max() to return variable names instead of values in Python?

So I would like to get the maximum value from 3 variables, x,y,z.x = 1 y = 2 z = 3 max(x, y, z) # returns 3 but I want "z"However this returns the value of z i.e 3. How do I get the name of …

SymPy Imaginary Number

Im messing around with writing some SymPy code to handle symbolic expressions with imaginary numbers.To start out, I want to get it to take x and y as real numbers and find the solution where x=iy. So …

Django 1.11 404 Page while Debug=True

Without making things difficult, I just want to show a special 404 render with staticfiles.If you set DEBUG = False you can use in urls.pyhandler404 = app.views.handler404But it is without staticfiles.…

zip()-like built-in function filling unequal lengths from left with None value

Is there a built-in function that works like zip(), but fills the results so that the length of the resulting list is the length of the longest input and fills the list from the left with e.g. None?Th…

Collection comparison is reflexive, yet does not short circuit. Why?

In python, the built in collections compare elements with the explicit assumption that they are reflexive:In enforcing reflexivity of elements, the comparison of collections assumes that for a collecti…

Anisotropic diffusion 2d images [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Export environment variables at runtime with airflow

I am currently converting workflows that were implemented in bash scripts before to Airflow DAGs. In the bash scripts, I was just exporting the variables at run time withexport HADOOP_CONF_DIR="/e…

Extracting diagonal blocks from a numpy array

I am searching for a neat way to extract the diagonal blocks of size 2x2 that lie along the main diagonal of a (2N)x(2N) numpy array (that is, there will be N such blocks). This generalises numpy.diag,…

AttributeError: Unknown property density [duplicate]

This question already has an answer here:matplotlib histogram plot density argument not working(1 answer)Closed 3 years ago.I am trying to get a hold of SciPy, but I am stuck with Unknown property dens…

Find diagonals sums in numpy (faster)

I have some board numpy arrays like that:array([[0, 0, 0, 1, 0, 0, 0, 0],[1, 0, 0, 0, 0, 1, 0, 1],[0, 0, 0, 0, 0, 0, 0, 1],[0, 1, 0, 1, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 1],[0, 0, 0, 0, 1, 0, 0, 0],[0,…