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
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:
SLACK_TITLE_TEMPLATE
: notification titleSLACK_LINK_TEMPLATE
: notification title linkSLACK_MESSAGE_TEMPLATE
: notification contentSLACK_COLOR_TEMPLATE
: notification color
The evaluation context of the different templates includes the following objects:
Message
: the Logspout router message, which owns a lot of information about the container in addition to the log content itself as Datafield (please refer to the associated Go types for details)Env
: a map of environment variables you can access using the index function in your template to extract some information from your specific environment setup
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)