A ClickHouse Handler for slog Go library.
See also:
- slog-multi:
slog.Handler
chaining, fanout, routing, failover, load balancing... - slog-formatter:
slog
attribute formatting - slog-sampling:
slog
sampling policy - slog-gin: Gin middleware for
slog
logger - slog-echo: Echo middleware for
slog
logger - slog-fiber: Fiber middleware for
slog
logger - slog-chi: Chi middleware for
slog
logger - slog-datadog: A
slog
handler forDatadog
- slog-rollbar: A
slog
handler forRollbar
- slog-sentry: A
slog
handler forSentry
- slog-syslog: A
slog
handler forSyslog
- slog-logstash: A
slog
handler forLogstash
- slog-fluentd: A
slog
handler forFluentd
- slog-graylog: A
slog
handler forGraylog
- slog-loki: A
slog
handler forLoki
- slog-slack: A
slog
handler forSlack
- slog-telegram: A
slog
handler forTelegram
- slog-mattermost: A
slog
handler forMattermost
- slog-microsoft-teams: A
slog
handler forMicrosoft Teams
- slog-webhook: A
slog
handler forWebhook
- slog-kafka: A
slog
handler forKafka
- slog-nats: A
slog
handler forNATS
- slog-parquet: A
slog
handler forParquet
+Object Storage
- slog-zap: A
slog
handler forZap
- slog-zerolog: A
slog
handler forZerolog
- slog-logrus: A
slog
handler forLogrus
- slog-channel: A
slog
handler for Go channels
go get github.com/smllnest/slog-clickhouse
Compatibility: go >= 1.21
type Option struct {
// log level (default: debug)
Level slog.Leveler
// ClickHouse connection
DB *sql.DB
LogTable string
Timeout time.Duration // default: 60s
// optional: customize clickhouse event builder
Converter Converter
// optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
Other global parameters:
slogclickhouse.SourceKey = "source"
slogclickhouse.ContextKey = "extra"
slogclickhouse.RequestKey = "request"
slogclickhouse.ErrorKeys = []string{"error", "err"}
slogclickhouse.RequestIgnoreHeaders = false
The following attributes are interpreted by slogclickhouse.DefaultConverter:
Atribute name | slog.Kind | Underlying type |
---|---|---|
"error" | any | error |
"request" | any | *http.Request |
other attributes | * |
Other attributes will be injected in extra field.
You must create the table before using it:
CREATE TABLE logs (
timestamp DateTime,
level String,
message String,
attrs String
) ENGINE = MergeTree()
ORDER BY timestamp
PARTITION BY toYYYYMMDD(timestamp)
then run the below code:
package main
import (
"log/slog"
"testing"
"time"
"github.com/smallnest/slog-clickhouse"
)
func main() {
conn := clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{"127.0.0.1:9000"},
Auth: clickhouse.Auth{
Database: "myapp",
Username: "",
Password: "",
},
Settings: clickhouse.Settings{
"max_execution_time": 60,
},
DialTimeout: time.Second * 30,
Debug: true,
BlockBufferSize: 10,
MaxCompressionBuffer: 10240,
})
if err := conn.Ping(); err != nil {
t.Log("local clickhouse server is not running, skipping test...")
return
}
conn.SetMaxIdleConns(5)
conn.SetMaxOpenConns(10)
conn.SetConnMaxLifetime(time.Hour)
logger := slog.New(Option{Level: slog.LevelDebug, DB: conn, LogTable: "myapp.logs"}.NewClickHouseHandler())
logger.Info("Hello, ClickHouse!", "key1", "value1", "key2", 2)
}
check the result in ClickHouse: