generated from unit-team-spbu/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
56 lines (47 loc) · 1.63 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from nameko_mongodb import MongoDatabase
from nameko.rpc import rpc
from nameko.web.handlers import http
import json
from datetime import datetime
class Logger:
"""Microservice for system logging"""
# Vars
name = 'logger'
db = MongoDatabase()
# Logic
def _save_log(self, log):
collection = self.db["logs"]
collection.insert_one(
{"date": log['time'], "service": log["service"], "method": log["method"],
"arguments": log["args"], "state": log["state"], "message": log["message"]}
)
# API
@rpc
def log(self, service, method, arguments, state, message):
"""
Args:
service (str) - service's name,
method (str) - method's name,
arguments (json serializable!! list in case of many, None or single arg) - method's arguments,
state (str) - error or OK
message (str) - info about log
"""
log = {"time": str(datetime.now()), "service": service, "method": method, "args": arguments,
"state": state, "message": message}
self._save_log(log)
@http('GET', '/full_logs')
def get_logs_handler(self, request):
cursor = self.db["logs"].find(
{}, {"_id": 0,})
logs = list()
for row in cursor:
logs.append(row)
return 200, json.dumps(logs)
@http('GET', '/short_logs')
def get_logs_handler(self, request):
cursor = self.db["logs"].find(
{}, {"_id": 0, "arguments": 0})
logs = list()
for row in cursor:
logs.append(row)
return 200, json.dumps(logs)