1
1
from gevent import Greenlet , GreenletExit
2
+ from gevent .thread import get_ident
2
3
import datetime
3
4
import logging
4
5
import traceback
@@ -54,7 +55,7 @@ def id(self):
54
55
@property
55
56
def ident (self ):
56
57
"""Compatibility with threading interface. A small, unique non-negative integer that identifies this object."""
57
- return self . minimal_ident
58
+ return get_ident ( self )
58
59
59
60
@property
60
61
def state (self ):
@@ -124,7 +125,7 @@ def terminate(self):
124
125
125
126
126
127
class ThreadLogHandler (logging .Handler ):
127
- def __init__ (self , thread = None , dest = None ):
128
+ def __init__ (self , thread = None , dest = None , level = logging . WARNING ):
128
129
"""Set up a log handler that appends messages to a list.
129
130
130
131
This log handler will first filter by ``thread``, if one is
@@ -141,17 +142,17 @@ def __init__(self, thread=None, dest=None):
141
142
lot of log messages, you may run into memory problems.
142
143
"""
143
144
logging .Handler .__init__ (self )
145
+ self .setLevel (level )
144
146
self .thread = thread
145
- self .dest = dest if dest else []
147
+ self .dest = dest if dest is not None else []
146
148
self .addFilter (self .check_thread )
147
149
148
150
def check_thread (self , record ):
149
151
"""Determine if a thread matches the desired record"""
150
152
if self .thread is None :
151
153
return 1
152
154
153
- thread_ident = getattr (record .thread , "ident" , None )
154
- if record .thread == self .thread .ident :
155
+ if get_ident () == get_ident (self .thread ):
155
156
return 1
156
157
return 0
157
158
@@ -162,6 +163,7 @@ def emit(self, record):
162
163
for k in ["created" , "levelname" , "levelno" , "lineno" , "filename" ]:
163
164
record_dict [k ] = getattr (record , k )
164
165
self .dest .append (record_dict )
166
+ print (self .thread .log )
165
167
# FIXME: make sure this doesn't become a memory disaster!
166
168
# We probably need to check the size of the list...
167
169
# TODO: think about whether any of the keys are security flaws
0 commit comments