# What am I doing wrong with message\_hook/3?

**URL:** <https://swi-prolog.discourse.group/t/what-am-i-doing-wrong-with-message-hook-3/4414>\
**Category:** Predicate\
**Created:** [September 10, 2021, 5:34pm UTC](https://swi-prolog.discourse.group/t/what-am-i-doing-wrong-with-message-hook-3/4414 "2021-09-10T17:34:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![avh](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@avh](https://swi-prolog.discourse.group/u/avh)\
**Post date:** [September 10, 2021, 5:34pm UTC](https://swi-prolog.discourse.group/t/what-am-i-doing-wrong-with-message-hook-3/4414/1 "2021-09-10T17:34:20Z")

</div>

message\_hook/3 is supposed to intercept calls to print\_message/2, thus making it possible to do something before executing print\_message. That is how I understand it.

But this did not work with me. I have defined a message\_hook for “information” type messages. But the are not intercepted, i.e. if I trace programm execution, message\_hook is never called.

Where am I going wrong? What is my misconception? Or is there a bug in my prolog (SWI-Prolog version (threaded, 64 bits, version 8.2.4)?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [September 12, 2021, 7:21am UTC](https://swi-prolog.discourse.group/t/what-am-i-doing-wrong-with-message-hook-3/4414/2 "2021-09-12T07:21:45Z")

</div>

Indeed, you cannot trace the hook because print\_message/2 disables the tracer. Keeping it enabled typically leads to incomprehensible output or non-terminating recursion. Otherwise it should work in just about any version except for the very early ones 🙂 Note that the hook **must** be defined in the **user** module. If that doesn’t help, please send some code that shows exactly what you are doing.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 12, 2021, 1:06pm UTC](https://swi-prolog.discourse.group/t/what-am-i-doing-wrong-with-message-hook-3/4414/3 "2021-09-12T13:06:41Z")

</div>

While checking if message\_hook/3 captured certain events I started the following Wiki article for those with same/similar questions.

[Using message\_hook/3](https://swi-prolog.discourse.group/t/using-message-hook-3/4429)

HTH

---

<div class="post-metadata">

**Author:** ![avh](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@avh](https://swi-prolog.discourse.group/u/avh)\
**Post date:** [September 13, 2021, 8:19am UTC](https://swi-prolog.discourse.group/t/what-am-i-doing-wrong-with-message-hook-3/4414/4 "2021-09-13T08:19:56Z")

</div>

Thanks for providing all this information. Things do work now as expected. I am happy.
