Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't filter logs by level in template (Incompatible types for comparison) #1969

Open
OddMagnet opened this issue Apr 26, 2024 · 1 comment

Comments

@OddMagnet
Copy link

OddMagnet commented Apr 26, 2024

Describe the bug

I'm trying to filter the logs in my template, since I don't need the info output.

I tried my template in the preview, there it works / I don't get errors
This is my template:

{{- with .Report -}}
  {{len .Skipped}} Skipped, {{len .Scanned}} Scanned, {{len .Updated}} Updated, {{len .Failed}} Failed, {{len .Fresh}} Fresh, {{len .Stale}} Stale
  {{- range .Skipped -}} {{- println -}}
    - {{.State}}: {{.Name}} ({{.ImageName}}): {{.Error}}
  {{- end -}}
  {{- range .Failed -}} {{- println -}}
    - {{.State}}: {{.Name}}: {{.Error}}
  {{- end}}
  {{if (or .Skipped .Failed) -}} {{- println -}} Updates available for: {{- end}} 
  {{- range .Updated -}} {{- println -}}
    - {{.Name}} ({{.ImageName}}): {{.CurrentImageID.ShortID}} → a new image {{.LatestImageID.ShortID}}
  {{- end -}}
{{end}}
{{if (and .Entries .Report) -}} {{- println -}} Logs: {{- end}}
{{range .Entries -}}
  {{- if (ne .Level "info") -}}
    {{.Time.Format `02 Jan 06 15:04 MST`}} [{{printf `%7.7s` .Level}}] {{.Message}}{{println}}
  {{- end}}
{{- end}}

This is the part that causes the bug:

  {{- if (ne .Level "info") -}}
    {{.Time.Format `02 Jan 06 15:04 MST`}} [{{printf `%7.7s` .Level}}] {{.Message}}{{println}}
  {{- end}}

The error I get is: level=fatal msg="Notification template error" error="template: :17:10: executing \"\" at <ne .Level \"InfoLevel\">: error calling ne: incompatible types for comparison"

I also tried it like this:

  {{- if (ne .Level InfoLevel) -}}
    {{.Time.Format `02 Jan 06 15:04 MST`}} [{{printf `%7.7s` .Level}}] {{.Message}}{{println}}
  {{- end}}

Which gives me the error: level=error msg="Could not use configured notification template: template: :17: function \"InfoLevel\" not defined. Using default template"

Steps to reproduce

  1. Add the above template to the environment of your watchtower compose file:
  watchtower:
    container_name: watchtower
    image: containrrr/watchtower:latest
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    environment:
      - WATCHTOWER_SCHEDULE=0 */2 * * * *   # For Debugging: check for updates every 2 minutes
      - WATCHTOWER_MONITOR_ONLY=true
      - WATCHTOWER_NOTIFICATION_URL=discord: ...
      - WATCHTOWER_NOTIFICATION_REPORT=true
      - | 
        WATCHTOWER_NOTIFICATION_TEMPLATE=
        {{- with .Report -}}
          {{len .Stale}} Stale, {{len .Skipped}} Skipped, {{len .Failed}} Failed
          {{- range .Skipped -}} {{- println -}}
            - {{.State}}: {{.Name}} ({{.ImageName}}): {{.Error}}
          {{- end -}}
          {{- range .Failed -}} {{- println -}}
            - {{.State}}: {{.Name}}: {{.Error}}
          {{- end}}
          {{if (or .Skipped .Failed) -}} {{- println -}} Updates available for: {{- end}} 
          {{- range .Stale -}} {{- println -}}
            - {{.Name}}: {{.CurrentImageID.ShortID}} has a new image {{.LatestImageID.ShortID}}
          {{- end -}}
        {{end}}
        {{if (and .Entries .Report) -}} {{- println -}} Logs: {{- end}}
        {{range .Entries -}}
          {{- if (ne .Level "info") -}}
            {{.Time.Format `02 Jan 06 15:04 MST`}} [{{printf `%7.7s` .Level}}] {{.Message}}{{println}}
          {{- end}}
        {{- end}}
  1. try to run Watchtower

Expected behavior

I'd expect either the preview on the website to give me the same error, or the Watchtower container to run with my template

Screenshots

No response

Environment

  • Linux (Unraid)
  • Architecture : x64
  • Docker Version: 24.0.9, build 2936816

Your logs

With {{- if (ne .Level "info") -}}

time="2024-04-26T20:22:47+02:00" level=fatal msg="Notification template error" error="template: :17:10: executing \"\" at <ne .Level \"INFO\">: error calling ne: incompatible types for comparison" notify=no

With {{- if (ne .Level InfoLevel) -}}:

time="2024-04-26T20:17:31+02:00" level=error msg="Could not use configured notification template: template: :17: function \"InfoLevel\" not defined. Using default template"
Copy link

Hi there! 👋🏼 As you're new to this repo, we'd like to suggest that you read our code of conduct as well as our contribution guidelines. Thanks a bunch for opening your first issue! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant