0

I am using MQTT paho library that has its own logger interface that is implemented by logrus logger, paho library has multiple levels: ERROR, CRITICAL and so on. I tried to do something like this:

    MQTT.ERROR = app.Log.WithField("level","ERROR")
    MQTT.CRITICAL = app.Log.WithField("level","CRITICAL")

but that does not log because you can not have a field named level, and if you do not add level, it always goes to info level because entry.Print is implementation that does log.Info inside, is there a way to pass a pointer to logrus logger with corresponding log level without making a new wrapper around it.

I know that it can be done with new package that will implement mqtt.Logger with functions that will have log.Error calls inside. But I feel that there should be a bit better way of doing this.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

0

I know that it can be done with new package that will implement mqtt.Logger with functions that will have log.Error calls inside. But I feel that there should be a bit better way of doing this.

It doesn't have to be in a new package, you can just define a struct and make it implement mqtt.Logger in the same package.

I'm afraid there is no better way to do it

aureliar
  • 1,476
  • 4
  • 16
  • Yeah sure it can be defined, but I was thinking that maybe there is a way to do it with logrus, maybe I will poke in logrus and try to make a PR, it seems useful to have a way to "reference" a logger with a certain level. I tried experimenting with hooks but that was totally wrong idea. – Amir Hasanbašić May 19 '21 at 11:13