How to Suppress Telego Logging: 4 Methods for Silent Bot Operation
You can suppress Telego logging by passing telego.WithDiscardLogger() when constructing your bot, which disables both debug and error output, or by providing a custom Logger implementation that discards all writes.
Telego, the popular Go library for the Telegram Bot API available at mymmrac/telego, writes diagnostic output through a pluggable logging interface attached to every Bot instance. While the default configuration prints debug and error messages to stderr, the library provides several bot options to suppress Telego logging entirely or redirect output for production environments.
Understanding Telego's Logger Architecture
The Bot struct in bot.go holds a log Logger field that receives all internal diagnostic calls. By default, Telego initializes this field with a standard logger implementation defined in logger.go that writes to stderr.
The Logger interface requires only two methods:
type Logger interface {
Debugf(format string, args ...any)
Errorf(format string, args ...any)
}
All logging suppression methods work by either disabling the output flags within the default logger or replacing the implementation entirely.
Methods to Suppress Telego Logging
Use WithDiscardLogger() for Zero Output
The simplest way to silence all logging is using the WithDiscardLogger() option. This functional option, defined in bot_options.go, calls WithDefaultLogger(false, false) internally, creating a logger with both DebugMode and PrintErrors disabled.
package main
import (
"github.com/mymmrac/telego"
)
func main() {
bot, err := telego.NewBot(
"YOUR_BOT_TOKEN",
telego.WithDiscardLogger(), // Suppresses both debug and error logs
)
if err != nil {
panic(err)
}
// Bot operates silently
_ = bot
}
Control Output with WithDefaultLogger()
For fine-grained control, use WithDefaultLogger(debugMode, printErrors) directly. This option, found at lines 50-57 of bot_options.go, accepts two boolean parameters:
debugMode: EnablesDebugfoutput whentrueprintErrors: EnablesErrorfoutput whentrue
To suppress debug messages but retain error logging:
bot, err := telego.NewBot(
"YOUR_BOT_TOKEN",
telego.WithDefaultLogger(false, true), // Debug off, errors on
)
Provide a Custom Silent Logger
You can replace the logger entirely by implementing the Logger interface and passing it via WithLogger(). This option, defined at lines 95-99 of bot_options.go, overwrites the bot's log field with your implementation.
Here is a complete example of a no-op logger that discards all output:
package main
import (
"github.com/mymmrac/telego"
)
type silentLogger struct{}
func (s *silentLogger) Debugf(format string, args ...any) {}
func (s *silentLogger) Errorf(format string, args ...any) {}
func main() {
bot, err := telego.NewBot(
"YOUR_BOT_TOKEN",
telego.WithLogger(&silentLogger{}), // Custom silent implementation
)
if err != nil {
panic(err)
}
_ = bot
}
Use WithExtendedDefaultLogger() for Advanced Control
For scenarios requiring string replacement in log messages while still controlling output, use WithExtendedDefaultLogger(debugMode, printErrors, replacer). As implemented in bot_options.go at lines 64-78, this constructor accepts a *strings.Replacer for message transformation.
Note that suppression still depends on the boolean flags:
bot, err := telego.NewBot(
"TOKEN",
telego.WithExtendedDefaultLogger(false, false, nil), // Silent, no replacement
)
How Logger Suppression Works Under the Hood
The default logger implementation in logger.go checks boolean flags before writing output. When DebugMode is false, the Debugf method returns immediately without formatting or printing. Similarly, when PrintErrors is false, Errorf becomes a no-op.
In bot_options.go, the WithDefaultLogger function (lines 50-57) constructs a logger struct with these flags:
WithDefaultLogger(debugMode bool, printErrors bool) BotOption {
return func(bot *Bot) error {
bot.log = &logger{
DebugMode: debugMode,
PrintErrors: printErrors,
// ... writer initialization
}
return nil
}
}
When you call WithDiscardLogger() (lines 87-90), it simply invokes WithDefaultLogger(false, false), ensuring both diagnostic channels are disabled before the bot begins processing updates.
Summary
- Use
WithDiscardLogger()for the quickest way to suppress all Telego logging output without implementing custom code. - Use
WithDefaultLogger(debugMode, printErrors)to selectively suppress debug messages while preserving error logs, or vice versa. - Use
WithLogger(custom)to inject your ownLoggerimplementation for complete control over log routing, formatting, or discarding. - All suppression methods work by either disabling the
DebugModeandPrintErrorsflags in the default logger (located inlogger.go) or by replacing the logger instance entirely via the options defined inbot_options.go.
Frequently Asked Questions
Can I suppress only debug messages but keep error logging in Telego?
Yes. Pass telego.WithDefaultLogger(false, true) when creating your bot. The first parameter disables Debugf output, while the second ensures Errorf messages continue to print to stderr. This is useful for production environments where you need error visibility without verbose debug noise.
Does Telego log to stdout or stderr by default?
Telego logs to stderr by default. The default logger implementation in logger.go initializes its output writer to os.Stderr, meaning all debug and error diagnostics appear in the standard error stream regardless of whether they represent actual errors or informational debug data.
Can I redirect Telego logs to a file instead of suppressing them?
Yes, though it requires a custom logger implementation. Because the Logger interface only requires Debugf and Errorf methods, you can create a struct that writes to an os.File or io.Writer of your choice, then pass it via telego.WithLogger(). This allows full control over log destination, rotation, and formatting without modifying the library source.
Is the Logger interface compatible with standard Go logging libraries?
While Telego defines its own minimal Logger interface, you can easily adapt standard libraries like log, log/slog, or zap by writing a thin wrapper that implements Debugf and Errorf. The interface signature matches common logging patterns, making integration straightforward without forcing dependencies on specific third-party logging frameworks.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →