audit log basic db implementation
This commit is contained in:
parent
e80a6753a5
commit
aa144b719a
4 changed files with 62 additions and 8 deletions
38
log/log.go
38
log/log.go
|
@ -22,17 +22,35 @@ type (
|
|||
)
|
||||
|
||||
const (
|
||||
TYPE_ACCOUNT = "account"
|
||||
TYPE_ACCOUNT string = "account"
|
||||
TYPE_MUSIC string = "music"
|
||||
TYPE_BLOG string = "blog"
|
||||
TYPE_ARTWORK string = "artwork"
|
||||
TYPE_MISC string = "misc"
|
||||
)
|
||||
|
||||
type LogLevel int
|
||||
const (
|
||||
LEVEL_INFO LogLevel = 0
|
||||
LEVEL_WARN LogLevel = 1
|
||||
)
|
||||
|
||||
func (self *Logger) Info(logType string, format string, args ...any) {
|
||||
fmt.Printf(fmt.Sprintf("[%s] INFO: %s", logType, format), args...)
|
||||
// TODO: push logs to DB
|
||||
logString := fmt.Sprintf(format, args...)
|
||||
fmt.Printf("[%s] INFO: %s", logType, logString)
|
||||
err := createLog(self.DB, LEVEL_INFO, logType, logString)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to push log to database: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Logger) Warn(logType string, format string, args ...any) {
|
||||
fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] WARN: %s", logType, format), args...)
|
||||
// TODO: push logs to DB
|
||||
logString := fmt.Sprintf(format, args...)
|
||||
fmt.Fprintf(os.Stderr, "[%s] WARN: %s", logType, logString)
|
||||
err := createLog(self.DB, LEVEL_WARN, logType, logString)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to push log to database: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Logger) Fatal(logType string, format string, args ...any) {
|
||||
|
@ -56,3 +74,13 @@ func (self *Logger) Delete(id string) error {
|
|||
// or just not deleting logs at all
|
||||
return nil
|
||||
}
|
||||
|
||||
func createLog(db *sqlx.DB, logLevel LogLevel, logType string, content string) error {
|
||||
_, err := db.Exec(
|
||||
"INSERT INTO auditlog (level, type, content) VALUES ($1,$2,$3)",
|
||||
logLevel,
|
||||
logType,
|
||||
content,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue