Commit 9b7a55c1 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Make verbosity config options also usable for irmaserver library

parent 2b31fbd9
...@@ -55,9 +55,7 @@ func (s *Server) Stop() { ...@@ -55,9 +55,7 @@ func (s *Server) Stop() {
func (s *Server) verifyConfiguration(configuration *server.Configuration) error { func (s *Server) verifyConfiguration(configuration *server.Configuration) error {
if s.conf.Logger == nil { if s.conf.Logger == nil {
s.conf.Logger = logrus.New() s.conf.Logger = server.NewLogger(s.conf.Verbose, s.conf.Quiet, s.conf.LogJSON)
s.conf.Logger.Level = logrus.DebugLevel
s.conf.Logger.Formatter = &logrus.TextFormatter{}
} }
server.Logger = s.conf.Logger server.Logger = s.conf.Logger
irma.Logger = s.conf.Logger irma.Logger = s.conf.Logger
......
...@@ -3,6 +3,7 @@ package server ...@@ -3,6 +3,7 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"os" "os"
...@@ -17,6 +18,7 @@ import ( ...@@ -17,6 +18,7 @@ import (
"github.com/privacybydesign/irmago" "github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/fs" "github.com/privacybydesign/irmago/internal/fs"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
) )
var Logger *logrus.Logger = logrus.StandardLogger() var Logger *logrus.Logger = logrus.StandardLogger()
...@@ -41,14 +43,21 @@ type Configuration struct { ...@@ -41,14 +43,21 @@ type Configuration struct {
IssuerPrivateKeys map[irma.IssuerIdentifier]*gabi.PrivateKey `json:"-"` IssuerPrivateKeys map[irma.IssuerIdentifier]*gabi.PrivateKey `json:"-"`
// URL at which the IRMA app can reach this server during sessions // URL at which the IRMA app can reach this server during sessions
URL string `json:"url" mapstructure:"url"` URL string `json:"url" mapstructure:"url"`
// Logging
Logger *logrus.Logger `json:"-"`
// (Optional) email address of server admin, for incidental notifications such as breaking API changes // (Optional) email address of server admin, for incidental notifications such as breaking API changes
// See https://github.com/privacybydesign/irmago/tree/master/server#specifying-an-email-address // See https://github.com/privacybydesign/irmago/tree/master/server#specifying-an-email-address
// for more information // for more information
Email string `json:"email" mapstructure:"email"` Email string `json:"email" mapstructure:"email"`
// Enable server sent events for status updates (experimental; tends to hang when a reverse proxy is used) // Enable server sent events for status updates (experimental; tends to hang when a reverse proxy is used)
EnableSSE bool EnableSSE bool
// Logging verbosity level: 0 is normal, 1 includes DEBUG level, 2 includes TRACE level
Verbose int `json:"verbose" mapstructure:"verbose"`
// Don't log anything at all
Quiet bool `json:"quiet" mapstructure:"quiet"`
// Output structured log in JSON format
LogJSON bool `json:"log_json" mapstructure:"log_json"`
// Custom logger instance. If specified, Verbose, Quiet and LogJSON are ignored.
Logger *logrus.Logger `json:"-"`
} }
type SessionPackage struct { type SessionPackage struct {
...@@ -362,3 +371,24 @@ func ToJson(o interface{}) string { ...@@ -362,3 +371,24 @@ func ToJson(o interface{}) string {
bts, _ := json.Marshal(o) bts, _ := json.Marshal(o)
return string(bts) return string(bts)
} }
func NewLogger(verbosity int, quiet bool, json bool) *logrus.Logger {
logger := logrus.New()
if quiet {
logger.Out = ioutil.Discard
return logger
}
logger.Level = Verbosity(verbosity)
if json {
logger.SetFormatter(&logrus.JSONFormatter{})
} else {
logger.SetFormatter(&prefixed.TextFormatter{
FullTimestamp: true,
DisableColors: runtime.GOOS == "windows",
})
}
return logger
}
package cmd package cmd
import ( import (
"io/ioutil"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"github.com/go-errors/errors" "github.com/go-errors/errors"
...@@ -13,10 +11,9 @@ import ( ...@@ -13,10 +11,9 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/x-cray/logrus-prefixed-formatter"
) )
var logger = logrus.StandardLogger() var logger = server.NewLogger(0, false, false)
var conf *requestorserver.Configuration var conf *requestorserver.Configuration
var RootCommand = &cobra.Command{ var RootCommand = &cobra.Command{
...@@ -37,11 +34,6 @@ var RootCommand = &cobra.Command{ ...@@ -37,11 +34,6 @@ var RootCommand = &cobra.Command{
} }
func init() { func init() {
logger.Level = logrus.InfoLevel
logger.SetFormatter(&prefixed.TextFormatter{
FullTimestamp: true,
DisableColors: runtime.GOOS == "windows",
})
if err := setFlags(RootCommand); err != nil { if err := setFlags(RootCommand); err != nil {
die(errors.WrapPrefix(err, "Failed to attach flags to "+RootCommand.Name()+" command", 0)) die(errors.WrapPrefix(err, "Failed to attach flags to "+RootCommand.Name()+" command", 0))
} }
...@@ -157,21 +149,17 @@ func configure(cmd *cobra.Command) error { ...@@ -157,21 +149,17 @@ func configure(cmd *cobra.Command) error {
} }
err := viper.ReadInConfig() // Hold error checking until we know how much of it to log err := viper.ReadInConfig() // Hold error checking until we know how much of it to log
// Set log level // Create our logger instance
if viper.GetBool("log-json") { logger = server.NewLogger(viper.GetInt("verbose"), viper.GetBool("quiet"), viper.GetBool("log-json"))
logger.SetFormatter(&logrus.JSONFormatter{})
}
logger.Level = server.Verbosity(viper.GetInt("verbose"))
if viper.GetBool("quiet") {
logger.Out = ioutil.Discard
}
// First log output: hello, development or production mode, log level
mode := "development" mode := "development"
if viper.GetBool("production") { if viper.GetBool("production") {
mode = "production" mode = "production"
} }
logger.WithField("mode", mode).WithField("verbosity", server.Verbosity(viper.GetInt("verbose"))).Info("irma server running") logger.WithField("mode", mode).WithField("verbosity", server.Verbosity(viper.GetInt("verbose"))).Info("irma server running")
// Now we finally examine and log any error from viper.ReadInConfig()
if err != nil { if err != nil {
if _, notfound := err.(viper.ConfigFileNotFoundError); notfound { if _, notfound := err.(viper.ConfigFileNotFoundError); notfound {
logger.Info("No configuration file found") logger.Info("No configuration file found")
...@@ -193,6 +181,9 @@ func configure(cmd *cobra.Command) error { ...@@ -193,6 +181,9 @@ func configure(cmd *cobra.Command) error {
URL: viper.GetString("url"), URL: viper.GetString("url"),
Email: viper.GetString("email"), Email: viper.GetString("email"),
EnableSSE: viper.GetBool("sse"), EnableSSE: viper.GetBool("sse"),
Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"),
Logger: logger, Logger: logger,
}, },
Permissions: requestorserver.Permissions{ Permissions: requestorserver.Permissions{
...@@ -210,9 +201,6 @@ func configure(cmd *cobra.Command) error { ...@@ -210,9 +201,6 @@ func configure(cmd *cobra.Command) error {
JwtPrivateKey: viper.GetString("jwt-privkey"), JwtPrivateKey: viper.GetString("jwt-privkey"),
JwtPrivateKeyFile: viper.GetString("jwt-privkey-file"), JwtPrivateKeyFile: viper.GetString("jwt-privkey-file"),
MaxRequestAge: viper.GetInt("max-request-age"), MaxRequestAge: viper.GetInt("max-request-age"),
Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"),
StaticPath: viper.GetString("static-path"), StaticPath: viper.GetString("static-path"),
StaticPrefix: viper.GetString("static-prefix"), StaticPrefix: viper.GetString("static-prefix"),
......
...@@ -65,10 +65,6 @@ type Configuration struct { ...@@ -65,10 +65,6 @@ type Configuration struct {
// Host static files under this URL prefix // Host static files under this URL prefix
StaticPrefix string `json:"static_prefix" mapstructure:"static_prefix"` StaticPrefix string `json:"static_prefix" mapstructure:"static_prefix"`
Verbose int `json:"verbose" mapstructure:"verbose"`
Quiet bool `json:"quiet" mapstructure:"quiet"`
LogJSON bool `json:"log_json" mapstructure:"log_json"`
Production bool `json:"production" mapstructure:"production"` Production bool `json:"production" mapstructure:"production"`
jwtPrivateKey *rsa.PrivateKey jwtPrivateKey *rsa.PrivateKey
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment