| 1 |
--- |
| 2 |
title: fmt.Warnf |
| 3 |
description: Logs a WARNING from a template. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [warnf] |
| 9 |
returnType: string |
| 10 |
signatures: ['fmt.Warnf FORMAT [INPUT]'] |
| 11 |
aliases: [/functions/warnf] |
| 12 |
--- |
| 13 |
|
| 14 |
{{% include "/_common/functions/fmt/format-string.md" %}} |
| 15 |
|
| 16 |
The `warnf` function evaluates the format string, then prints the result to the WARNING log. Hugo prints each unique message once to avoid flooding the log with duplicate warnings. |
| 17 |
|
| 18 |
```go-html-template |
| 19 |
{{ warnf "The %q shortcode was unable to find %s. See %s" .Name $file .Position }} |
| 20 |
``` |
| 21 |
|
| 22 |
Use the [`warnidf`][] function to allow optional suppression of specific warnings. |
| 23 |
|
| 24 |
To prevent suppression of duplicate messages when using `warnf` for debugging, make each message unique with the [`math.Counter`][] function. For example: |
| 25 |
|
| 26 |
```go-html-template |
| 27 |
{{ range site.RegularPages }} |
| 28 |
{{ .Section | warnf "%#[2]v [%[1]d]" math.Counter }} |
| 29 |
{{ end }} |
| 30 |
``` |
| 31 |
|
| 32 |
[`math.Counter`]: /functions/math/counter/ |
| 33 |
[`warnidf`]: /functions/fmt/warnidf/ |