From 3769b8ab973ea3e13a4d99d79be5180aee38d015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E8=88=9F?= <13007110208@163.com> Date: Wed, 5 Feb 2025 16:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dlog=E9=9A=94=E5=A4=A9?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=97=A5=E5=BF=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqllineage.py | 4 ++-- utils/log.py | 21 ++++++++++++++++----- utils/sql_parse.py | 24 ++++++++++++------------ 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/sqllineage.py b/sqllineage.py index e754d4c..6235e35 100644 --- a/sqllineage.py +++ b/sqllineage.py @@ -4,6 +4,7 @@ from utils.log import Log app = Flask(__name__) +log = Log().getlog() @app.route('/') def index(): @@ -11,8 +12,7 @@ def index(): @app.route('/convert', methods=['POST']) def convert_sql(): - # 创建一个新的Log实例,确保每天创建一个新的日志文件 - log = Log().getlog() + sql_input = request.form['sql'] hologres_connection = request.form['hologresConnection'] log.info("SQL Input: %s", sql_input) diff --git a/utils/log.py b/utils/log.py index 3650a39..4b64d58 100644 --- a/utils/log.py +++ b/utils/log.py @@ -32,7 +32,10 @@ class Log(): ch = logging.StreamHandler() 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) ch.setFormatter(formatter) @@ -40,9 +43,9 @@ class Log(): self.logger.addHandler(ch) 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] - if today != log_date: + if current_date != log_date: self.update_log_file() return self.logger @@ -50,9 +53,17 @@ class Log(): logger = self.getlog() 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__": - log = Log().getlog() + log = Log() log.info("---测试开始----") log.error("操作步骤1,2,3") - log.warning("----测试结束----") + log.warning("----测试结束----") \ No newline at end of file diff --git a/utils/sql_parse.py b/utils/sql_parse.py index c032677..92aef17 100644 --- a/utils/sql_parse.py +++ b/utils/sql_parse.py @@ -3,9 +3,11 @@ import sqlglot from sqlglot.expressions import ColumnDef 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} ( {columns} @@ -22,9 +24,8 @@ TBLPROPERTIES ( ''' return odps_sql + def extract_create_table(sql_script): - # 创建一个新的Log实例,确保每天创建一个新的日志文件 - log = Log().getlog() # 解析 SQL 脚本 parsed = sqlparse.parse(sql_script) @@ -49,9 +50,8 @@ def extract_create_table(sql_script): return "\n".join(create_table_statements) -def parse_create_table_sql(create_table_sql,hologres_connection): - # 创建一个新的Log实例,确保每天创建一个新的日志文件 - log = Log().getlog() + +def parse_create_table_sql(create_table_sql, hologres_connection): result = extract_create_table(create_table_sql) @@ -74,12 +74,12 @@ def parse_create_table_sql(create_table_sql,hologres_connection): # 如果是TEXT类型,则转换为STRING if column_type == 'TEXT': column_type = 'STRING' - columns.append(column_name+" "+column_type) - colmapping.append(column_name+":"+column_name) + columns.append(column_name + " " + column_type) + colmapping.append(column_name + ":" + column_name) # 将columns,colmapping转换成字符串用,分割 columns_str = ",\n".join(columns) colmapping_str = ",".join(colmapping) - table_name_str=str(table_name).split('.')[-1] - schema=str(table_name).split('.')[0] + table_name_str = str(table_name).split('.')[-1] + schema = str(table_name).split('.')[0] - return odps(schema,table_name_str,columns_str,colmapping_str,hologres_connection) \ No newline at end of file + return odps(schema, table_name_str, columns_str, colmapping_str, hologres_connection)