修复log隔天打印日志问题

This commit is contained in:
尹舟 2025-02-05 16:16:44 +08:00
parent 7846f32458
commit 3769b8ab97
3 changed files with 30 additions and 19 deletions

View File

@ -4,6 +4,7 @@ from utils.log import Log
app = Flask(__name__) app = Flask(__name__)
log = Log().getlog()
@app.route('/') @app.route('/')
def index(): def index():
@ -11,8 +12,7 @@ def index():
@app.route('/convert', methods=['POST']) @app.route('/convert', methods=['POST'])
def convert_sql(): def convert_sql():
# 创建一个新的Log实例确保每天创建一个新的日志文件
log = Log().getlog()
sql_input = request.form['sql'] sql_input = request.form['sql']
hologres_connection = request.form['hologresConnection'] hologres_connection = request.form['hologresConnection']
log.info("SQL Input: %s", sql_input) log.info("SQL Input: %s", sql_input)

View File

@ -32,7 +32,10 @@ class Log():
ch = logging.StreamHandler() ch = logging.StreamHandler()
ch.setLevel(logging.INFO) ch.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s] %(filename)s line:%(lineno)d [%(levelname)s]%(message)s') formatter = logging.Formatter(
'[%(asctime)s] %(filename)s line:%(lineno)d [%(levelname)s]%(message)s',
datefmt="%Y-%m-%d %H:%M:%S"
)
fh.setFormatter(formatter) fh.setFormatter(formatter)
ch.setFormatter(formatter) ch.setFormatter(formatter)
@ -40,9 +43,9 @@ class Log():
self.logger.addHandler(ch) self.logger.addHandler(ch)
def getlog(self): def getlog(self):
today = datetime.now().strftime("%Y_%m_%d") current_date = datetime.now().strftime("%Y_%m_%d")
log_date = os.path.basename(self.log_name).split('.')[0] log_date = os.path.basename(self.log_name).split('.')[0]
if today != log_date: if current_date != log_date:
self.update_log_file() self.update_log_file()
return self.logger return self.logger
@ -50,9 +53,17 @@ class Log():
logger = self.getlog() logger = self.getlog()
logger.info(msg, *args, **kwargs) logger.info(msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
logger = self.getlog()
logger.error(msg, *args, **kwargs)
def warning(self, msg, *args, **kwargs):
logger = self.getlog()
logger.warning(msg, *args, **kwargs)
if __name__ == "__main__": if __name__ == "__main__":
log = Log().getlog() log = Log()
log.info("---测试开始----") log.info("---测试开始----")
log.error("操作步骤1,2,3") log.error("操作步骤1,2,3")
log.warning("----测试结束----") log.warning("----测试结束----")

View File

@ -3,9 +3,11 @@ import sqlglot
from sqlglot.expressions import ColumnDef from sqlglot.expressions import ColumnDef
from utils.log import Log from utils.log import Log
def odps(schema,table_name,columns,colmapping,hologres_connection): log = Log().getlog()
odps_sql=f'''
def odps(schema, table_name, columns, colmapping, hologres_connection):
odps_sql = f'''
CREATE EXTERNAL TABLE IF NOT EXISTS {table_name} CREATE EXTERNAL TABLE IF NOT EXISTS {table_name}
( (
{columns} {columns}
@ -22,9 +24,8 @@ TBLPROPERTIES (
''' '''
return odps_sql return odps_sql
def extract_create_table(sql_script): def extract_create_table(sql_script):
# 创建一个新的Log实例确保每天创建一个新的日志文件
log = Log().getlog()
# 解析 SQL 脚本 # 解析 SQL 脚本
parsed = sqlparse.parse(sql_script) parsed = sqlparse.parse(sql_script)
@ -49,9 +50,8 @@ def extract_create_table(sql_script):
return "\n".join(create_table_statements) return "\n".join(create_table_statements)
def parse_create_table_sql(create_table_sql,hologres_connection):
# 创建一个新的Log实例确保每天创建一个新的日志文件 def parse_create_table_sql(create_table_sql, hologres_connection):
log = Log().getlog()
result = extract_create_table(create_table_sql) result = extract_create_table(create_table_sql)
@ -74,12 +74,12 @@ def parse_create_table_sql(create_table_sql,hologres_connection):
# 如果是TEXT类型则转换为STRING # 如果是TEXT类型则转换为STRING
if column_type == 'TEXT': if column_type == 'TEXT':
column_type = 'STRING' column_type = 'STRING'
columns.append(column_name+" "+column_type) columns.append(column_name + " " + column_type)
colmapping.append(column_name+":"+column_name) colmapping.append(column_name + ":" + column_name)
# 将columns,colmapping转换成字符串用,分割 # 将columns,colmapping转换成字符串用,分割
columns_str = ",\n".join(columns) columns_str = ",\n".join(columns)
colmapping_str = ",".join(colmapping) colmapping_str = ",".join(colmapping)
table_name_str=str(table_name).split('.')[-1] table_name_str = str(table_name).split('.')[-1]
schema=str(table_name).split('.')[0] schema = str(table_name).split('.')[0]
return odps(schema,table_name_str,columns_str,colmapping_str,hologres_connection) return odps(schema, table_name_str, columns_str, colmapping_str, hologres_connection)