|
a |
|
b/qiita_db/logger.py |
|
|
1 |
r""" |
|
|
2 |
Logging objects (:mod: `qiita_db.logger`) |
|
|
3 |
==================================== |
|
|
4 |
|
|
|
5 |
..currentmodule:: qiita_db.logger |
|
|
6 |
|
|
|
7 |
This module provides objects for recording log information |
|
|
8 |
|
|
|
9 |
Classes |
|
|
10 |
------- |
|
|
11 |
|
|
|
12 |
..autosummary:: |
|
|
13 |
:toctree: generated/ |
|
|
14 |
|
|
|
15 |
LogEntry |
|
|
16 |
""" |
|
|
17 |
|
|
|
18 |
# ----------------------------------------------------------------------------- |
|
|
19 |
# Copyright (c) 2014--, The Qiita Development Team. |
|
|
20 |
# |
|
|
21 |
# Distributed under the terms of the BSD 3-clause License. |
|
|
22 |
# |
|
|
23 |
# The full license is in the file LICENSE, distributed with this software. |
|
|
24 |
# ----------------------------------------------------------------------------- |
|
|
25 |
from json import loads, dumps |
|
|
26 |
|
|
|
27 |
import qiita_db as qdb |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
class LogEntry(qdb.base.QiitaObject): |
|
|
31 |
""" |
|
|
32 |
Attributes |
|
|
33 |
---------- |
|
|
34 |
severity |
|
|
35 |
time |
|
|
36 |
info |
|
|
37 |
msg |
|
|
38 |
|
|
|
39 |
Methods |
|
|
40 |
------- |
|
|
41 |
clear_info |
|
|
42 |
add_info |
|
|
43 |
""" |
|
|
44 |
|
|
|
45 |
_table = 'logging' |
|
|
46 |
|
|
|
47 |
@classmethod |
|
|
48 |
def newest_records(cls, numrecords=100): |
|
|
49 |
"""Return a list of the newest records in the logging table |
|
|
50 |
|
|
|
51 |
Parameters |
|
|
52 |
---------- |
|
|
53 |
numrecords : int, optional |
|
|
54 |
The number of records to return. Default 100 |
|
|
55 |
|
|
|
56 |
Returns |
|
|
57 |
------- |
|
|
58 |
list of LogEntry objects |
|
|
59 |
list of the log entries |
|
|
60 |
""" |
|
|
61 |
with qdb.sql_connection.TRN: |
|
|
62 |
sql = """SELECT logging_id |
|
|
63 |
FROM qiita.{0} |
|
|
64 |
ORDER BY logging_id DESC LIMIT %s""".format(cls._table) |
|
|
65 |
qdb.sql_connection.TRN.add(sql, [numrecords]) |
|
|
66 |
|
|
|
67 |
return [cls(i) |
|
|
68 |
for i in qdb.sql_connection.TRN.execute_fetchflatten()] |
|
|
69 |
|
|
|
70 |
@classmethod |
|
|
71 |
def create(cls, severity, msg, info=None): |
|
|
72 |
"""Creates a new LogEntry object |
|
|
73 |
|
|
|
74 |
Parameters |
|
|
75 |
---------- |
|
|
76 |
severity : str {Warning, Runtime, Fatal} |
|
|
77 |
The level of severity to use for the LogEntry. Refers to an entry |
|
|
78 |
in the SEVERITY table. |
|
|
79 |
msg : str |
|
|
80 |
The message text |
|
|
81 |
info : dict, optional |
|
|
82 |
Defaults to ``None``. If supplied, the information will be added |
|
|
83 |
as the first entry in a list of information dicts. If ``None``, |
|
|
84 |
an empty dict will be added. |
|
|
85 |
|
|
|
86 |
Notes |
|
|
87 |
----- |
|
|
88 |
- When `info` is added, keys can be of any type, but upon retrieval, |
|
|
89 |
they will be of type str |
|
|
90 |
""" |
|
|
91 |
if info is None: |
|
|
92 |
info = {} |
|
|
93 |
|
|
|
94 |
info = dumps([info]) |
|
|
95 |
|
|
|
96 |
with qdb.sql_connection.TRN: |
|
|
97 |
sql = """INSERT INTO qiita.{} (time, severity_id, msg, information) |
|
|
98 |
VALUES (NOW(), %s, %s, %s) |
|
|
99 |
RETURNING logging_id""".format(cls._table) |
|
|
100 |
severity_id = qdb.util.convert_to_id(severity, "severity") |
|
|
101 |
qdb.sql_connection.TRN.add(sql, [severity_id, msg, info]) |
|
|
102 |
|
|
|
103 |
return cls(qdb.sql_connection.TRN.execute_fetchlast()) |
|
|
104 |
|
|
|
105 |
@property |
|
|
106 |
def severity(self): |
|
|
107 |
"""Returns the severity_id associated with this LogEntry |
|
|
108 |
|
|
|
109 |
Returns |
|
|
110 |
------- |
|
|
111 |
int |
|
|
112 |
This is a key to the SEVERITY table |
|
|
113 |
""" |
|
|
114 |
with qdb.sql_connection.TRN: |
|
|
115 |
sql = """SELECT severity_id FROM qiita.{} |
|
|
116 |
WHERE logging_id = %s""".format(self._table) |
|
|
117 |
qdb.sql_connection.TRN.add(sql, [self.id]) |
|
|
118 |
return qdb.sql_connection.TRN.execute_fetchlast() |
|
|
119 |
|
|
|
120 |
@property |
|
|
121 |
def time(self): |
|
|
122 |
"""Returns the time that this LogEntry was created |
|
|
123 |
|
|
|
124 |
Returns |
|
|
125 |
------- |
|
|
126 |
datetime |
|
|
127 |
""" |
|
|
128 |
with qdb.sql_connection.TRN: |
|
|
129 |
sql = "SELECT time FROM qiita.{} WHERE logging_id = %s".format( |
|
|
130 |
self._table) |
|
|
131 |
qdb.sql_connection.TRN.add(sql, [self.id]) |
|
|
132 |
|
|
|
133 |
return qdb.sql_connection.TRN.execute_fetchlast() |
|
|
134 |
|
|
|
135 |
@property |
|
|
136 |
def info(self): |
|
|
137 |
"""Returns the info associated with this LogEntry |
|
|
138 |
|
|
|
139 |
Returns |
|
|
140 |
------- |
|
|
141 |
list of dict |
|
|
142 |
Each entry in the list is information that was added (the info |
|
|
143 |
added upon creation will be index 0, and if additional info |
|
|
144 |
was supplied subsequently, those entries will occupy subsequent |
|
|
145 |
indices) |
|
|
146 |
|
|
|
147 |
Notes |
|
|
148 |
----- |
|
|
149 |
- When `info` is added, keys can be of any type, but upon retrieval, |
|
|
150 |
they will be of type str |
|
|
151 |
""" |
|
|
152 |
with qdb.sql_connection.TRN: |
|
|
153 |
sql = """SELECT information FROM qiita.{} WHERE |
|
|
154 |
logging_id = %s""".format(self._table) |
|
|
155 |
qdb.sql_connection.TRN.add(sql, [self.id]) |
|
|
156 |
|
|
|
157 |
rows = qdb.sql_connection.TRN.execute_fetchlast() |
|
|
158 |
|
|
|
159 |
if rows: |
|
|
160 |
results = loads(rows) |
|
|
161 |
else: |
|
|
162 |
results = {} |
|
|
163 |
|
|
|
164 |
return results |
|
|
165 |
|
|
|
166 |
@property |
|
|
167 |
def msg(self): |
|
|
168 |
"""Gets the message text for this LogEntry |
|
|
169 |
|
|
|
170 |
Returns |
|
|
171 |
------- |
|
|
172 |
str |
|
|
173 |
""" |
|
|
174 |
with qdb.sql_connection.TRN: |
|
|
175 |
sql = "SELECT msg FROM qiita.{0} WHERE logging_id = %s".format( |
|
|
176 |
self._table) |
|
|
177 |
qdb.sql_connection.TRN.add(sql, [self.id]) |
|
|
178 |
|
|
|
179 |
return qdb.sql_connection.TRN.execute_fetchlast() |
|
|
180 |
|
|
|
181 |
def clear_info(self): |
|
|
182 |
"""Resets the list of info dicts to be an empty list |
|
|
183 |
""" |
|
|
184 |
sql = """UPDATE qiita.{} SET information = %s |
|
|
185 |
WHERE logging_id = %s""".format(self._table) |
|
|
186 |
qdb.sql_connection.perform_as_transaction(sql, [dumps([]), self.id]) |
|
|
187 |
|
|
|
188 |
def add_info(self, info): |
|
|
189 |
"""Adds new information to the info associated with this LogEntry |
|
|
190 |
|
|
|
191 |
Parameters |
|
|
192 |
---------- |
|
|
193 |
info : dict |
|
|
194 |
The information to add. |
|
|
195 |
|
|
|
196 |
Notes |
|
|
197 |
----- |
|
|
198 |
- When `info` is added, keys can be of any type, but upon retrieval, |
|
|
199 |
they will be of type str |
|
|
200 |
""" |
|
|
201 |
current_info = self.info |
|
|
202 |
current_info.append(info) |
|
|
203 |
new_info = dumps(current_info) |
|
|
204 |
|
|
|
205 |
sql = """UPDATE qiita.{} SET information = %s |
|
|
206 |
WHERE logging_id = %s""".format(self._table) |
|
|
207 |
qdb.sql_connection.perform_as_transaction(sql, [new_info, self.id]) |