tucnak / telebot

Telebot is a Telegram bot framework in Go.
MIT License
4.04k stars 469 forks source link

Is it possible to pass a logger interface to enable custom logger instance? #619

Closed greenhat616 closed 1 year ago

greenhat616 commented 1 year ago

Despite Go's poor design in not providing a standard Logger interface, we can still achieve a unified Logger instance by customizing the Logger interface. For example, I prefer using JSON logs for easy log collection.

Example

type Logger interface {
    Debugw(msg string, fields ...interface{})
    Infow(msg string, fields ...interface{})
    Warnw(msg string, fields ...interface{})
    Errorw(msg string, fields ...interface{})
}

Later on, you only need to provide a Logger option when creating the robot (we can implement the Logger interface you require), and you can provide a default logger implementation at creation. I believe such changes are not very invasive.

TheOrdinaryWow commented 1 year ago

Indeed, it'd be quite useful if zap could be introduced as logger instead of using the built-in log.

demget commented 1 year ago

All the log calls are adjustable: you can write your own middleware for logging incoming requests with the logger package you like. There is also a Bot.OnError field to override the error-handler.