Skip to content

Commit

Permalink
add log config set_log
Browse files Browse the repository at this point in the history
  • Loading branch information
snower committed Feb 17, 2017
1 parent 5d6be50 commit cc48c66
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions tormysql/__init__.py
Expand Up @@ -29,6 +29,7 @@
from .pool import ConnectionPool
from .cursor import CursorNotReadAllDataError, CursorNotIterError
from .pool import ConnectionPoolClosedError, ConnectionPoolUsedError, ConnectionNotFoundError, ConnectionNotUsedError, ConnectionUsedError, WaitConnectionTimeoutError
from .log import set_log
from . import helpers

version = "0.3.2"
Expand Down
4 changes: 2 additions & 2 deletions tormysql/helpers.py
Expand Up @@ -2,9 +2,9 @@
# 16/3/25
# create by: snower

import logging
from tornado import gen
from .pool import ConnectionPool as BaseConnectionPool
from . import log

class TransactionClosedError(Exception):
pass
Expand Down Expand Up @@ -54,7 +54,7 @@ def rollback(self):

def __del__(self):
if self._connection:
logging.warning("Transaction has not committed or rollbacked %s.", self._connection)
log.get_log().warning("Transaction has not committed or rollbacked %s.", self._connection)
self._connection.do_close()
self._connection = None

Expand Down
14 changes: 14 additions & 0 deletions tormysql/log.py
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
# 17/2/17
# create by: snower

import logging

_log = logging

def get_log():
return _log

def set_log(log):
global _log
_log = log
14 changes: 8 additions & 6 deletions tormysql/pool.py
Expand Up @@ -7,12 +7,12 @@
'''

import time
import logging
from collections import deque
from tornado.concurrent import Future
from tornado.ioloop import IOLoop
from pymysql._compat import text_type
from .client import Client
from . import log


class ConnectionPoolClosedError(Exception):
Expand Down Expand Up @@ -250,7 +250,7 @@ def connection_close_callback(self, connection):
self._connections.remove(connection)
self._connections_count -= 1
except ValueError:
logging.warning("Close unknown Connection %s.", connection)
log.get_log().warning("Close unknown Connection %s.", connection)
if self._close_future and not self._used_connections and not self._connections:
IOLoop.current().add_callback(self._close_future.set_result, None)
self._close_future = None
Expand Down Expand Up @@ -299,14 +299,16 @@ def check_idle_connections(self):
if now - connection.used_time > (self._wait_connection_timeout * 4) ** 2:
connection.do_close()
if self._debug_connection_used:
logging.error("Connection used timeout close, used time %.2fs %s %s.\n%s", now - connection.used_time, connection, self, connection.get_last_query_sql())
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())
else:
logging.error("Connection used timeout close, used time %.2fs %s %s.", now - connection.used_time, connection, self)
log.get_log().error("Connection used timeout close, used time %.2fs %s %s.", now - connection.used_time, connection, self)
elif now - connection.used_time > self._wait_connection_timeout ** 2 * 2:
if self._debug_connection_used:
logging.warning("Connection maybe not release, used time %.2fs %s %s.\n%s", now - connection.used_time, connection, self, connection.get_last_query_sql())
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())
else:
logging.warning("Connection maybe not release, used time %.2fs %s %s.", now - connection.used_time, connection, self)
log.get_log().warning("Connection maybe not release, used time %.2fs %s %s.", now - connection.used_time, connection, self)
elif self._debug_connection_used:
log.get_log().warning("Connection used time %.2fs %s %s.\n%s", now - connection.used_time, connection, self, connection.get_last_query_sql())

next_check_time = now + self._idle_seconds
for connection in tuple(self._connections):
Expand Down

0 comments on commit cc48c66

Please sign in to comment.