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

Add template function to syslog adapter to traverse .Container.Config.Env #182

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion adapters/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/syslog"
"net"
"os"
"strings"
"text/template"
"time"

Expand All @@ -29,6 +30,16 @@ func getopt(name, dfault string) string {
return value
}

func tplGetEnvVar(env []string, key string) string {
key_equals := key + "="
for _, value := range env {
if strings.HasPrefix(value, key_equals) {
return value[length(key_equals)+1:]
}
}
return ""
}

func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp"))
if !found {
Expand Down Expand Up @@ -67,7 +78,10 @@ func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
default:
return nil, errors.New("unsupported syslog format: " + format)
}
tmpl, err := template.New("syslog").Parse(tmplStr)

funcMap := template.FuncMap{
"getEnvVar": tplGetEnvVar}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would call the function getenv more in the line of other default template functions and the normal os.Getenv go function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand your comment here. What I'm trying to do is create a new template function so you can get environment variables from the slice .Container.Config.Env , as you can see in the first code bit in my first comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I understand. I just though it would be nice if the naming was more 'go-like'. Doesn't really matter much.
Cheers

tmpl, err := template.New("syslog").Funcs(funcMap).Parse(tmplStr)
if err != nil {
return nil, err
}
Expand Down