|
1
|
|
|
package models |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"fmt" |
|
6
|
|
|
"github.com/memnix/memnixrest/pkg/database" |
|
7
|
|
|
"time" |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
type Log struct { |
|
11
|
|
|
Type LogType `json:"type"` |
|
12
|
|
|
Message string `json:"message"` |
|
13
|
|
|
Event LogEvent `json:"event"` |
|
14
|
|
|
CreatedAt time.Time `json:"createdat"` |
|
15
|
|
|
UserID uint `json:"userid"` |
|
16
|
|
|
DeckID uint `json:"deckid"` |
|
17
|
|
|
CardID uint `json:"cardid"` |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// SendLog sends log to the rabbitmq channel |
|
21
|
|
|
func (l *Log) SendLog() error { |
|
22
|
|
|
jsonObject, _ := l.ToJSON() |
|
23
|
|
|
key := fmt.Sprintf("%s.%s", l.Type, l.Event) |
|
24
|
|
|
|
|
25
|
|
|
err := database.SendMessageToChannel(database.RabbitMqChan, jsonObject, key) |
|
26
|
|
|
return err |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// Set Log |
|
30
|
|
|
func (l *Log) Set(logType LogType, message string, event LogEvent, userID, deckID, cardID uint) { |
|
31
|
|
|
l.Type = logType |
|
32
|
|
|
l.Message = message |
|
33
|
|
|
l.Event = event |
|
34
|
|
|
l.UserID = userID |
|
35
|
|
|
l.CardID = cardID |
|
36
|
|
|
l.DeckID = deckID |
|
37
|
|
|
l.CreatedAt = time.Now() |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// CreateLog returns a new Log object |
|
41
|
|
|
func CreateLog(message string, event LogEvent) *Log { |
|
42
|
|
|
return &Log{Message: message, Event: event, CreatedAt: time.Now()} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// SetType method |
|
46
|
|
|
func (l *Log) SetType(logType LogType) *Log { |
|
47
|
|
|
l.Type = logType |
|
48
|
|
|
return l |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// AttachIDs method |
|
52
|
|
|
func (l *Log) AttachIDs(userID, deckID, cardID uint) *Log { |
|
53
|
|
|
if userID != 0 { |
|
54
|
|
|
l.UserID = userID |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if cardID != 0 { |
|
58
|
|
|
l.CardID = cardID |
|
59
|
|
|
} |
|
60
|
|
|
if deckID != 0 { |
|
61
|
|
|
l.DeckID = deckID |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return l |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// ToJSON method |
|
68
|
|
|
func (l *Log) ToJSON() ([]byte, error) { |
|
69
|
|
|
body, err := json.Marshal(l) |
|
70
|
|
|
if err != nil { |
|
71
|
|
|
return nil, err |
|
72
|
|
|
} |
|
73
|
|
|
return body, nil |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
type LogType string |
|
77
|
|
|
|
|
78
|
|
|
const ( |
|
79
|
|
|
LogTypeInfo LogType = "info" |
|
80
|
|
|
LogTypeWarning LogType = "warning" |
|
81
|
|
|
LogTypeError LogType = "error" |
|
82
|
|
|
) |
|
83
|
|
|
|
|
84
|
|
|
// LogEvent enum type |
|
85
|
|
|
type LogEvent string |
|
86
|
|
|
|
|
87
|
|
|
const ( |
|
88
|
|
|
LogUndefined LogEvent = "undefined" |
|
89
|
|
|
LogUserLogin LogEvent = "user.login" |
|
90
|
|
|
LogUserLogout LogEvent = "user.logout" |
|
91
|
|
|
LogUserRegister LogEvent = "user.register" |
|
92
|
|
|
LogUserEdit LogEvent = "user.edit" |
|
93
|
|
|
LogUserDeleted LogEvent = "user.deleted" |
|
94
|
|
|
LogSubscribe LogEvent = "user.subscribe" |
|
95
|
|
|
LogUnsubscribe LogEvent = "user.unsubscribe" |
|
96
|
|
|
LogUserDeckLimit LogEvent = "user.deckLimit" |
|
97
|
|
|
LogPublishRequest LogEvent = "deck.publish" |
|
98
|
|
|
LogDeckCreated LogEvent = "deck.created" |
|
99
|
|
|
LogDeckDeleted LogEvent = "deck.deleted" |
|
100
|
|
|
LogDeckEdited LogEvent = "deck.edited" |
|
101
|
|
|
LogDeckCardLimit LogEvent = "deck.cardLimit" |
|
102
|
|
|
LogCardCreated LogEvent = "card.created" |
|
103
|
|
|
LogCardDeleted LogEvent = "card.deleted" |
|
104
|
|
|
LogCardEdited LogEvent = "card.edited" |
|
105
|
|
|
LogAlreadyUsedEmail LogEvent = "register.usedEmail" |
|
106
|
|
|
LogIncorrectEmail LogEvent = "login.incorrectEmail" |
|
107
|
|
|
LogIncorrectPassword LogEvent = "login.incorrectPassword" |
|
108
|
|
|
LogLoginError LogEvent = "login.error" |
|
109
|
|
|
LogPermissionForbidden LogEvent = "permission.forbidden" |
|
110
|
|
|
LogQueryGetError LogEvent = "query.get" |
|
111
|
|
|
LogBodyParserError LogEvent = "query.bodyParser" |
|
112
|
|
|
LogBadRequest LogEvent = "query.badRequest" |
|
113
|
|
|
) |
|
114
|
|
|
|