-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
283 lines (237 loc) · 8.71 KB
/
index.html
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Django-mongolog by gnulnx</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/github-dark.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="javascripts/respond.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!--[if lt IE 8]>
<link rel="stylesheet" href="stylesheets/ie.css">
<![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
</head>
<body>
<div id="header">
<nav>
<li class="fork"><a href="https://github.com/gnulnx/django-mongolog">View On GitHub</a></li>
<li class="downloads"><a href="https://github.com/gnulnx/django-mongolog/zipball/master">ZIP</a></li>
<li class="downloads"><a href="https://github.com/gnulnx/django-mongolog/tarball/master">TAR</a></li>
<li class="title">DOWNLOADS</li>
</nav>
</div><!-- end header -->
<div class="wrapper">
<section>
<div id="title">
<h1>Django-mongolog</h1>
<p>A Simple Mongo Based Logger for Django</p>
<hr>
<span class="credits left">Project maintained by <a href="https://github.com/gnulnx">gnulnx</a></span>
<span class="credits right">Hosted on GitHub Pages — Theme by <a href="https://twitter.com/michigangraham">mattgraham</a></span>
</div>
<h1>
<a id="mongolog" class="anchor" href="#mongolog" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>MongoLog</h1>
<p>MongoLog is a simple Mongo based log handler that can be easly used with
standard python/django logging.</p>
<p>Please visit the <a href="https://groups.google.com/forum/#!forum/mongolog-users">MongoLog Users
Group</a> with any
questions/suggestions. Thanks.</p>
<h2>
<a id="quick-start" class="anchor" href="#quick-start" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Quick start</h2>
<ol>
<li>
<p>Add "mongolog" to your INSTALLED_APPS like this
: </p>
<pre><code>INSTALLED_APPS = (
...
'mongolog',
)
</code></pre>
</li>
<li>
<p>Add the SimpleMongoLogHandler to your LOGGING config.
: </p>
<pre><code>LOGGING = {
'version': 1,
'handlers': {
'mongolog': {
'level': 'DEBUG',
'class': 'mongolog.SimpleMongoLogHandler',
'connection': 'mongodb://localhost:27017'
},
},
'loggers': {
'': {
'handlers': ['mongolog'],
'level': 'DEBUG',
'propagate': True
},
},
}
</code></pre>
</li>
</ol>
<p>3) Start your management shell:</p>
<pre><code> ./manage.py shell
</code></pre>
<p>4) Create a couple of log entries
: </p>
<pre><code> import logging
import pymongo
logger = logging.getLogger(__name__)
One of the cool things about mongolog is that it can log complex
data structures in a way that makes them both human parsable and
queryable. So for instance if we create the following log
message:
# Pro Tip: You can copy and paste all of this
LOG_MSG = {
'test': True,
'test class': 'TestBaseMongoLogHandler',
'Life': {
'Domain': {
'Bacteria': [
{
'name': ValueError, # intentional bad value
'description': 'Just a bad description'
}
],
'Archaea': [],
'Eukaryota': [
{
'name': 'Excavata',
'description': 'Various flagellate protozoa',
},
{
'name': 'Amoebozoa',
'descritpion': 'most lobose amoeboids and slime moulds',
},
{
'name': 'Opisthokonta',
'description': 'animals, fungi, choanoflagellates, etc.',
},
]
}
}
}
Now let's log our message at each of the defined log levels...
logger.debug(LOG_MSG)
logger.info(LOG_MSG)
logger.warn(LOG_MSG)
logger.error(LOG_MSG)
try:
raise ValueError("Bad Value")
except ValueError as e:
logger.exception(LOG_MSG)
raise
</code></pre>
<p>5) Now log into your mongo shell and look at some results
: </p>
<pre><code> ./mongo
use mongolog
db.mongolog.findOne({'level': "INFO"})
Will produde a mongo document like:
{
"_id" : ObjectId("5664a22bdd162ca58f0693d2"),
"name" : "__builtin__",
"thread" : NumberLong("140735229362944"),
"level" : "INFO",
"process" : 42383,
"module" : "<console>",
"filename" : "<console>",
"func" : "<module>",
"time" : ISODate("2015-12-06T21:01:31.258Z"),
"msg" : {
"test" : true,
"Life" : {
"Domain" : {
"Eukaryota" : [
{
"name" : "Excavata",
"description" : "Various flagellate protozoa"
},
{
"name" : "Amoebozoa",
"descritpion" : "most lobose amoeboids and slime moulds"
},
{
"name" : "Opisthokonta",
"description" : "animals, fungi, choanoflagellates, etc."
}
],
"Archaea" : [ ],
"Bacteria" : [
{
"name" : "<type 'exceptions.ValueError'>",
"description" : "Just a bad description"
}
]
}
},
"test class" : "TestBaseMongoLogHandler"
},
"path" : "<console>",
"line" : 1
}
Take a look at the "msg" section and you will notice that all of
the information from our LOG\_MSG is contained under that key in
standard mongo data structures. This means that we can query
based on our log message. For example in your mongo shell try
the following queries:
// Find all documents logged with a 'test' key
> db.mongolog.find({'msg.test': {$exists: true}}).count()
5
// Find all documents that have a Eukaryota name in the list of ["Amoebozoa", "Opisthokonta"]
> db.mongolog.find({'msg.Life.Domain.Eukaryota.name': {$in: ["Amoebozoa", "Opisthokonta"]}}).count()
1
// Same as above but only those documents logged at level INFO
>db.mongolog.find({
'level': 'INFO',
'msg.Life.Domain.Eukaryota.name': {$in: ["Amoebozoa", "Opisthokonta"]},
}).count()
1
// And again at level ERROR.
>db.mongolog.find({
'level': 'INFO',
'msg.Life.Domain.Eukaryota.name': {$in: ["Amoebozoa", "Opisthokonta"]},
}).count()
2
// Notice that now two records are returned. This is because
// logger.exception(...) also logs at level ERROR, but also notice that if when we
// pretty print the records...
>db.mongolog.find({
'level': 'ERROR',
'msg.Life.Domain.Eukaryota.name': {$in: ["Amoebozoa", "Opisthokonta"]},
}).pretty()
// ...that one of the entries has exception info. When running in a real environment
// and not the console the 'trace' section will be populated with the full stack trace.
"exception" : {
"info" : [
"<type 'exceptions.ValueError'>",
"Bad Value",
"<traceback object at 0x106853b90>"
],
"trace" :
null
}
</code></pre>
<h2>
<a id="future-roadmap" class="anchor" href="#future-roadmap" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Future Roadmap</h2>
<p>Currently mongolog has pretty solid support for logging arbitrary
datastructures. If it finds an object it doesn't know how to natively
serialize it will try to convert it to str().</p>
<p>The next steps are to create a set of most used query operations for
probing the log.</p>
<p>Please give a shout out with
<a href="https://groups.google.com/forum/#!forum/mongolog-users">feedback</a> and
feature requests.</p>
<p>Thanks</p>
</section>
</div>
<!--[if !IE]><script>fixScale(document);</script><![endif]-->
</body>
</html>