# Dash - logging the name of the callback functionin addition to POST /\_dash\_update-component

**URL:** <https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488>\
**Category:** Dash Python\
**Created:** [March 27, 2025, 1:30pm UTC](https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488 "2025-03-27T13:30:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sau001](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@sau001](https://community.plotly.com/u/sau001)\
**Post date:** [March 27, 2025, 1:30pm UTC](https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488/1 "2025-03-27T13:30:30Z")

</div>

Hello Team,

I have configured basic logging in `app.py`. It works fine. I do have some suggestions.

Is it possible to expose the name of the Dash call back function in the logs ?

```auto
2025-03-27 12:47:31 - INFO - 127.0.0.1 - - [27/Mar/2025 12:47:31] "POST /_dash-update-component HTTP/1.1" 200 -
2025-03-27 12:47:53 - INFO - 127.0.0.1 - - [27/Mar/2025 12:47:53] "POST /_dash-update-component HTTP/1.1" 200 -
2025-03-27 12:47:53 - INFO - 127.0.0.1 - - [27/Mar/2025 12:47:53] "POST /_dash-update-component HTTP/1.1" 200 -
2025-03-27 12:47:59 - INFO - 127.0.0.1 - - [27/Mar/2025 12:47:59] "POST /_dash-update-component HTTP/1.1" 200 -

```

The logs would be much more insightful, if we could see the attributes too

- Name of the call back function
- Time taken for the request

Example:

```auto
2025-03-27 12:47:59 - INFO - 127.0.0.1 - - [27/Mar/2025 12:47:59] "POST /_dash-update-component HTTP/1.1" 200 - my_callback_func - 512ms

```

Thanks,  
Sau

---

<div class="post-metadata">

**Author:** ![james783](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@james783](https://community.plotly.com/u/james783)\
**Post date:** [March 29, 2025, 11:40am UTC](https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488/2 "2025-03-29T11:40:09Z")

</div>

Hello,

Yes, you can definitely enhance your Dash application’s logging to include the callback function’s name and execution time. To achieve this, you’ll need to customize the Dash server’s request handling and integrate logging within your callback functions.

Here’s how you can modify your app.py to achieve the desired logging output:

Python

import dash  
from dash import html, Input, Output  
import logging  
import time  
from flask import request

# Configure logging

logging.basicConfig(level=logging.INFO, format=‘%(asctime)s - %(levelname)s - %(message)s’)  
logger = logging.getLogger( **name** )

app = dash.Dash( **name** )

app.layout = html.Div([  
html.Button(‘Click Me’, id=‘my-button’),  
html.Div(id=‘output-div’)  
])

@app.callback(  
Output(‘output-div’, ‘children’),  
Input(‘my-button’, ‘n\_clicks’)  
)  
def my\_callback\_func(n\_clicks):  
start\_time = time.time()  
if n\_clicks is None:  
return “Click the button”  
time.sleep(0.5) # Simulate some processing time  
end\_time = time.time()  
execution\_time\_ms = int((end\_time - start\_time) \* 1000)

```
# Log the request details
logger.info(f"{request.remote_addr} - - [{time.strftime('%d/%b/%Y %H:%M:%S')}] \"POST /_dash-update-component HTTP/1.1\" 200 - {my_callback_func. __name__ } - {execution_time_ms}ms")

return f"Button clicked {n_clicks} times. Callback executed in {execution_time_ms}ms"

```

if **name** == ‘ **main** ’:  
app.run\_server(debug=True)  
Explanation:

Import Necessary Modules:

logging for logging functionality.  
time for measuring execution time.  
flask.request to access request details.  
Configure Logging:

logging.basicConfig() sets up basic logging with a timestamp, log level, and message format.  
logger = logging.getLogger( **name** ) creates a logger instance.  
Callback Function Modification: [SunPass](https://www-sunpass.com)

start\_time = time.time() records the start time of the callback execution.  
time.sleep(0.5) simulates some processing time. Replace this with your actual callback logic.  
end\_time = time.time() records the end time.  
execution\_time\_ms calculates the execution time in milliseconds.  
Logging the Request Details:  
logger.info(…) logs the request details, including:  
request.remote\_addr (client IP address).  
Timestamp.  
Request method and URL.  
HTTP status code (200).  
my\_callback\_func. **name** (name of the callback function).  
execution\_time\_ms (execution time in milliseconds).  
Key improvements:

Callback Function Name: The my\_callback\_func. **name** attribute retrieves the name of the callback function.  
Execution Time: The execution time is calculated and included in the log message, providing valuable performance insights.  
Request Details: The log message includes the client IP address, timestamp, request method, URL, and HTTP status code, giving you a more complete picture of the request.  
Clear Log Format: The log format is customized to include all the desired information in a readable format.  
Important Notes:

Best Regards

---

<div class="post-metadata">

**Author:** ![sau001](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@sau001](https://community.plotly.com/u/sau001)\
**Post date:** [March 30, 2025, 12:14am UTC](https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488/3 "2025-03-30T00:14:27Z")

</div>

Hello James,  
Thanks for your response.

If I understand correctly, as per your response, I am expected to do the logging in every call back function. This will work fine.

However, coding the extra logging statements in each and every callback is not very productive.

I was looking for something more generic or something more centralized so that I would not have to touch each and every callback function.

Regards,  
Sau

---

<div class="post-metadata">

**Author:** ![jinnyzor](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jinnyzor/32/21194_2.png) [@jinnyzor](https://community.plotly.com/u/jinnyzor)\
**Post date:** [March 31, 2025, 2:42pm UTC](https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488/4 "2025-03-31T14:42:11Z")

</div>

Hello @sau001,

@james783 looks like it was some AI response.

I dont think there is a way to do this currently. The closest thing would be by using hooks. But I dont think you have access to the local variables at that point.

---

<div class="post-metadata">

**Author:** ![sau001](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@sau001](https://community.plotly.com/u/sau001)\
**Post date:** [March 31, 2025, 3:02pm UTC](https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488/5 "2025-03-31T15:02:43Z")

</div>

Thank you @jinnyzor

---

<div class="post-metadata">

**Author:** ![jinnyzor](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jinnyzor/32/21194_2.png) [@jinnyzor](https://community.plotly.com/u/jinnyzor)\
**Post date:** [March 31, 2025, 3:35pm UTC](https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488/6 "2025-03-31T15:35:27Z")

</div>

Here, give this a try, it uses the same mapping style as Dash.py:

```auto
@hooks.custom_data('custom_data')
def custom_context(_):
    body = flask.request.get_json()
    match = app.callback_map.get(body.get('output'))
    if match is None:
        match = GLOBAL_CALLBACK_MAP.get(body.get('output'))
    if match:
        print(match.get('callback'))

```

You’ll need to import flask for it. But it should print out the functions it is calling. You could probably fine-tune it a little to be able to target specific certain files.

---

<div class="post-metadata">

**Author:** ![sau001](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@sau001](https://community.plotly.com/u/sau001)\
**Post date:** [April 1, 2025, 10:00am UTC](https://community.plotly.com/t/dash-logging-the-name-of-the-callback-functionin-addition-to-post-dash-update-component/91488/7 "2025-04-01T10:00:34Z")

</div>

Thank you @jinnyzor

I will definitely read up on hooks. Looks promising.
