wip: refactory log with zap

This commit is contained in:
loveuer
2025-03-10 22:41:10 +08:00
parent 0cf05cdf30
commit 1ea5507e5f
12 changed files with 82 additions and 225 deletions

View File

@ -32,11 +32,11 @@ func New(uri string) (*gorm.DB, error) {
switch ins.Scheme {
case "sqlite":
path := strings.TrimPrefix(uri, ins.Scheme+"://")
log.Debug("db.New: type = %s, path = %s", ins.Scheme, path)
log.Logger.Sugar().Debugf("db.New: type = %s, path = %s", ins.Scheme, path)
tx, err = gorm.Open(sqlite.Open(path))
case "mysql", "mariadb":
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s", username, password, ins.Host, ins.Path, ins.RawQuery)
log.Debug("db.New: type = %s, dsn = %s", ins.Scheme, dsn)
log.Logger.Sugar().Debugf("db.New: type = %s, dsn = %s", ins.Scheme, dsn)
tx, err = gorm.Open(mysql.Open(dsn))
case "pg", "postgres", "postgresql":
opts := make([]string, 0)
@ -44,7 +44,7 @@ func New(uri string) (*gorm.DB, error) {
opts = append(opts, fmt.Sprintf("%s=%s", key, val))
}
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s %s", ins.Hostname(), username, password, ins.Path, ins.Port(), strings.Join(opts, " "))
log.Debug("db.New: type = %s, dsn = %s", ins.Scheme, dsn)
log.Logger.Sugar().Debugf("db.New: type = %s, dsn = %s", ins.Scheme, dsn)
tx, err = gorm.Open(postgres.Open(dsn))
default:
return nil, fmt.Errorf("invalid database type(uri_scheme): %s", ins.Scheme)