mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
add LogLevel/WithLogLevel in Configuration and run Configurators before Build state
Former-commit-id: d38f1fdae98d650692775f4cee06bd017aba959f
This commit is contained in:
parent
6a6117eb4f
commit
116503a9a5
|
@ -61,6 +61,14 @@ type myController struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SayHello implements helloworld.GreeterServer.
|
// SayHello implements helloworld.GreeterServer.
|
||||||
|
// See https://github.com/kataras/iris/issues/1449#issuecomment-625570442
|
||||||
|
// for the comments below (https://github.com/iris-contrib/swagger).
|
||||||
|
//
|
||||||
|
// @Description greet service
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {string} string "Hello {name}"
|
||||||
|
// @Router /helloworld.Greeter/SayHello [post]
|
||||||
func (c *myController) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
func (c *myController) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
||||||
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
|
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,6 +185,13 @@ var WithGlobalConfiguration = func(app *Application) {
|
||||||
app.Configure(WithConfiguration(YAML(globalConfigurationKeyword)))
|
app.Configure(WithConfiguration(YAML(globalConfigurationKeyword)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithLogLevel sets the `Configuration.LogLevel` field.
|
||||||
|
func WithLogLevel(level string) Configurator {
|
||||||
|
return func(app *Application) {
|
||||||
|
app.config.LogLevel = level
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithoutServerError will cause to ignore the matched "errors"
|
// WithoutServerError will cause to ignore the matched "errors"
|
||||||
// from the main application's `Run/Listen` function.
|
// from the main application's `Run/Listen` function.
|
||||||
//
|
//
|
||||||
|
@ -717,10 +724,24 @@ func (tc TunnelingConfiguration) createTunnel(tunnelAPIRequest ngrokTunnel, publ
|
||||||
// these can be passed via options also, look at the top of this file(configuration.go).
|
// these can be passed via options also, look at the top of this file(configuration.go).
|
||||||
// Configuration is a valid OptionSetter.
|
// Configuration is a valid OptionSetter.
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
// vhost is private and set only with .Run method, it cannot be changed after the first set.
|
// vhost is private and set only with .Run/Listen methods, it cannot be changed after the first set.
|
||||||
// It can be retrieved by the context if needed (i.e router for subdomains)
|
// It can be retrieved by the context if needed (i.e router for subdomains)
|
||||||
vhost string
|
vhost string
|
||||||
|
|
||||||
|
// LogLevel is the log level the application should use to output messages.
|
||||||
|
// Logger, by default, is mostly used on Build state but it is also possible
|
||||||
|
// that debug error messages could be thrown when the app is running, e.g.
|
||||||
|
// when malformed data structures try to be sent on Client (i.e Context.JSON/JSONP/XML...).
|
||||||
|
//
|
||||||
|
// Defaults to "info". Possible values are:
|
||||||
|
// * "disable"
|
||||||
|
// * "fatal"
|
||||||
|
// * "error"
|
||||||
|
// * "warn"
|
||||||
|
// * "info"
|
||||||
|
// * "debug"
|
||||||
|
LogLevel string `json:"logLevel" yaml:"LogLevel" toml:"LogLevel" env:"LOG_LEVEL"`
|
||||||
|
|
||||||
// Tunneling can be optionally set to enable ngrok http(s) tunneling for this Iris app instance.
|
// Tunneling can be optionally set to enable ngrok http(s) tunneling for this Iris app instance.
|
||||||
// See the `WithTunneling` Configurator too.
|
// See the `WithTunneling` Configurator too.
|
||||||
Tunneling TunnelingConfiguration `json:"tunneling,omitempty" yaml:"Tunneling" toml:"Tunneling"`
|
Tunneling TunnelingConfiguration `json:"tunneling,omitempty" yaml:"Tunneling" toml:"Tunneling"`
|
||||||
|
@ -924,6 +945,12 @@ func (c Configuration) GetVHost() string {
|
||||||
return c.vhost
|
return c.vhost
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLogLevel returns the `Configuration.LogLevel` field.
|
||||||
|
// The same (as `golog.LogLevel`) can be retrieved through `app.Logger().Level`.
|
||||||
|
func (c Configuration) GetLogLevel() string {
|
||||||
|
return c.vhost
|
||||||
|
}
|
||||||
|
|
||||||
// GetDisablePathCorrection returns the Configuration#DisablePathCorrection.
|
// GetDisablePathCorrection returns the Configuration#DisablePathCorrection.
|
||||||
// DisablePathCorrection disables the correcting
|
// DisablePathCorrection disables the correcting
|
||||||
// and redirecting or executing directly the handler of
|
// and redirecting or executing directly the handler of
|
||||||
|
@ -1094,6 +1121,10 @@ func WithConfiguration(c Configuration) Configurator {
|
||||||
return func(app *Application) {
|
return func(app *Application) {
|
||||||
main := app.config
|
main := app.config
|
||||||
|
|
||||||
|
if v := c.LogLevel; v != "" {
|
||||||
|
main.LogLevel = v
|
||||||
|
}
|
||||||
|
|
||||||
if c.Tunneling.isEnabled() {
|
if c.Tunneling.isEnabled() {
|
||||||
main.Tunneling = c.Tunneling
|
main.Tunneling = c.Tunneling
|
||||||
}
|
}
|
||||||
|
@ -1201,6 +1232,7 @@ func WithConfiguration(c Configuration) Configurator {
|
||||||
// DefaultConfiguration returns the default configuration for an iris station, fills the main Configuration
|
// DefaultConfiguration returns the default configuration for an iris station, fills the main Configuration
|
||||||
func DefaultConfiguration() Configuration {
|
func DefaultConfiguration() Configuration {
|
||||||
return Configuration{
|
return Configuration{
|
||||||
|
LogLevel: "info",
|
||||||
DisableStartupLog: false,
|
DisableStartupLog: false,
|
||||||
DisableInterruptHandler: false,
|
DisableInterruptHandler: false,
|
||||||
DisablePathCorrection: false,
|
DisablePathCorrection: false,
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import "github.com/kataras/iris/v12/core/netutil"
|
||||||
"github.com/kataras/iris/v12/core/netutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ConfigurationReadOnly can be implemented
|
// ConfigurationReadOnly can be implemented
|
||||||
// by Configuration, it's being used inside the Context.
|
// by Configuration, it's being used inside the Context.
|
||||||
|
@ -17,6 +15,10 @@ type ConfigurationReadOnly interface {
|
||||||
// If original addr was 0.0.0.0, it will return localhost.
|
// If original addr was 0.0.0.0, it will return localhost.
|
||||||
GetVHost() string
|
GetVHost() string
|
||||||
|
|
||||||
|
// GetLogLevel returns the `Configuration.LogLevel` field.
|
||||||
|
// The same (as `golog.LogLevel`) can be retrieved through `app.Logger().Level`.
|
||||||
|
GetLogLevel() string
|
||||||
|
|
||||||
// GetDisablePathCorrection returns the configuration.DisablePathCorrection,
|
// GetDisablePathCorrection returns the configuration.DisablePathCorrection,
|
||||||
// DisablePathCorrection corrects and redirects the requested path to the registered path
|
// DisablePathCorrection corrects and redirects the requested path to the registered path
|
||||||
// for example, if /home/ path is requested but no handler for this Route found,
|
// for example, if /home/ path is requested but no handler for this Route found,
|
||||||
|
|
|
@ -50,12 +50,14 @@ var ErrNotRouteAdder = errors.New("request handler does not implement AddRoute m
|
||||||
// Works before or after Build state.
|
// Works before or after Build state.
|
||||||
// Mainly used for internal cases like `iris.WithSitemap`.
|
// Mainly used for internal cases like `iris.WithSitemap`.
|
||||||
// Do NOT use it on serve-time.
|
// Do NOT use it on serve-time.
|
||||||
func (router *Router) AddRouteUnsafe(r *Route) error {
|
func (router *Router) AddRouteUnsafe(routes ...*Route) error {
|
||||||
if h := router.requestHandler; h != nil {
|
if h := router.requestHandler; h != nil {
|
||||||
if v, ok := h.(interface {
|
if v, ok := h.(interface {
|
||||||
AddRoute(*Route) error
|
AddRoute(*Route) error
|
||||||
}); ok {
|
}); ok {
|
||||||
return v.AddRoute(r)
|
for _, r := range routes {
|
||||||
|
return v.AddRoute(r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ func New(t *testing.T, app *iris.Application, setters ...OptionSetter) *httpexpe
|
||||||
app.Logger().SetLevel(conf.LogLevel)
|
app.Logger().SetLevel(conf.LogLevel)
|
||||||
|
|
||||||
if err := app.Build(); err != nil {
|
if err := app.Build(); err != nil {
|
||||||
if conf.LogLevel != "disable" && conf.LogLevel != "disabled" {
|
if conf.LogLevel != "disable" {
|
||||||
app.Logger().Println(err.Error())
|
app.Logger().Println(err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
30
iris.go
30
iris.go
|
@ -1,7 +1,6 @@
|
||||||
package iris
|
package iris
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// std packages
|
|
||||||
"bytes"
|
"bytes"
|
||||||
stdContext "context"
|
stdContext "context"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -16,27 +15,18 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// context for the handlers
|
"github.com/kataras/iris/v12/cache"
|
||||||
"github.com/kataras/iris/v12/context"
|
"github.com/kataras/iris/v12/context"
|
||||||
"github.com/kataras/iris/v12/hero"
|
|
||||||
|
|
||||||
// core packages, required to build the application
|
|
||||||
"github.com/kataras/iris/v12/core/errgroup"
|
"github.com/kataras/iris/v12/core/errgroup"
|
||||||
|
"github.com/kataras/iris/v12/core/handlerconv"
|
||||||
"github.com/kataras/iris/v12/core/host"
|
"github.com/kataras/iris/v12/core/host"
|
||||||
"github.com/kataras/iris/v12/core/netutil"
|
"github.com/kataras/iris/v12/core/netutil"
|
||||||
"github.com/kataras/iris/v12/core/router"
|
"github.com/kataras/iris/v12/core/router"
|
||||||
|
"github.com/kataras/iris/v12/hero"
|
||||||
// handlerconv conversions
|
|
||||||
"github.com/kataras/iris/v12/core/handlerconv"
|
|
||||||
// cache conversions
|
|
||||||
"github.com/kataras/iris/v12/cache"
|
|
||||||
// view
|
|
||||||
"github.com/kataras/iris/v12/view"
|
|
||||||
// i18n
|
|
||||||
"github.com/kataras/iris/v12/i18n"
|
"github.com/kataras/iris/v12/i18n"
|
||||||
// handlers used in `Default` function
|
|
||||||
requestLogger "github.com/kataras/iris/v12/middleware/logger"
|
requestLogger "github.com/kataras/iris/v12/middleware/logger"
|
||||||
"github.com/kataras/iris/v12/middleware/recover"
|
"github.com/kataras/iris/v12/middleware/recover"
|
||||||
|
"github.com/kataras/iris/v12/view"
|
||||||
|
|
||||||
"github.com/kataras/golog"
|
"github.com/kataras/golog"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
@ -274,6 +264,7 @@ func (app *Application) ConfigurationReadOnly() context.ConfigurationReadOnly {
|
||||||
// - "info"
|
// - "info"
|
||||||
// - "debug"
|
// - "debug"
|
||||||
// Usage: app.Logger().SetLevel("error")
|
// Usage: app.Logger().SetLevel("error")
|
||||||
|
// Or set the level through Configurartion's LogLevel or WithLogLevel functional option.
|
||||||
// Defaults to "info" level.
|
// Defaults to "info" level.
|
||||||
//
|
//
|
||||||
// Callers can use the application's logger which is
|
// Callers can use the application's logger which is
|
||||||
|
@ -745,6 +736,12 @@ func (app *Application) Build() error {
|
||||||
// start := time.Now()
|
// start := time.Now()
|
||||||
app.builded = true // even if fails.
|
app.builded = true // even if fails.
|
||||||
|
|
||||||
|
// check if a prior app.Logger().SetLevel called and if not
|
||||||
|
// then set the defined configuration's log level.
|
||||||
|
if app.logger.Level == golog.InfoLevel /* the default level */ {
|
||||||
|
app.logger.SetLevel(app.config.LogLevel)
|
||||||
|
}
|
||||||
|
|
||||||
rp := errgroup.New("Application Builder")
|
rp := errgroup.New("Application Builder")
|
||||||
rp.Err(app.APIBuilder.GetReporter())
|
rp.Err(app.APIBuilder.GetReporter())
|
||||||
|
|
||||||
|
@ -1028,14 +1025,13 @@ func (app *Application) Listen(hostPort string, withOrWithout ...Configurator) e
|
||||||
// the following runners:
|
// the following runners:
|
||||||
// `Listener`, `Server`, `Addr`, `TLS`, `AutoTLS` and `Raw`.
|
// `Listener`, `Server`, `Addr`, `TLS`, `AutoTLS` and `Raw`.
|
||||||
func (app *Application) Run(serve Runner, withOrWithout ...Configurator) error {
|
func (app *Application) Run(serve Runner, withOrWithout ...Configurator) error {
|
||||||
// first Build because it doesn't need anything from configuration,
|
app.Configure(withOrWithout...)
|
||||||
// this gives the user the chance to modify the router inside a configurator as well.
|
|
||||||
if err := app.Build(); err != nil {
|
if err := app.Build(); err != nil {
|
||||||
app.logger.Error(err)
|
app.logger.Error(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Configure(withOrWithout...)
|
|
||||||
app.tryStartTunneling()
|
app.tryStartTunneling()
|
||||||
|
|
||||||
if len(app.Hosts) > 0 {
|
if len(app.Hosts) > 0 {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user