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() {
func (s *Server) verifyConfiguration(configuration *server.Configuration) error {
if s.conf.Logger == nil {
s.conf.Logger = logrus.New()
s.conf.Logger.Level = logrus.DebugLevel
s.conf.Logger.Formatter = &logrus.TextFormatter{}
s.conf.Logger = server.NewLogger(s.conf.Verbose, s.conf.Quiet, s.conf.LogJSON)
}
server.Logger = s.conf.Logger
irma.Logger = s.conf.Logger
......
......@@ -3,6 +3,7 @@ package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
......@@ -17,6 +18,7 @@ import (
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/fs"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)
var Logger *logrus.Logger = logrus.StandardLogger()
......@@ -41,14 +43,21 @@ type Configuration struct {
IssuerPrivateKeys map[irma.IssuerIdentifier]*gabi.PrivateKey `json:"-"`
// URL at which the IRMA app can reach this server during sessions
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
// See https://github.com/privacybydesign/irmago/tree/master/server#specifying-an-email-address
// for more information
Email string `json:"email" mapstructure:"email"`
// Enable server sent events for status updates (experimental; tends to hang when a reverse proxy is used)
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 {
......@@ -362,3 +371,24 @@ func ToJson(o interface{}) string {
bts, _ := json.Marshal(o)
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
import (
"io/ioutil"
"path/filepath"
"runtime"
"strings"
"github.com/go-errors/errors"
......@@ -13,10 +11,9 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"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 RootCommand = &cobra.Command{
......@@ -37,11 +34,6 @@ var RootCommand = &cobra.Command{
}
func init() {
logger.Level = logrus.InfoLevel
logger.SetFormatter(&prefixed.TextFormatter{
FullTimestamp: true,
DisableColors: runtime.GOOS == "windows",
})
if err := setFlags(RootCommand); err != nil {
die(errors.WrapPrefix(err, "Failed to attach flags to "+RootCommand.Name()+" command", 0))
}
......@@ -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
// Set log level
if viper.GetBool("log-json") {
logger.SetFormatter(&logrus.JSONFormatter{})
}
logger.Level = server.Verbosity(viper.GetInt("verbose"))
if viper.GetBool("quiet") {
logger.Out = ioutil.Discard
}
// Create our logger instance
logger = server.NewLogger(viper.GetInt("verbose"), viper.GetBool("quiet"), viper.GetBool("log-json"))
// First log output: hello, development or production mode, log level
mode := "development"
if viper.GetBool("production") {
mode = "production"
}
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 _, notfound := err.(viper.ConfigFileNotFoundError); notfound {
logger.Info("No configuration file found")
......@@ -193,6 +181,9 @@ func configure(cmd *cobra.Command) error {
URL: viper.GetString("url"),
Email: viper.GetString("email"),
EnableSSE: viper.GetBool("sse"),
Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"),
Logger: logger,
},
Permissions: requestorserver.Permissions{
......@@ -210,9 +201,6 @@ func configure(cmd *cobra.Command) error {
JwtPrivateKey: viper.GetString("jwt-privkey"),
JwtPrivateKeyFile: viper.GetString("jwt-privkey-file"),
MaxRequestAge: viper.GetInt("max-request-age"),
Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"),
StaticPath: viper.GetString("static-path"),
StaticPrefix: viper.GetString("static-prefix"),
......
......@@ -65,10 +65,6 @@ type Configuration struct {
// Host static files under this URL 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"`
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