# Logging a Bot

**URL:** <https://forum.rasa.com/t/logging-a-bot/1935>\
**Category:** Rasa Open Source\
**Created:** [October 16, 2018, 1:01pm UTC](https://forum.rasa.com/t/logging-a-bot/1935 "2018-10-16T13:01:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![enryh](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/enryh/32/124_2.png) [@enryh](https://forum.rasa.com/u/enryh)\
**Post date:** [October 16, 2018, 1:01pm UTC](https://forum.rasa.com/t/logging-a-bot/1935/1 "2018-10-16T13:01:09Z")

</div>

Hey,

I know that using `rasa_core.run` it is possible to log events to a file. Is the same possible when you let an agent handle messages, like Slack, Facebook, etc, or is it not due to data protection?

`agent.handle_channels ( input_channel, port, serve_forever= True` has no option to specify a log.

I also do not really understand how logging is performed in debug mode using the command line, so if some understands it in more detail, I would be happy about clarifications:)

Cheers, Henry

---

<div class="post-metadata">

**Author:** ![enryh](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/enryh/32/124_2.png) [@enryh](https://forum.rasa.com/u/enryh)\
**Post date:** [October 23, 2018, 4:53pm UTC](https://forum.rasa.com/t/logging-a-bot/1935/2 "2018-10-23T16:53:24Z")

</div>

I asked yesterday on RASA Summit. I should work using a Tracker Store, which is implementend in [`rasa_core.tracker_store.py`](https://github.com/RasaHQ/rasa_core/blob/master/rasa_core/tracker_store.py).

Searching for MongoDB I found a related topic: [How to save bot conversation on mongodb?](http://forum.rasa.com/t/how-to-save-bot-conversation-on-mongodb/1018)

---

<div class="post-metadata">

**Author:** ![enryh](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/enryh/32/124_2.png) [@enryh](https://forum.rasa.com/u/enryh)\
**Post date:** [October 26, 2018, 2:59pm UTC](https://forum.rasa.com/t/logging-a-bot/1935/3 "2018-10-26T14:59:30Z")

</div>

If you have [MongoDB](https://docs.mongodb.com/manual/administration/install-community/) or [Redis](https://redis.io/download) installed as `key-value` stores, the tracker (composed of events as Bot Actions or User Inputs) is saved to the database. After using you bot, you can then extract an `Dialog` instance and get the lists of events which you then have to parse.

```python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import rasa_core
from rasa_core.agent import Agent
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.domain import Domain
from rasa_core.utils import EndpointConfig
from rasa_core.run import serve_application
from rasa_core.tracker_store import RedisTrackerStore
from rasa_core.tracker_store import MongoTrackerStore

logger = logging.getLogger( __name__ )

domain = Domain.load('domain.yml')
# db = RedisTrackerStore(domain, 
# host='localhost',
# port=6379, db=0, password=None, 
# event_broker=None,
# record_exp=None )
db = MongoTrackerStore(
                                domain,
				host='mongodb://localhost:27017', 
				db='rasa', username=None, 
				password=None, collection='conversations', 
				event_broker=None)

def train_dialogue(domain = 'domain.yml',
				   model_path = './models/dialogue',
				   training_data_file = './data/stories.md'):
	"""Train a model"""
	nlu_interpreter = RasaNLUInterpreter('./models/nlu/default/')
	action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")				
	agent = Agent( domain = domain, 
                                policies = [MemoizationPolicy(), KerasPolicy()],
				interpreter=nlu_interpreter, 
                                generator=None, 
                                tracker_store=db, 
				action_endpoint=action_endpoint,
                                fingerprint=None)
	data = agent.load_data(training_data_file)	
	agent.train(	data,
				epochs = 100,
				batch_size = 25,
				validation_split = 0.2
                          )		
	agent.persist(model_path)
	return agent
	
if __name__ == ' __main__':
	agent = train_dialogue()
	rasa_core.run.serve_application(agent , channel='cmdline')		

	#extract list of events and print its names 
	events = db.retrieve("default").as_dialogue().events
	for event in events:
		print(event.as_story_string())

```

This then yields something like

```auto
action_listen
inform{"location": "home"}
slot{"location": "home"}
action_weather
None
action_listen
deny
utter_goodbye
None
action_listen
restart

```

where _None_ is generated from Utter (BotUttered) Actions. This list could be parsed into a story by some rules.

Unfortunately, and I would appreciate hints on that, this approach saves all conversations to one _\_id_ in MongoDB - and that even persistently, so rerunning the skript after closing the console keeps alls previous stories. Any idea?

---

<div class="post-metadata">

**Author:** ![souvikg10](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/souvikg10/32/93_2.png) [@souvikg10](https://forum.rasa.com/u/souvikg10)\
**Post date:** [October 26, 2018, 3:04pm UTC](https://forum.rasa.com/t/logging-a-bot/1935/4 "2018-10-26T15:04:59Z")

</div>

you can create two collections, one which you remove once the conversation is over and copy the tracker to another one for persisted storage.

in redis, you do that with record\_exp flag.

Though i am not how that would affect your conversation unless they are really small. however you should also reset all slots at the end of the conversation or at the end where you have performed the final action

---

<div class="post-metadata">

**Author:** ![enryh](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/enryh/32/124_2.png) [@enryh](https://forum.rasa.com/u/enryh)\
**Post date:** [October 26, 2018, 3:08pm UTC](https://forum.rasa.com/t/logging-a-bot/1935/5 "2018-10-26T15:08:57Z")

</div>

Okay thanks, I have a look at redis.

- `action_restart` does set all slots to _none_?! (I have it included and will add it to my extract!)

---

<div class="post-metadata">

**Author:** ![dheeman6588](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@dheeman6588](https://forum.rasa.com/u/dheeman6588)\
**Post date:** [October 29, 2018, 6:39am UTC](https://forum.rasa.com/t/logging-a-bot/1935/6 "2018-10-29T06:39:04Z")

</div>

Hi souvik, I wanted to know if all the conversations from different users will also be stored in the mongo tracker store?

---

<div class="post-metadata">

**Author:** ![souvikg10](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/souvikg10/32/93_2.png) [@souvikg10](https://forum.rasa.com/u/souvikg10)\
**Post date:** [November 1, 2018, 10:32pm UTC](https://forum.rasa.com/t/logging-a-bot/1935/7 "2018-11-01T22:32:32Z")

</div>

Hi, Sorry i don’t receive any notification unless i am tagged. 😃

anyway, yes all conversations from different users(different sender-id) will be stored in mongo tracker store as a tracker object [https://rasa.com/docs/core/server/](https://rasa.com/docs/core/server/)

Check the endpoint `GET ` `/conversations/` ( _str:__sender\_id_ ) `/tracker`

---

<div class="post-metadata">

**Author:** ![azizullah2017](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/azizullah2017/32/241_2.png) [@azizullah2017](https://forum.rasa.com/u/azizullah2017)\
**Post date:** [January 10, 2019, 9:23am UTC](https://forum.rasa.com/t/logging-a-bot/1935/8 "2019-01-10T09:23:54Z")

</div>

```auto
rasa_core.run.serve_application(agent , channel='cmdline')

```

how change the channel from **‘cmdline’** to " **rest"**
