internal/log: fix setting log level (#74)

- POMERIUM_DEBUG no longer sets log level.
- LOG_LEVEL now responsible for setting global log level.
This commit is contained in:
Bobby DeSimone 2019-03-28 11:40:36 -07:00 committed by GitHub
parent a39e84cef8
commit b18a462681
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 5 deletions

View file

@ -15,7 +15,8 @@
- **WebSocket Support** : With [Go 1.12](https://golang.org/doc/go1.12#net/http/httputil) pomerium automatically proxies WebSocket requests. - **WebSocket Support** : With [Go 1.12](https://golang.org/doc/go1.12#net/http/httputil) pomerium automatically proxies WebSocket requests.
**CHANGED**: **CHANGED**:
- Add `LOG_LEVEL` config setting that allows for setting the desired minimum log level for an event to be logged.
- Changed `POMERIUM_DEBUG` config setting to just do console-pretty printing. No longer sets log level.
- Updated `generate_wildcard_cert.sh` to generate a elliptic curve 256 cert by default. - Updated `generate_wildcard_cert.sh` to generate a elliptic curve 256 cert by default.
- Updated `env.example` to include a `POLICY` setting example. - Updated `env.example` to include a `POLICY` setting example.
- Added `IDP_SERVICE_ACCOUNT` to `env.example` . - Added `IDP_SERVICE_ACCOUNT` to `env.example` .

View file

@ -33,6 +33,9 @@ func main() {
if *debugFlag || mainOpts.Debug { if *debugFlag || mainOpts.Debug {
log.SetDebugMode() log.SetDebugMode()
} }
if mainOpts.LogLevel != "" {
log.SetLevel(mainOpts.LogLevel)
}
if *versionFlag { if *versionFlag {
fmt.Printf("%s", version.FullVersion()) fmt.Printf("%s", version.FullVersion())
os.Exit(0) os.Exit(0)

View file

@ -12,10 +12,13 @@ import (
// or if a file location is not provided, the server will attempt to find a matching keypair // or if a file location is not provided, the server will attempt to find a matching keypair
// in the local directory as `./cert.pem` and `./privkey.pem` respectively. // in the local directory as `./cert.pem` and `./privkey.pem` respectively.
type Options struct { type Options struct {
// Debug enables more verbose logging, and outputs human-readable logs to Stdout. // Debug outputs human-readable logs to Stdout.
// Set with POMERIUM_DEBUG
Debug bool `envconfig:"POMERIUM_DEBUG"` Debug bool `envconfig:"POMERIUM_DEBUG"`
// LogLevel sets the global override for log level. All Loggers will use at least this value.
// Possible options are "info","warn", and "error". Defaults to "debug".
LogLevel string `envconfig:"LOG_LEVEL"`
// SharedKey is the shared secret authorization key used to mutually authenticate // SharedKey is the shared secret authorization key used to mutually authenticate
// requests between services. // requests between services.
SharedKey string `envconfig:"SHARED_SECRET"` SharedKey string `envconfig:"SHARED_SECRET"`
@ -39,6 +42,7 @@ type Options struct {
var defaultOptions = &Options{ var defaultOptions = &Options{
Debug: false, Debug: false,
LogLevel: "debug",
Services: "all", Services: "all",
} }

View file

@ -60,7 +60,7 @@ Policy contains the routes, and their access policies. For example,
- Type: `bool` - Type: `bool`
- Default: `false` - Default: `false`
By default, JSON encoded logs are produced. Debug enables colored, human-readable, and more verbose logs to be streamed to [standard out](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)). In production, it's recommended to be set to `false`. By default, JSON encoded logs are produced. Debug enables colored, human-readable logs to be streamed to [standard out](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)). In production, it's recommended to be set to `false`.
For example, if `true` For example, if `true`
@ -80,6 +80,15 @@ If `false`
{"level":"info","OverrideCertificateName":"","addr":"auth.corp.beyondperimeter.com:443","time":"2019-02-18T10:41:03-08:00","message":"proxy/authenticator: grpc connection"} {"level":"info","OverrideCertificateName":"","addr":"auth.corp.beyondperimeter.com:443","time":"2019-02-18T10:41:03-08:00","message":"proxy/authenticator: grpc connection"}
``` ```
### Log Level
- Environmental Variable: `LOG_LEVEL`
- Type: `string`
- Options: `debug` `info` `warn` `error`
- Default: `debug`
Log level sets the global logging level for pomerium. Only logs of the desired level and above will be logged.
### Certificate ### Certificate
- Environmental Variable: either `CERTIFICATE` or `CERTIFICATE_FILE` - Environmental Variable: either `CERTIFICATE` or `CERTIFICATE_FILE`

View file

@ -15,7 +15,21 @@ var Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
// SetDebugMode tells the logger to use standard out and pretty print output. // SetDebugMode tells the logger to use standard out and pretty print output.
func SetDebugMode() { func SetDebugMode() {
Logger = Logger.Output(zerolog.ConsoleWriter{Out: os.Stdout}) Logger = Logger.Output(zerolog.ConsoleWriter{Out: os.Stdout})
// zerolog.SetGlobalLevel(zerolog.InfoLevel) }
// SetLevel sets the minimum global log level. Options are 'debu' 'info' 'warn' and 'error'.
// Defaults to 'debug'
func SetLevel(level string) {
switch level {
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
} }
// With creates a child logger with the field added to its context. // With creates a child logger with the field added to its context.

View file

@ -131,3 +131,27 @@ func Example() {
// Output: {"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"} // Output: {"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}
} }
func ExampleSetLevel() {
setup()
log.SetLevel("info")
log.Debug().Msg("Debug")
log.Info().Msg("Debug or Info")
log.SetLevel("warn")
log.Debug().Msg("Debug")
log.Info().Msg("Debug or Info")
log.Warn().Msg("Debug or Info or Warn")
log.SetLevel("error")
log.Debug().Msg("Debug")
log.Info().Msg("Debug or Info")
log.Warn().Msg("Debug or Info or Warn")
log.Error().Msg("Debug or Info or Warn or Error")
log.SetLevel("default-fall-through")
log.Debug().Msg("Debug")
// Output:
// {"level":"info","time":1199811905,"message":"Debug or Info"}
// {"level":"warn","time":1199811905,"message":"Debug or Info or Warn"}
// {"level":"error","time":1199811905,"message":"Debug or Info or Warn or Error"}
// {"level":"debug","time":1199811905,"message":"Debug"}
}