Skip to main content

Overview

Gogram provides a flexible logging system. By default, it uses a built-in logger that prints to stdout with different log levels. You can configure the default logger or replace it entirely with your own implementation.

Default Logger

You can configure the default logger when initializing the client.
client, _ := telegram.NewClient(telegram.ClientConfig{
    LogLevel: telegram.LogDebug, // Set log level
    Logger:   telegram.NewDefaultLogger("my-bot"), // Custom prefix
})

Custom Logger

You can integrate Gogram with your existing logging infrastructure (e.g., Zap, Logrus) by implementing the telegram.Logger interface.

Simple Wrapper

For most use cases, you can use WrapSimpleLogger which requires implementing only a few basic methods.
// Assume you have a Zap logger
type ZapAdapter struct {
    *zap.SugaredLogger
}

// Implement the SimpleLogger interface methods
func (z *ZapAdapter) Debug(msg any, args ...any) { z.Debugf(fmt.Sprint(msg), args...) }
func (z *ZapAdapter) Info(msg any, args ...any)  { z.Infof(fmt.Sprint(msg), args...) }
func (z *ZapAdapter) Warn(msg any, args ...any)  { z.Warnf(fmt.Sprint(msg), args...) }
func (z *ZapAdapter) Error(msg any, args ...any) { z.Errorf(fmt.Sprint(msg), args...) }
// ... implement GetLevel/SetLevel etc.

// Usage
client, _ := telegram.NewClient(telegram.ClientConfig{
    Logger: telegram.WrapSimpleLogger(&ZapAdapter{logger}),
})

Advanced Logger Implementation

If you need full control (formatting, structured logging, fields), implement the full telegram.Logger interface directly.
type Logger interface {
    SetLevel(telegram.LogLevel) Logger
    WithField(key string, value any) Logger
    Debug(msg any, args ...any)
    Info(msg any, args ...any)
    Warn(msg any, args ...any)
    Error(msg any, args ...any)
    Lev() LogLevel
    SetColor(bool)
    Color() bool
    SetPrefix(string)
    CloneInternal() Logger
    // ... possibly others, check source
}