<p>I could not find a way to work around this yet. Validation is a side effect free function (a pure function) so it can't write to any log.</p>
<p>What I do is create a debug endpoint in my contract log info there</p>
<p>So I will have something like this</p>
<pre><code>debug :: AsContractError e =&gt; () -&gt; Contract w Schema e ()
debug _ = do
        utxos &lt;- utxosAt address
        Contract.logInfo @String $ show utxos
        Contract.logInfo @String $ show (length utxos)
        wallet1Utxos &lt;- utxosAt . walletAddress $ knownWallet 2
        Contract.logInfo @String $ show wallet1Utxos
        wallet2Utxos &lt;- utxosAt . walletAddress $ knownWallet 1
        Contract.logInfo @String $ &quot;Utxos for wallet 2: &quot; ++ show wallet2Utxos
</code></pre>
<p>And then use that in a EmulatorTrace()</p>
<pre><code>mintAndTransfer :: EmulatorTrace ()
mintAndTransfer = do
        let tn = &quot;USDC&quot;
        h1 &lt;- activateContractWallet (knownWallet 1) (endpoints @ContractError)
        callEndpoint @&quot;mint&quot; h1 $ MintParams
                { mintParamsTokenName = tn
                , mintParamsAmount = 100 }
        void $ waitNSlots 1
        callEndpoint @&quot;mint&quot; h1 $ MintParams
                { mintParamsTokenName = tn
                , mintParamsAmount = 200 }
        void $ waitNSlots 1
        callEndpoint @&quot;transferToWallet&quot; h1 $ TransferToWalletParams
                { tpTo = walletPubKeyHash $ knownWallet 2
                , tpAmount = 50
                , tpTokenName = tn }
        void $ waitNSlots 1
        callEndpoint @&quot;debug&quot; h1 ()
        void $ waitNSlots 1
</code></pre>
<p>Not ideal I know but it helps sometime.</p>

CC BY-SA 4.0
2021-11-22T15:16:02.243Z
2021-11-22T15:16:02.243Z
Calin
<p>There is a recent update regarding the error message from validator onchain here <a href="https://github.com/input-output-hk/cardano-node/releases" rel="nofollow noreferrer">https://github.com/input-output-hk/cardano-node/releases</a>
If you update your cardano-node to version 1.31.0, you will be able to see log message from traceIfFalse. Something like</p>
<pre><code>The provided Plutus code called 'error'.
Script debugging logs: UTxO not consumed
</code></pre>
<p>Like @Calin answered, you can always run any test with the Emulator Trace, which gives you where it fails onchain.</p>

