Skip to content
Snippets Groups Projects
Commit 25e62278 authored by Dan Lorenc's avatar Dan Lorenc
Browse files

Add a production logging configuration and a flag.

This is only toggled on for the server right now.
parent 66d0c1a5
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ import ( ...@@ -28,6 +28,7 @@ import (
) )
var cfgFile string var cfgFile string
var logType string
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
...@@ -57,6 +58,7 @@ func init() { ...@@ -57,6 +58,7 @@ func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.rekor-server.yaml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.rekor-server.yaml)")
rootCmd.PersistentFlags().StringVar(&logType, "log_type", "dev", "logger type to use (dev/prod)")
rootCmd.PersistentFlags().String("trillian_log_server.address", "127.0.0.1", "Trillian log server address") rootCmd.PersistentFlags().String("trillian_log_server.address", "127.0.0.1", "Trillian log server address")
rootCmd.PersistentFlags().Uint16("trillian_log_server.port", 8091, "Trillian log server port") rootCmd.PersistentFlags().Uint16("trillian_log_server.port", 8091, "Trillian log server port")
......
...@@ -38,6 +38,9 @@ var serveCmd = &cobra.Command{ ...@@ -38,6 +38,9 @@ var serveCmd = &cobra.Command{
Long: `Starts a http server and serves the configured api`, Long: `Starts a http server and serves the configured api`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Setup the logger to dev/prod
log.ConfigureLogger(viper.GetString("log_type"))
// workaround for https://github.com/projectrekor/rekor/issues/68 // workaround for https://github.com/projectrekor/rekor/issues/68
// from https://github.com/golang/glog/commit/fca8c8854093a154ff1eb580aae10276ad6b1b5f // from https://github.com/golang/glog/commit/fca8c8854093a154ff1eb580aae10276ad6b1b5f
_ = flag.CommandLine.Parse([]string{}) _ = flag.CommandLine.Parse([]string{})
......
...@@ -25,7 +25,8 @@ spec: ...@@ -25,7 +25,8 @@ spec:
"--trillian_log_server.port=8091", "--trillian_log_server.port=8091",
"--rekor_server.address=0.0.0.0", "--rekor_server.address=0.0.0.0",
"--redis_server.address=10.234.175.59", "--redis_server.address=10.234.175.59",
"--redis_server.port=6379" "--redis_server.port=6379",
"--log_type=prod",
] ]
resources: resources:
requests: requests:
......
...@@ -61,6 +61,8 @@ services: ...@@ -61,6 +61,8 @@ services:
"--redis_server.address=redis-server", "--redis_server.address=redis-server",
"--redis_server.port=6379", "--redis_server.port=6379",
"--rekor_server.address=0.0.0.0", "--rekor_server.address=0.0.0.0",
# Uncomment this for production logging
# "--log_type=prod",
] ]
restart: always # keep the server running restart: always # keep the server running
ports: ports:
......
...@@ -119,9 +119,19 @@ func setupMiddlewares(handler http.Handler) http.Handler { ...@@ -119,9 +119,19 @@ func setupMiddlewares(handler http.Handler) http.Handler {
return handler return handler
} }
// We need this type to act as an adapter between zap and the middleware request logger.
type logAdapter struct {
}
func (l *logAdapter) Print(v ...interface{}) {
log.Logger.Info(v...)
}
// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document. // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
// So this is a good place to plug in a panic handling middleware, logging and metrics // So this is a good place to plug in a panic handling middleware, logging and metrics
func setupGlobalMiddleware(handler http.Handler) http.Handler { func setupGlobalMiddleware(handler http.Handler) http.Handler {
middleware.DefaultLogger = middleware.RequestLogger(
&middleware.DefaultLogFormatter{Logger: &logAdapter{}})
returnHandler := middleware.Logger(handler) returnHandler := middleware.Logger(handler)
returnHandler = middleware.Recoverer(returnHandler) returnHandler = middleware.Recoverer(returnHandler)
returnHandler = middleware.Heartbeat("/ping")(returnHandler) returnHandler = middleware.Heartbeat("/ping")(returnHandler)
......
...@@ -10,17 +10,26 @@ import ( ...@@ -10,17 +10,26 @@ import (
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
var Logger = createGlobalLogger() // Set the default logger to development mode
var Logger *zap.SugaredLogger
func createGlobalLogger() *zap.SugaredLogger { func init() {
cfg := zap.NewDevelopmentConfig() ConfigureLogger("dev")
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder }
func ConfigureLogger(logType string) {
var cfg zap.Config
if logType == "prod" {
cfg = zap.NewProductionConfig()
} else {
cfg = zap.NewDevelopmentConfig()
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
logger, err := cfg.Build() logger, err := cfg.Build()
if err != nil { if err != nil {
log.Fatalln("createLogger", err) log.Fatalln("createLogger", err)
} }
Logger = logger.Sugar()
return logger.Sugar()
} }
func WithRequestID(ctx context.Context, id string) context.Context { func WithRequestID(ctx context.Context, id string) context.Context {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment