# Need better debugging support

**URL:** https://discourse.gohugo.io/t/need-better-debugging-support/12071
**Category:** feature
**Created:** [May 21, 2018, 8:43am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071 "2018-05-21T08:43:08Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![timendres](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/timendres/32/6024_2.png) [@timendres](https://discourse.gohugo.io/u/timendres)
#### Post date: [May 21, 2018, 8:43am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/1 "2018-05-21T08:43:08Z")

</div>

Debugging my templates is impossible with the tools at my disposal.

I have an exceptionally complex template structure that is attempting to output API documentation. This requires model tables with fields which can also be models. In other words, templates are looping over fields and outputting data that can nest many levels.

Debugging with print(f) pollutes the page so terribly that I cannot use it practically. I have tied errorf, but this also outputs to the page, and it appears that it does not output data “in order” - by that, I mean the output seems to include errorf’s from calls to templates before the errorf that should come out before that template call. I believe there is some sort of buffering going on.

What would help tremendously would be a simple “debugf” call that would output to a file, and that output would be flushed at the end of every debugf statement to ensure orderly output.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totallyinformation/32/5668_2.png) [@TotallyInformation](https://discourse.gohugo.io/u/TotallyInformation)
#### Post date: [May 21, 2018, 9:51am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/2 "2018-05-21T09:51:59Z")

</div>

What I do is to have a partial that is a JavaScript script populated with console.log statements containing Hugo tags.

Use of the partial in the build is controlled by a site parameter so I can easily turn it on/off.

Here is an example:

```auto
{{/* Output to JavaScript Console only - see baseof.html */}}
<script>
    console.log('-------- Hugo Debug: Theme = Twenty-Sixteen -------')
    console.log('HUGO_ENV: ', '{{ (getenv "HUGO_ENV") | default "NOT SET" }}')
    console.log('Long Env: ', '{{ (getenv "HUGO_ENV") | (getenv "ENVIRONMENT") | default .Site.Params.env | default "DEV" }}')
    console.log('Is Prod?: ', '{{ (eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production")) }}')
    console.log('Page Kind: ', '{{.Page.Kind}}')
    console.log('Current Section: ', '{{.Page.CurrentSection | default "NOT SET" }}')
    console.log('Page Section: ', '{{.Page.Section | default "NOT SET" }}')
    console.log('Page Type: ', '{{.Page.Type}}')
    console.log('Page Dir: ', '{{.Page.Dir | default "/" }}{{ .File }}')
    console.log('Page Wd: ', '{{.Page.FuzzyWordCount}}')
    console.log('Page Home?: ', '{{.Page.IsHome}}')
    console.log('Page Node?: ', '{{.Page.IsNode}}')
    console.log('Page Page?: ', '{{.Page.IsPage}}')
    console.log('Page Date: ', '{{.Page.Date}}')
    console.log('Page Lastmod: ', '{{.Page.Lastmod}}')
    console.log('Page Date=Lastmod?: ', '{{ (eq .Page.Lastmod .Page.Date) }}')
    console.log('Page Date<>Lastmod?: ', '{{ (ne .Lastmod .Date) }}')
    console.log('Section Parent: ', '{{ with .Parent}}{{ .Dir | default "/" }}{{ .File }}{{end}}')
    console.log('Sharing Icons?: ', '{{ .Site.Params.Sharingicons | default "NOT DEFINED" }}')
    console.log('Comments?: ', '{{ .Params.comments | default "NOT DEFINED" }}')
    console.log('Disquss?: ', '{{ $.Site.DisqusShortname | default "NOT DEFINED" }}')
    console.log('Google Analytics?: ', '{{ .Site.GoogleAnalytics | default "NOT DEFINED" }}')
    console.log('Count of related pages: ', '{{if .Page.IsPage}}{{ len (.Site.RegularPages.Related .) }}{{end}}' )
    console.log(': ', '')
    console.log('---------------------------------------------------')
</script>

```

But yes, it is hard to debug Hugo. Not as hard as Jekyll though, in my - very limited - experience.

---

<div class="post-metadata">

### Author: ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)
#### Post date: [May 21, 2018, 11:38am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/3 "2018-05-21T11:38:27Z")

</div>

> [@timendres](#):
>
> This requires model tables with fields which can also be models. In other words, templates are looping over fields and outputting data that can nest many levels.

I handle debugging of such things by using a recursive partial.

I have a theme called [`hugo-bare-min-theme`](https://github.com/kaushalmodi/hugo-bare-min-theme) designed solely for debug and development of Hugo sites. You don’t have to use the whole theme; copying just the `debugprint.html` partial that I link in that project’s README (and `debugprint.css` partial) would suffice.

* * *

Related:

> [@Better understanding of Kind, Type, Layout, bundle](https://discourse.gohugo.io/t/better-understanding-of-kind-type-layout-bundle/11122):
>
> Hello all, I’ve just updated the [hugo-bare-min-theme](https://github.com/kaushalmodi/hugo-bare-min-theme). It now shows the page Kind, Type, Layout and if it’s a bundle on the top-left of each page. Demo sites: [https://hugo-sandbox.netlify.com/](https://hugo-sandbox.netlify.com/)[https://hugo-bare-min.netlify.com/](https://hugo-bare-min.netlify.com/) It’s a minor detail, but might help someone better understand those concepts.

---

<div class="post-metadata">

### Author: ![Jura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jura/32/13581_2.png) [@Jura](https://discourse.gohugo.io/u/Jura)
#### Post date: [May 22, 2018, 4:25am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/4 "2018-05-22T04:25:22Z")

</div>

Would it make sense @kaushalmodi and @TotallyInformation to include your debugging templates (combined, with the best pieces of each) as an internal template for Hugo?

In that case, people can debug a page as easily as:

```auto
{{ template "_internal/debugging.html" . }}

```

We already have [internal templates](https://gohugo.io/templates/internal/#readout) for situations that are much easier to accomplish, like adding a Google Analytics tracking script.

It makes sense to me to have the more complex situation of debugging also included ‘out of the box’.

---

<div class="post-metadata">

### Author: ![timendres](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/timendres/32/6024_2.png) [@timendres](https://discourse.gohugo.io/u/timendres)
#### Post date: [May 22, 2018, 4:57am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/5 "2018-05-22T04:57:00Z")

</div>

Thank you for these ideas. They are interesting.

However, these do not solve the problem. Logging to a file is the solution that is needed, and adding a **debugf** function cannot be terribly difficult. I have lost so many hours of my time to something that could have been debugged in minutes with proper logging.

---

<div class="post-metadata">

### Author: ![Jura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jura/32/13581_2.png) [@Jura](https://discourse.gohugo.io/u/Jura)
#### Post date: [May 22, 2018, 8:18am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/6 "2018-05-22T08:18:41Z")

</div>

I agree with better need for debugging support.

But in the mean time you can use [the `errorf` function](https://gohugo.io/functions/errorf/) to print information to the command line. If you use the `logFile` [configuration variable](https://gohugo.io/getting-started/configuration/) you can also have that output appear in a file, from what I understand.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totallyinformation/32/5668_2.png) [@TotallyInformation](https://discourse.gohugo.io/u/TotallyInformation)
#### Post date: [May 22, 2018, 9:10am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/7 "2018-05-22T09:10:02Z")

</div>

Perhaps we could all agree on an internal template. In fact, probably a couple - one that produces visible results and one that produces JavaScript console results.

Though honestly, it is probably easier simply to keep them on hand in this forum so that people can include and adapt as needed. It would be quite hard to create a template that covers everyone’s needs.

---

<div class="post-metadata">

### Author: ![timendres](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/timendres/32/6024_2.png) [@timendres](https://discourse.gohugo.io/u/timendres)
#### Post date: [May 22, 2018, 10:39am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/8 "2018-05-22T10:39:45Z")

</div>

Simply cannot agree to that at all. Console output disappears, and output in my page messes with the visible results in a way that makes it difficult to see what I am debugging.

Honestly, I have never developed with any language that did not provide me with a proper logging mechanism, or a means to create one.

This seems like an exceptionally simple function to add to Hugo, and one that is essential for development of anything other than Mickey Mouse templates.

---

<div class="post-metadata">

### Author: ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)
#### Post date: [May 22, 2018, 11:26am UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/9 "2018-05-22T11:26:32Z")

</div>

> [@Jura](#):
>
> It makes sense to me to have the more complex situation of debugging also included ‘out of the box’.

The `debugprint.html` partial detects a slice/map using a regex, so is not very robust by definition. _But it works for all my use cases._ So it wouldn’t be a good candidate for an internal template _yet_ … until [IsSlice, IsMap get implemented](https://github.com/gohugoio/hugo/issues/4081#issuecomment-364997783).

There was also a discussion on [debug/dump function](https://github.com/gohugoio/hugo/issues/3957) a while back. Folks interested should chime in their interest or support to make that happen in that issue.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totallyinformation/32/5668_2.png) [@TotallyInformation](https://discourse.gohugo.io/u/TotallyInformation)
#### Post date: [May 22, 2018, 12:44pm UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/10 "2018-05-22T12:44:22Z")

</div>

> [@timendres](#):
>
> This seems like an exceptionally simple function to add to Hugo, and one that is essential for development of anything other than Mickey Mouse templates.

I’m sure that contributions would be welcomed.

---

<div class="post-metadata">

### Author: ![Jura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jura/32/13581_2.png) [@Jura](https://discourse.gohugo.io/u/Jura)
#### Post date: [May 22, 2018, 3:06pm UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/11 "2018-05-22T15:06:26Z")

</div>

> [@TotallyInformation](#):
>
> Though honestly, it is probably easier simply to keep them on hand in this forum so that people can include and adapt as needed.

Perhaps add them to the documentation? The problem with keeping them here on the forum is that they’re so easily lost. It depends on people actively remember them. Much like the [requesting help](https://discourse.gohugo.io/t/requesting-help/9132) thread that Rick put together, but doesn’t get referenced here on the forum enough (in my view).

> [@TotallyInformation](#):
>
> I’m sure that contributions would be welcomed.

I totally understand where you’re coming from and I agree. But I also feel comments such as this don’t always help. Not everyone knows Go programming, has the time to learn Go, or has the time to learn the Hugo project and make contributions. We perhaps sometimes forget how hard that is. 🙂

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totallyinformation/32/5668_2.png) [@TotallyInformation](https://discourse.gohugo.io/u/TotallyInformation)
#### Post date: [May 24, 2018, 2:30pm UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/12 "2018-05-24T14:30:00Z")

</div>

> [@Jura](#):
>
> ![](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totallyinformation/40/5668_1.png) TotallyInformation:
> 
> > Though honestly, it is probably easier simply to keep them on hand in this forum so that people can include and adapt as needed.
> 
> Perhaps add them to the documentation? The problem with keeping them here on the forum is that they’re so easily lost. It depends on people actively remember them. Much like the [requesting help](https://discourse.gohugo.io/t/requesting-help/9132) thread that Rick put together, but doesn’t get referenced here on the forum enough (in my view).
> 
> ![](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totallyinformation/40/5668_1.png) TotallyInformation:

I don’t disagree about the issues with keeping things in a forum. However, there is a hints and tips section. The Hugo docs might be a better place perhaps but that would be up to the Hugo devs to comment on I think. I’d be happy to submit my suggestion somewhere if there is agreement on where. I fully expect that I will add more things like this to [my knowledgebase](https://it.knightnet.org.uk/kb/hugo/). I started that because of similar questions and issues of location in another open source project.

> [@Jura](#):
>
> > I’m sure that contributions would be welcomed.
> 
> I totally understand where you’re coming from and I agree. But I also feel comments such as this don’t always help. Not everyone knows Go programming, has the time to learn Go, or has the time to learn the Hugo project and make contributions. We perhaps sometimes forget how hard that is. 🙂

But they do remind people that these forums are not a one-way street. I know that the Hugo devs are busy people - like the rest of us, they probably don’t have time to deal with all requests and desires. I am not a developer but I do try to add value to projects I use whether through input of hints, tips, help, documentation updates and even bits of code if possible. I’m not promoting myself here, just saying that we can all find ways to help. The OPs response came back very aggressively and while this may well be a language issue, it is still frustrating to see such responses when you know that there are people working really hard to make good things.

---

<div class="post-metadata">

### Author: ![timendres](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/timendres/32/6024_2.png) [@timendres](https://discourse.gohugo.io/u/timendres)
#### Post date: [May 31, 2018, 5:04pm UTC](https://discourse.gohugo.io/t/need-better-debugging-support/12071/13 "2018-05-31T17:04:42Z")

</div>

> [@TotallyInformation](#):
>
> The OPs response came back very aggressively and while this may well be a language issue, it is still frustrating to see such responses when you know that there are people working really hard to make good things.

No aggression was intended, just a statement of fact. If you look specifically at the partial here:

> <https://github.com/devmycloud/MyCloudAPI-DOC/blob/master/hugo/layouts/partials/restful-object-fields.html>

which is working recursively with other partials, as well as the overall layout and data models of this project, which produces the documentation of a API, you can see that trying to debug this logic without any sort of logging ability would be difficult at best. The recursive nature of these partials, combined with Golang’s less than appealing scoping, makes this a difficult thing to code, let alone debug.

If I was not engaged 16 hours a day 7 days a week, I would be more than happy to contribute to the project. My contributions to open source over decades has been substantial. Sadly, at the moment I am burdened with delivering working systems for startup companies and have no time to contribute.
