Skip to content

Commit

Permalink
Add more template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Rousseau committed Mar 20, 2018
1 parent 5839e0b commit 1c25e25
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ You can tell logspout to only include certain containers by setting filter param
--volume=/var/run/docker.sock:/var/run/docker.sock \
gliderlabs/logspout \
raw://192.168.10.10:5000?filter.name=*_db

$ docker run \
--volume=/var/run/docker.sock:/var/run/docker.sock \
gliderlabs/logspout \
raw://192.168.10.10:5000?filter.id=3b6ba57db54a

$ docker run \
--volume=/var/run/docker.sock:/var/run/docker.sock \
gliderlabs/logspout \
raw://192.168.10.10:5000?filter.sources=stdout%2Cstderr

# Forward logs from containers with both label 'a' starting with 'x', and label 'b' ending in 'y'.
$ docker run \
--volume=/var/run/docker.sock:/var/run/docker.sock \
Expand Down Expand Up @@ -156,6 +156,15 @@ Logspout relies on the Docker API to retrieve container logs. A failure in the A
* `SYSLOG_TAG` - datum for tag field (default `{{.ContainerName}}+route.Options["append_tag"]`)
* `SYSLOG_TIMESTAMP` - datum for timestamp field (default `{{.Timestamp}}`)

##### Built-in Template Functions

There are a few built in functions as well:

* `join $string[] $sep` - Join concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string. Alias for [`strings.Join`][go.string.Join]. `{{ join .Container.Config.Hostname "1" "-"}}`
* `replace $string $old $new $count` - Replaces all occurrences of a string within another string. Alias for [`strings.Replace`][go.string.Replace]. `{{ replace .Container.Config.Hostname "-" "_" }}`
* `split $string $sep` - Splits a string into an array using a separator string. Alias for [`strings.Split`][go.string.Split]. `{{ split .Container.Config.Hostname ":" }}`


#### Raw Format

The raw adapter has a function `toJSON` that can be used to format the message/fields to generate JSON-like output in a simple way, or full JSON output.
Expand Down Expand Up @@ -233,7 +242,7 @@ docker stack deploy --compose-file <name of your compose file>
```

More information about services and their mode of deployment can be found here:
https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/
https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/

## Modules

Expand Down
8 changes: 7 additions & 1 deletion adapters/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ var (
econnResetErrStr string
)

var funcs = template.FuncMap{
"join": strings.Join,
"replace": strings.Replace,
"split": strings.Split,
}

func init() {
hostname, _ = os.Hostname()
econnResetErrStr = fmt.Sprintf("write: %s", syscall.ECONNRESET.Error())
Expand Down Expand Up @@ -107,7 +113,7 @@ func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
default:
return nil, errors.New("unsupported syslog format: " + format)
}
tmpl, err := template.New("syslog").Parse(tmplStr)
tmpl, err := template.New("syslog").Funcs(funcs).Parse(tmplStr)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1c25e25

Please sign in to comment.