Caddy: Logging configuration should support placeholders

Created on 8 Nov 2019  路  5Comments  路  Source: caddyserver/caddy

1. What would you like to have changed?

The following config.json won't work (run: loading initial config: loading new config: setting up default log: unrecognized log level: {env.LOG_LEVEL}), but it would be nice if it did:

{
  "logging": {
    "sink": {
      "writer": {}
    },
    "logs": {
      "default": {
        "level": "{env.LOG_LEVEL}",
        "encoder": {
          "format": "json"
        }
      }
    }
  },
  "apps": {
    "http": {
      "servers": {
        "server-1": {
          "listen": [":443"],
          "routes": [
            {
              "match": [
                {
                  "host": ["localhost"]
                }
              ],
              "handle": [
                {
                  "handler": "reverse_proxy",
                  "upstreams": [
                    {
                      "dial": "localhost:8080"
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

2. Why is this feature a useful, necessary, and/or important addition to this project?

Especially when running in Docker or Kubernetes, it's common to configure applications via environment variables. Having the option to have a common base configuration which works as-is, but is still customizable, is very useful. One application could set LOG_LEVEL=debug and another LOG_LEVEL=info, but both could use the same config.json.

For the same reason, it could also be useful to support placeholders in the filename option of the file output module.

There might be other fields where placeholders can be useful, but those are the most useful ones, in my opinion.

3. What alternatives are there, or what are you doing in the meantime to work around the lack of this feature?

No config.json reuse, each application has their own

4. Please link to any relevant issues, pull requests, or other discussions.

--

feature request help wanted

All 5 comments

This should be pretty easy to implement. Free game for anyone who would like to submit a PR!

Hi,

I want to implement this. I am thinking of using text/template when reading the config in loadConfig function. We can pass environment variables as a map to the template. So the above example would be written as {{ index . "LOG_LEVEL"}} instead of {env.LOG_LEVEL}

What do you think about this approach?

@iamd3vil Look into the Replacer funcs

@Mohammed90 Ohh, didn't know this existed. Seems pretty easy now. Will send a PR.

Thanks

@pascalgn Thanks to @iamd3vil's contributions, you can now use placeholders for log level and log filenames! Probably only useful for environment variable substitutions, but there ya have it. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dafanasiev picture dafanasiev  路  3Comments

muhammadmuzzammil1998 picture muhammadmuzzammil1998  路  3Comments

mikolysz picture mikolysz  路  3Comments

wayneashleyberry picture wayneashleyberry  路  3Comments

mschneider82 picture mschneider82  路  3Comments