Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MySql API Connector: How to disable logging

Tags:

python

mysql

I am using the MySQL API in Python Version 8.0.31). This library writes to my logs/terminal, which I don't like for security and clarity reasons.
How do I disable the logging of the MySQL connector?

Log example:

2022-12-30 21:03:59,287 # _do_auth(): user: test
2022-12-30 21:03:59,287 # _do_auth(): self._auth_plugin: mysql_native_password
2022-12-30 21:03:59,287 # _do_auth(): user: test
2022-12-30 21:03:59,288 # _do_auth(): password: test
2022-12-30 21:03:59,288 package: mysql.connector.plugins
2022-12-30 21:03:59,288 plugin_name: mysql_native_password
2022-12-30 21:03:59,290 AUTHENTICATION_PLUGIN_CLASS: MySQLNativePasswordAuthPlugin
2022-12-30 21:03:59,290 new_auth_plugin: mysql_native_password
2022-12-30 21:03:59,292 package: mysql.connector.plugins
2022-12-30 21:03:59,292 plugin_name: caching_sha2_password

Usage:

import mysql.connector
...
self.sql_con = mysql.connector.connect(host=self._Host,
                                                    database=self._DataBase,
                                                    user=self._User,
                                                    password=self._Password,
                                                    auth_plugin='mysql_native_password')

In the rest of my project I am using the "standard" logging tool:

import logging
from logging.config import fileConfig
...

if LogToFile == True:
    self.path = "./logs/"
    if DebugMode == False:       
        self.filename = "app"
        self.save_old()
    else:
        self.filename = "SoftwareTestLog"
   
    logging.basicConfig(filename=self.path+self.filename+'.log', filemode='w', 
                        level=logging.DEBUG,format='%(asctime)-8s %(message)s')   
    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)                     
else:     
    logging.basicConfig(level=logging.DEBUG,format='%(asctime)-8s %(message)s')   
            
self.mylogger = logging.getLogger(__name__)
...
myLogger.info("doing logs blabla")

I checked the API doc here and here I only found the function log() in the ShellAPI.

like image 884
theLastDay Avatar asked Jun 05 '26 23:06

theLastDay


1 Answers

I fixed the problem

logging.getLogger("mysql.connector").setLevel(logging.WARNING)
self.sql_con = mysql.connector.connect(host=self._Host,
                                        database=self._DataBase,
                                        user=self._User,
                                        password=self._Password,                                                             
                                        auth_plugin='mysql_native_password')
like image 165
theLastDay Avatar answered Jun 08 '26 11:06

theLastDay