
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
github.com/neirpyc/slog-gorm
Advanced tools
slog-gorm provides a slog adapter, highly configurable, for gorm logger
to have homogeneous logs between your application / script and gorm.
slog.Handler, which allows you to keep control on
the format of your logs.slog.Level for errors, slow queries or the other logs.golang >= 1.21import (
"log/slog"
"os"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
slogGorm "github.com/orandin/slog-gorm"
)
// Create an slog-gorm instance
gormLogger := slogGorm.New() // use slog.Default() by default
// GORM: Globally mode
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
Logger: gormLogger,
})
// GORM: Continuous session mode
tx := db.Session(&Session{Logger: gormLogger})
tx.First(&user)
tx.Model(&user).Update("Age", 18)
slog.LoggerThe following example shows you how to use a specific slog.Logger with slog-gorm:
// With your slog.Logger
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
// Also, you can set specific attributes to distinguish between your application logs and gorm logs
// logger = logger.With(slog.String("log_type", "database"))
gormLogger := slogGorm.New(
slogGorm.WithLogger(logger), // Optional, use slog.Default() by default
slogGorm.WithTraceAll(slog.Info), // trace all messages and define the default logging level
)
slog.LevelAs some loggers (e.g. syslog) have their own logging levels, slog-gorm lets you
use them to ensure the consistency of your logs and make them easier to understand.
You can set the logging level for these log types:
| Type | Description | Default |
|---|---|---|
slogGorm.ErrorLogType | For SQL errors | slog.LevelError |
slogGorm.SlowQueryLogType | For slow queries | slog.LevelWarn |
slogGorm.DefaultLogType | For other messages (default level) | slog.LevelInfo |
Example:
const (
LOG_EMERG = slog.Level(0)
// ...
LOG_ERR = slog.Level(3)
LOG_WARNING = slog.Level(4)
LOG_NOTICE = slog.Level(5)
// ...
LOG_DEBUG = slog.Level(7)
)
logger := slog.New(syslogHandler)
gormLogger := slogGorm.New(
slogGorm.WithLogger(logger),
// Set logging level for SQL errors
slogGorm.SetLogLevel(slogGorm.ErrorLogType, LOG_ERR)
// Set logging level for slow queries
slogGorm.SetLogLevel(slogGorm.SlowQueryLogType, LOG_NOTICE)
// Set logging level for other messages (default level)
slogGorm.SetLogLevel(slogGorm.DefaultLogType, LOG_DEBUG)
)
customLogger := sloggorm.New(
sloggorm.WithSlowThreshold(500 * time.Millisecond), // to identify slow queries
sloggorm.WithRecordNotFoundError(), // don't ignore not found errors
sloggorm.WithSourceField("origin"), // instead of "file" (by default)
slogGorm.WithErrorField("err"), // instead of "error" (by default)
)
By default, the slow queries and SQL errors are logged, but you can ignore all SQL messages with WithIgnoreTrace().
customLogger := sloggorm.New(
sloggorm.WithIgnoreTrace(), // disable the tracing of SQL queries by the logger.
)
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.