import mysql.connector
import config
import threading

enabled = False
connected = False

db_lock = threading.Lock()


def log(message, kind):
    if enabled:
        with db_lock:
            global conn, cur, connected
            if not connected:
                conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
                cur = conn.cursor()
                connected = True
            cur.execute('INSERT INTO vkbot_logmessage VALUES (NULL, %s, %s, NOW())', (message, kind))
            conn.commit()

import mysql.connector
import config
import threading

enabled = False

db_lock = threading.Lock()
conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
cur = conn.cursor()

def log(message, kind):
    if enabled:
        with db_lock:
            cur.execute('INSERT INTO vkbot_logmessage VALUES (NULL, %s, %s, NOW())', (message, kind))
            conn.commit()

--- 
+++ 
@@ -3,13 +3,18 @@
 import threading
 
 enabled = False
+connected = False
 
 db_lock = threading.Lock()
-conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
-cur = conn.cursor()
+
 
 def log(message, kind):
     if enabled:
         with db_lock:
+            global conn, cur, connected
+            if not connected:
+                conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
+                cur = conn.cursor()
+                connected = True
             cur.execute('INSERT INTO vkbot_logmessage VALUES (NULL, %s, %s, NOW())', (message, kind))
             conn.commit()
Connect to MySQL only when needed