CC BY-SA 4.0
2021-11-25T07:32:49.693Z
It is still just a pre-prepared string logging, not variable logging though
serx
CC BY-SA 4.0
2021-11-25T19:48:43.927Z
Yes, that's right, but at least it gets improved and now while testing on testnet/mainnet we at least know which condition doesn't meet.
Giang Nguyen
CC BY-SA 4.0
2021-11-25T05:56:02.707Z
2021-11-25T05:56:02.707Z
Giang Nguyen
<p>As a newbie in Plutus and Haskell, I write code by printing/logging variables to see their value, which is the only reason why I managed to write Contract, off-chain Plutus part.
My question is, are there any possible ways (even hacky ones, doesn't matter), to inspect variable value in on-chain, validator Plutus part.</p>
<p>I am aware of trace, traceIfFalse, etc. All of the functions can be found here:
<a href="https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Trace.html" rel="nofollow noreferrer">https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Trace.html</a>
But it doesn't help, as you can't inspect variables.</p>
<p>This enhancement seems to be dead:
<a href="https://github.com/input-output-hk/plutus/issues/3164" rel="nofollow noreferrer">https://github.com/input-output-hk/plutus/issues/3164</a>
Searched a lot of sources and found nothing useful. I am aware why it's difficult (BuiltInString, absence of Show), only asking for possible solutions/tricks/hacks and not for explanation why it's impossible.</p>
<p>This question is related:
<a href="https://cardano.stackexchange.com/questions/3949/logging-in-validator?rq=1">Logging in validator</a></p>
<p>Thanks</p>
<p>EDIT:</p>
<p>let's image I have a validator:</p>
<pre><code>mkValidator :: Order -&gt; Order -&gt; OrderAction -&gt; ScriptContext -&gt; Bool
mkValidator o oDatum act ctx = True
</code></pre>
<p>how can I print the value of for example ctx (ScriptContext), or oDatum, then executing the whole script with Emulator, so that whole script compiles and I can see ctx Value somewhere in EmulatorTrace (or any other way)?</p>
<p>I mean something like this:</p>
<pre><code>mkValidator o oDatum act ctx = trace &quot;test&quot; ctx &amp;&amp; True
</code></pre>

CC BY-SA 4.0
2021-11-20T22:28:46.863Z
Why is impossible to inspect variables with `trace`? Could you include specific examples of what you're trying to do?
Mitchell Turner
CC BY-SA 4.0
2021-11-21T10:26:24.323Z
let's image I have a validator: 
mkValidator :: Order -> Order -> OrderAction -> ScriptContext -> Bool
mkValidator o oDatum act ctx = True.

how can I print the value of for example ctx (ScriptContext), or oDatum, then executing the whole script with Emulator, so that whole script compiles and I can see ctx Value somewhere in EmulatorTrace?

I mean something like this:
mkValidator o oDatum act ctx = trace "test" ctx && True
serx
<plutus>
Is there any way to debug/log variables in validator?
# Is there any way to debug/log variables in validator?

(Asked by: username_0 on 2021-11-20)

As a newbie in Plutus and Haskell, I write code by printing/logging variables to see their value, which is the only reason why I managed to write Contract, off-chain Plutus part.
My question is, are there any possible ways (even hacky ones, doesn't matter), to inspect variable value in on-chain, validator Plutus part.
I am aware of trace, traceIfFalse, etc. All of the functions can be found here:
https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Trace.html
But it doesn't help, as you can't inspect variables.
This enhancement seems to be dead:
https://github.com/input-output-hk/plutus/issues/3164
Searched a lot of sources and found nothing useful. I am aware why it's difficult (BuiltInString, absence of Show), only asking for possible solutions/tricks/hacks and not for explanation why it's impossible.
This question is related:
Logging in validator
Thanks
EDIT:
let's image I have a validator:
mkValidator :: Order -> Order -> OrderAction -> ScriptContext -> Bool
mkValidator o oDatum act ctx = True

how can I print the value of for example ctx (ScriptContext), or oDatum, then executing the whole script with Emulator, so that whole script compiles and I can see ctx Value somewhere in EmulatorTrace (or any other way)?
I mean something like this:
mkValidator o oDatum act ctx = trace "test" ctx && True

**Tags:** plutus
> **Comments:**
> **username_1**: Why is impossible to inspect variables with `trace`? Could you include specific examples of what you're trying to do?  
> **username_0**: let's image I have a validator:  mkValidator :: Order -> Order -> OrderAction -> ScriptContext -> Bool mkValidator o oDatum act ctx = True.  how can I print the value of for example ctx (ScriptContext), or oDatum, then executing the whole script with Emulator, so that whole script compiles and I can see ctx Value somewhere in EmulatorTrace?  I mean something like this: mkValidator o oDatum act ctx = trace "test" ctx && True  

---

## 2 answers

### Answer by username_2 on 2021-11-22. Score: 2

I could not find a way to work around this yet. Validation is a side effect free function (a pure function) so it can't write to any log.
What I do is create a debug endpoint in my contract log info there
So I will have something like this
debug :: AsContractError e => () -> Contract w Schema e ()
debug _ = do
        utxos <- utxosAt address
        Contract.logInfo @String $ show utxos
        Contract.logInfo @String $ show (length utxos)
        wallet1Utxos <- utxosAt . walletAddress $ knownWallet 2
        Contract.logInfo @String $ show wallet1Utxos
        wallet2Utxos <- utxosAt . walletAddress $ knownWallet 1
        Contract.logInfo @String $ "Utxos for wallet 2: " ++ show wallet2Utxos

And then use that in a EmulatorTrace()
mintAndTransfer :: EmulatorTrace ()
mintAndTransfer = do
        let tn = "USDC"
        h1 <- activateContractWallet (knownWallet 1) (endpoints @ContractError)
        callEndpoint @"mint" h1 $ MintParams
                { mintParamsTokenName = tn
                , mintParamsAmount = 100 }
        void $ waitNSlots 1
        callEndpoint @"mint" h1 $ MintParams
                { mintParamsTokenName = tn
                , mintParamsAmount = 200 }
        void $ waitNSlots 1
        callEndpoint @"transferToWallet" h1 $ TransferToWalletParams
                { tpTo = walletPubKeyHash $ knownWallet 2
                , tpAmount = 50
                , tpTokenName = tn }
        void $ waitNSlots 1
        callEndpoint @"debug" h1 ()
        void $ waitNSlots 1

Not ideal I know but it helps sometime.


---

### Answer by username_3 on 2021-11-25. Score: 2

There is a recent update regarding the error message from validator onchain here https://github.com/input-output-hk/cardano-node/releases
If you update your cardano-node to version 1.31.0, you will be able to see log message from traceIfFalse. Something like
The provided Plutus code called 'error'.
Script debugging logs: UTxO not consumed

Like @Calin answered, you can always run any test with the Emulator Trace, which gives you where it fails onchain.

> **Comments:**
> **username_0**: It is still just a pre-prepared string logging, not variable logging though  
> **username_3**: Yes, that's right, but at least it gets improved and now while testing on testnet/mainnet we at least know which condition doesn't meet.  

---

