Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging to ease debugging connection reattempts #77

Open
blitzrk opened this issue Nov 13, 2016 · 3 comments
Open

Add logging to ease debugging connection reattempts #77

blitzrk opened this issue Nov 13, 2016 · 3 comments

Comments

@blitzrk
Copy link

blitzrk commented Nov 13, 2016

Background: I was setting up tls+tcp connection with mutual authentication. I didn't realize that I needed to provide a ServerName in the tls.Config, so mangos kept trying to reconnect, but I just saw this as a timeout occurring from my code. I eventually had to throw in print statements throughout core.go to find the cause.

I propose adding a very simple logging solution.

My idea is to let users register loggers (like how database/sql registers drivers). A default one that logs to stderr could be provided in a mangos subpackage. I'll submit a PR with a proof of concept in just a minute. Using it would look like this:

// Not a working example
package mangos_test

import (
        "crypto/tls"
        "crypto/x509"
        "log"

        "github.com/go-mangos/mangos"
        "github.com/go-mangos/mangos/protocol/push"
        "github.com/go-mangos/mangos/transport/tlstcp"

        _ "github.com/go-mangos/mangos/log"
)

var (
        url  = "localhost:5555"
        conf = &tls.Config{
                Certificates: []tls.Certificate{},
                RootCAs:      x509.NewCertPool(),
        }
)

func init() {
        mangos.SetLogLevel(2)
}

func Example() {
        sock, err = push.NewSocket()
        if err != nil {
                log.Fatalf("can't get new push socket: %v", err)
        }
        sock.AddTransport(tlstcp.NewTransport())
        if err = sock.DialOption("tls+tcp://"+url, conf); err != nil {
                log.Fatalf("can't dial on push socket: %v", err)
        }
        defer sock.Close()

        if err = sock.Send([]byte("Hello world!")); err != nil {
                log.Fatalf("can't send message on push socket: %v", err)
        }
}
@blitzrk
Copy link
Author

blitzrk commented Nov 14, 2016

By default, all calls in mangos that use log.{Info,Infof,Debug,Debugf} would be noops. By importing a logger such as with import _ "github.com/go-mangos/mangos/log" a stderr logger would be registered, but all logging would still be a noop until a log level was set. With a call to mangos.SetLogLevel(1), future Info logging would be logged. At log level 2 (or greater), debug info would also be logged.

I only added a single log usage to the main code where it was useful for me, but more could easily be added.

@lobocv
Copy link
Contributor

lobocv commented Dec 18, 2018

@gdamore You've mentioned your aversion to adding dependencies to the project (logrus), if that is still the case then may I suggest we define an interface that defines hooks which we can then add to the library.
An example of a hook would be a func(args map[string]interface{}). The user can then define their adapter which could implement the func and translate it into their preferred logger (ie stdlib or logrus).

What do you think?

Example:
https://play.golang.org/p/_r-a-n09GY9

@gdamore
Copy link
Contributor

gdamore commented Nov 18, 2019

I'd be fine with a hook style interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants