51 lines
981 B
Go
51 lines
981 B
Go
package error_log
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
"sismedika.com/sas/westone/utils"
|
|
)
|
|
|
|
type Store struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
func NewStore(db *sqlx.DB) *Store {
|
|
return &Store{db: db}
|
|
}
|
|
|
|
func (s *Store) CreateErrorLog(errval utils.LogError) error {
|
|
tx, err := s.db.BeginTxx(context.Background(), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func() {
|
|
if err != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
qry := `INSERT INTO westone_log.error_log
|
|
(ErrorLogCode, ErrorTraceID, ErrorLogType, ErrorLogParams, ErrorLogQuery, ErrorMessage, ErrorLogTimestamp)
|
|
VALUES (:ErrorLogCode, :ErrorTraceID, :ErrorLogType, :ErrorLogParams, :ErrorLogQuery, :ErrorMessage, :ErrorLogTimestamp)`
|
|
|
|
_, err = tx.NamedExec(qry, errval)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
delqry := `DELETE FROM error_log WHERE ErrorLogTimestamp < DATE_SUB(NOW(), INTERVAL 1 MONTH)`
|
|
_, err = tx.Exec(delqry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|