Home

Awesome

logspout-slack

Build Status Code Climate Test Coverage License: MIT

A minimalistic adapter for logspout to send notifications to Slack using webhooks.

Follow the instructions to build your own logspout image including this module. In a nutshell, copy the contents of the custom folder and add the following import line above others in modules.go:

import (
  _ "github.com/kalisio/logspout-slack"
  ...
)

If you'd like to select a particular version create the following Dockerfile:

ARG VERSION
FROM gliderlabs/logspout:$VERSION

ONBUILD COPY ./build.sh /src/build.sh
ONBUILD COPY ./modules.go /src/modules.go

Then build your image with: docker build --no-cache --pull --force-rm --build-arg VERSION=v3.2.11 -f dockerfile -t logspout:v3.2.11 .

Run the container like this:

docker run --name="logspout" \
	--volume=/var/run/docker.sock:/var/run/docker.sock \
	your configuration options (see below)
	logspout:v3.2.11 \
	slack://hooks.slack.com

You can also deploy it in a Docker Swarm using a configuration like this:

version: '3.5'

services:
  logspout:
    image: logspout:v3.2.11
    command:
      - 'slack://hooks.slack.com?filter.sources=stdout%2Cstderr&filter.name=*aktnmap*'
    volumes:
      - /etc/hostname:/etc/host_hostname:ro
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - SLACK_WEBHOOK_URL=/services/xxx
      - SLACK_MESSAGE_FILTER=".*?"
      - BACKLOG=false
    healthcheck:
      test: ["CMD", "wget", "-q", "--tries=1", "--spider", "http://localhost:80/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 1m
    deploy:
      mode: global
      resources:
        limits:
          cpus: '0.20'
          memory: 256M
        reservations:
          cpus: '0.10'
          memory: 128M
      restart_policy:
        condition: on-failure
    networks:
      - network

networks:
  network:
    name: ${DOCKER_NETWORK}
    external: true

Configuration options

You can use the standard logspout filters to filter container names and output types:

docker run --name="logspout" \
	--volume=/var/run/docker.sock:/var/run/docker.sock \
	logspout:v3.2.11 \
	slack://hooks.slack.com?filter.sources=stdout%2Cstderr&filter.name=*my_container*'

Note: you must URL-encode parameter values such as the comma and the name filter is not a regex but rather a path pattern

You can set your webhook URL/path using the SLACK_WEBHOOK_URL environment variable:

docker run --name="logspout" -e SLACK_WEBHOOK_URL="/services/xxx" ...

You can filter the messages to be sent to Slack using a regex in the SLACK_MESSAGE_FILTER environment variable:

docker run -e SLACK_MESSAGE_FILTER=".*error" ...

Then you can customize how you format the notifications using the following environment variables containing Go template expressions:

The evaluation context of the different templates includes the following objects:

Here are some examples:

SLACK_TITLE_TEMPLATE={{ .Message.Container.Name }}
SLACK_MESSAGE_TEMPLATE={{ .Message.Data }}
SLACK_LINK_TEMPLATE=https://app.{{ index .Env "SUBDOMAIN" }}

Note: take care to enclose your template expressions between `{{ and `}} in Docker compose file because Docker Swarm processes variables as template expressions as well. (see this issue)