Skip to content

Commit cc48c66

Browse files
committed
add log config set_log
1 parent 5d6be50 commit cc48c66

4 files changed

Lines changed: 25 additions & 8 deletions

File tree

tormysql/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .pool import ConnectionPool
3030
from .cursor import CursorNotReadAllDataError, CursorNotIterError
3131
from .pool import ConnectionPoolClosedError, ConnectionPoolUsedError, ConnectionNotFoundError, ConnectionNotUsedError, ConnectionUsedError, WaitConnectionTimeoutError
32+
from .log import set_log
3233
from . import helpers
3334

3435
version = "0.3.2"

tormysql/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# 16/3/25
33
# create by: snower
44

5-
import logging
65
from tornado import gen
76
from .pool import ConnectionPool as BaseConnectionPool
7+
from . import log
88

99
class TransactionClosedError(Exception):
1010
pass
@@ -54,7 +54,7 @@ def rollback(self):
5454

5555
def __del__(self):
5656
if self._connection:
57-
logging.warning("Transaction has not committed or rollbacked %s.", self._connection)
57+
log.get_log().warning("Transaction has not committed or rollbacked %s.", self._connection)
5858
self._connection.do_close()
5959
self._connection = None
6060

tormysql/log.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
# 17/2/17
3+
# create by: snower
4+
5+
import logging
6+
7+
_log = logging
8+
9+
def get_log():
10+
return _log
11+
12+
def set_log(log):
13+
global _log
14+
_log = log

tormysql/pool.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
'''
88

99
import time
10-
import logging
1110
from collections import deque
1211
from tornado.concurrent import Future
1312
from tornado.ioloop import IOLoop
1413
from pymysql._compat import text_type
1514
from .client import Client
15+
from . import log
1616

1717

1818
class ConnectionPoolClosedError(Exception):
@@ -250,7 +250,7 @@ def connection_close_callback(self, connection):
250250
self._connections.remove(connection)
251251
self._connections_count -= 1
252252
except ValueError:
253-
logging.warning("Close unknown Connection %s.", connection)
253+
log.get_log().warning("Close unknown Connection %s.", connection)
254254
if self._close_future and not self._used_connections and not self._connections:
255255
IOLoop.current().add_callback(self._close_future.set_result, None)
256256
self._close_future = None
@@ -299,14 +299,16 @@ def check_idle_connections(self):
299299
if now - connection.used_time > (self._wait_connection_timeout * 4) ** 2:
300300
connection.do_close()
301301
if self._debug_connection_used:
302-
logging.error("Connection used timeout close, used time %.2fs %s %s.\n%s", now - connection.used_time, connection, self, connection.get_last_query_sql())
302+
log.get_log().error("Connection used timeout close, used time %.2fs %s %s.\n%s", now - connection.used_time, connection, self, connection.get_last_query_sql())
303303
else:
304-
logging.error("Connection used timeout close, used time %.2fs %s %s.", now - connection.used_time, connection, self)
304+
log.get_log().error("Connection used timeout close, used time %.2fs %s %s.", now - connection.used_time, connection, self)
305305
elif now - connection.used_time > self._wait_connection_timeout ** 2 * 2:
306306
if self._debug_connection_used:
307-
logging.warning("Connection maybe not release, used time %.2fs %s %s.\n%s", now - connection.used_time, connection, self, connection.get_last_query_sql())
307+
log.get_log().warning("Connection maybe not release, used time %.2fs %s %s.\n%s", now - connection.used_time, connection, self, connection.get_last_query_sql())
308308
else:
309-
logging.warning("Connection maybe not release, used time %.2fs %s %s.", now - connection.used_time, connection, self)
309+
log.get_log().warning("Connection maybe not release, used time %.2fs %s %s.", now - connection.used_time, connection, self)
310+
elif self._debug_connection_used:
311+
log.get_log().warning("Connection used time %.2fs %s %s.\n%s", now - connection.used_time, connection, self, connection.get_last_query_sql())
310312

311313
next_check_time = now + self._idle_seconds
312314
for connection in tuple(self._connections):

0 commit comments

Comments
 (0)