I would like to disable colorer messages (colored logs in terminal) after creating new echo. Is there possibility to do it or I have to configure whole echo structure myself?
I would like to have colorer disable switch
I can't find colorer disable switch
Run any echo application and try to disable colored logs in terminal.
Or even to be more flexible in terms of production logging I would change "⇛ http server started on %s\n" message in StartServer method to use defined logger or even delete them.
You can typecast Echo#Logger back to gommon/log#Logger and call DisableColor(). To completely disable logs set the log level to OFF. Logger is an interface, so you can definitely extend it to use any external logging library. If you have more ideas don't hesitate to comment.
@vishr I don't want to turn off logs. I want to log to STDOUT logs relevant to my business logic with a logger that I am using also in different part of service (I use echo asynchronously).
According to the code in echo repository:
// StartServer starts a custom http server.
func (e *Echo) StartServer(s *http.Server) (err error) {
// Setup
e.colorer.SetOutput(e.Logger.Output())
s.Handler = e
s.ErrorLog = e.stdLogger
if s.TLSConfig == nil {
if e.Listener == nil {
e.Listener, err = newListener(s.Addr)
if err != nil {
return err
}
}
e.colorer.Printf("⇛ http server started on %s\n", e.colorer.Green(e.Listener.Addr()))
return s.Serve(e.Listener)
}
if e.TLSListener == nil {
l, err := newListener(s.Addr)
if err != nil {
return err
}
e.TLSListener = tls.NewListener(l, s.TLSConfig)
}
e.colorer.Printf("⇛ https server started on %s\n", e.colorer.Green(e.TLSListener.Addr()))
return s.Serve(e.TLSListener)
}
colorer is a private value of echo structure and is created always logger doesn't have anything common with it.
My code:
package server
import (
"fmt"
"time"
"github.com/Sirupsen/logrus"
"github.com/karhoo/svc-users/app/configuration"
"github.com/karhoo/svc-users/app/interface/rest/handler"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/pkg/errors"
"github.com/sandalwing/echo-logrusmiddleware"
"golang.org/x/net/context"
)
type WebHookServer struct {
echo *echo.Echo
conf configuration.WebHookServer
}
func NewWebHookServer(conf configuration.WebHookServer, log *logrus.Logger, handler *handler.WebHookHandler) *WebHookServer {
e := echo.New()
e.Logger = logrusmiddleware.Logger{Logger: log}
e.Use(logrusmiddleware.Hook())
e.Use(middleware.Recover())
e.GET("/postUserRegistration", handler.PostUserRegistrationHookHandler)
return &WebHookServer{
echo: e,
conf: conf,
}
}
func (s *WebHookServer) Start() {
go func() {
if err := s.echo.Start(fmt.Sprintf(":%d", s.conf.Port)); err != nil {
s.echo.Logger.Fatal(errors.Wrap(err, "Failed to start WebHookServer"))
}
s.echo.Logger.Info("WebHookServer started")
}()
}
func (s *WebHookServer) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := s.echo.Shutdown(ctx); err != nil {
s.echo.Logger.Fatal(errors.Wrap(err, "failed to shutdown WebHookServer"))
}
}
I can't see how to do it. Could you point me on the right track?
Echo#colorer is only used to display the banner/info during the startup. Apart from that all the colors you see are probably from logger instance. Colors get automatically disabled for non-terminal output i.e. writing to a file or terminals that don't support it. Did you try to call DisableColor() on logger after typecasting?
Update: If you are just talking about the initial setup color logs, please let me know - we can definitely do something about it.
@vishr Yes I am talking only about initial setup color logs.
@Gobonoid You can use Echo#HideBanner to hide the startup message.
Most helpful comment
@Gobonoid You can use
Echo#HideBannerto hide the startup message.