log search implementation
This commit is contained in:
parent
01c285de1b
commit
1397274967
2 changed files with 71 additions and 15 deletions
67
log/log.go
67
log/log.go
|
@ -15,6 +15,7 @@ type (
|
|||
|
||||
Log struct {
|
||||
ID string `json:"id" db:"id"`
|
||||
Level LogLevel `json:"level" db:"level"`
|
||||
Type string `json:"type" db:"type"`
|
||||
Content string `json:"content" db:"content"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
|
@ -35,6 +36,8 @@ const (
|
|||
LEVEL_WARN LogLevel = 1
|
||||
)
|
||||
|
||||
const DEFAULT_LOG_PAGE_LENGTH = 25
|
||||
|
||||
func (self *Logger) Info(logType string, format string, args ...any) {
|
||||
logString := fmt.Sprintf(format, args...)
|
||||
fmt.Printf("[%s] INFO: %s", logType, logString)
|
||||
|
@ -58,21 +61,61 @@ func (self *Logger) Fatal(logType string, format string, args ...any) {
|
|||
// we won't need to push fatal logs to DB, as these usually precede a panic or crash
|
||||
}
|
||||
|
||||
func (self *Logger) Fetch(id string) *Log {
|
||||
// TODO: log fetch
|
||||
return nil
|
||||
func (self *Logger) Fetch(id string) (*Log, error) {
|
||||
log := Log{}
|
||||
err := self.DB.Get(&log, "SELECT * FROM auditlog WHERE id=$1", id)
|
||||
return &log, err
|
||||
}
|
||||
|
||||
func (self *Logger) Search(typeFilters []string, content string, offset int, limit int) []Log {
|
||||
// TODO: log search
|
||||
return []Log{}
|
||||
}
|
||||
func (self *Logger) Search(levelFilters []LogLevel, typeFilters []string, content string, offset int, limit int) ([]*Log, error) {
|
||||
logs := []*Log{}
|
||||
|
||||
func (self *Logger) Delete(id string) error {
|
||||
// TODO: log deletion
|
||||
// consider: logging the deletion of logs?
|
||||
// or just not deleting logs at all
|
||||
return nil
|
||||
params := []any{ limit, offset }
|
||||
conditions := ""
|
||||
|
||||
if len(content) > 0 {
|
||||
content = "%" + content + "%"
|
||||
conditions += " WHERE content LIKE $3"
|
||||
params = append(params, content)
|
||||
}
|
||||
|
||||
if len(levelFilters) > 0 {
|
||||
conditions += " AND level IN ("
|
||||
for i := range levelFilters {
|
||||
conditions += fmt.Sprintf("$%d", len(params) + 1)
|
||||
if i < len(levelFilters) - 1 {
|
||||
conditions += ","
|
||||
}
|
||||
params = append(params, levelFilters[i])
|
||||
}
|
||||
conditions += ")"
|
||||
}
|
||||
|
||||
if len(typeFilters) > 0 {
|
||||
conditions += " AND type IN ("
|
||||
for i := range typeFilters {
|
||||
conditions += fmt.Sprintf("$%d", len(params) + 1)
|
||||
if i < len(typeFilters) - 1 {
|
||||
conditions += ","
|
||||
}
|
||||
params = append(params, typeFilters[i])
|
||||
}
|
||||
conditions += ")"
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(
|
||||
"SELECT * FROM auditlog%s ORDER BY created_at DESC LIMIT $1 OFFSET $2",
|
||||
conditions,
|
||||
)
|
||||
|
||||
// TODO: remove after testing
|
||||
fmt.Println(query)
|
||||
|
||||
err := self.DB.Select(&logs, query, params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return logs, nil
|
||||
}
|
||||
|
||||
func createLog(db *sqlx.DB, logLevel LogLevel, logType string, content string) error {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue