# Log with timestamp

**URL:** https://discourse.mitmproxy.org/t/log-with-timestamp/767
**Category:** help
**Created:** [December 18, 2017, 4:35pm UTC](https://discourse.mitmproxy.org/t/log-with-timestamp/767 "2017-12-18T16:35:39Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Gigiarum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/gigiarum/32/278_2.png) [@Gigiarum](https://discourse.mitmproxy.org/u/Gigiarum)
#### Post date: [December 18, 2017, 4:35pm UTC](https://discourse.mitmproxy.org/t/log-with-timestamp/767/1 "2017-12-18T16:35:40Z")

</div>

Hello,  
I want to write all log to standard error and standard output with a timestamp prefix: where I have to modify?

Thanks in advance.

---

<div class="post-metadata">

### Author: ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)
#### Post date: [December 18, 2017, 10:29pm UTC](https://discourse.mitmproxy.org/t/log-with-timestamp/767/2 "2017-12-18T22:29:45Z")

</div>

Hi,

You can either write our own script, pipe mitmdump’s output through some unix tool that adds timestamps, or modify [https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/addons/dumper.py](https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/addons/dumper.py). 🙂

---

<div class="post-metadata">

### Author: ![Gigiarum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/gigiarum/32/278_2.png) [@Gigiarum](https://discourse.mitmproxy.org/u/Gigiarum)
#### Post date: [January 10, 2018, 3:29pm UTC](https://discourse.mitmproxy.org/t/log-with-timestamp/767/3 "2018-01-10T15:29:40Z")

</div>

Thank you for your response.  
I try to modify the class you linked insert the command  
**`text = datetime.today().isoformat() + ' ' + text;`**  
before the line  
`click.secho(text, file=self.outfp, **style)`  
in the method `echo`  
but with mitmdump I don’t see any difference in the standard output.  
What can I do?

---

<div class="post-metadata">

### Author: ![Gigiarum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/gigiarum/32/278_2.png) [@Gigiarum](https://discourse.mitmproxy.org/u/Gigiarum)
#### Post date: [January 25, 2018, 12:34pm UTC](https://discourse.mitmproxy.org/t/log-with-timestamp/767/4 "2018-01-25T12:34:50Z")

</div>

I came back to this point and I see this log:

> Proxy server listening at [http://0.0.0.0:8888](http://0.0.0.0:8888)  
> 192.168.3.128:50011: clientconnect  
> 2018-01-25T12:21:13 192.168.3.128:50011: GET [http://www.google.it/](http://www.google.it/)  
> 2018-01-25T12:21:13 \<\< 302 Found 230b  
> 192.168.3.128:50012: clientconnect  
> 192.168.3.128:50013: clientconnect  
> 192.168.3.128:50014: clientconnect  
> 192.168.3.128:50015: clientconnect  
> 2018-01-25T12:21:13 192.168.3.128:50014: GET [Google](https://www.google.it/?gws_rd=ssl)  
> 2018-01-25T12:21:13 \<\< 200 OK 64.09k

How can I put a timestamp before every line? Can I put also the log level (ERROR, WARN, INFO, …)?

Thanks

---

<div class="post-metadata">

### Author: ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)
#### Post date: [January 25, 2018, 1:06pm UTC](https://discourse.mitmproxy.org/t/log-with-timestamp/767/5 "2018-01-25T13:06:30Z")

</div>

Oh, it looks like you also need to adjust [https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/addons/termlog.py](https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/addons/termlog.py). 🙂

---

<div class="post-metadata">

### Author: ![Gigiarum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/gigiarum/32/278_2.png) [@Gigiarum](https://discourse.mitmproxy.org/u/Gigiarum)
#### Post date: [January 25, 2018, 1:37pm UTC](https://discourse.mitmproxy.org/t/log-with-timestamp/767/6 "2018-01-25T13:37:26Z")

</div>

Perfect!

I added

```
        #add timestamp and log level as message prefix
        ts = datetime.utcnow().isoformat("T", "seconds")
        level = "{:5s}".format(e.level.upper())
        e.msg = ts + " " + level + " " + e.msg

```

before the click.secho calling (from datetime import datetime).

Here the result:

> 2018-01-25T13:30:07 INFO Proxy server listening at [http://0.0.0.0:8888](http://0.0.0.0:8888)  
> 2018-01-25T13:30:13 INFO 192.168.3.128:53349: clientconnect  
> 2018-01-25T13:30:13 192.168.3.128:53349: GET [https://www.google.it](https://www.google.it)  
> 2018-01-25T13:30:13 \<\< 200 OK 8.94k

---

<div class="post-metadata">

### Author: ![bwelsh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/bwelsh/32/370_2.png) [@bwelsh](https://discourse.mitmproxy.org/u/bwelsh)
#### Post date: [November 4, 2018, 9:34pm UTC](https://discourse.mitmproxy.org/t/log-with-timestamp/767/7 "2018-11-04T21:34:41Z")

</div>

Thanks for this tip. I modified

- /home/pi/.local/lib/python3.5/site-packages/mitmproxy/addons/termlog.py  
on my raspbian linux

How do I regenerate the pyc file?

- /home/pi/.local/lib/python3.5/site-packages/mitmproxy/addons/ **pycache** /termlog.cpython-35.pyc
