!610 add log_dir for restful app.py

From: @zhoupengcheng11 
Reviewed-by: @yangyongguang, @gaoruoshu 
Signed-off-by: @gaoruoshu
This commit is contained in:
openeuler-ci-bot
2024-03-26 08:31:37 +00:00
committed by Gitee
7 changed files with 38 additions and 13 deletions

View File

@@ -474,6 +474,7 @@ System is the parameter information required for system optimization. You must m
**Log information**
Change the log level as required. The default log level is info. Log information is recorded in the **/var/log/messages** file.
You can modify the configuration item log_dir to specify the log directory.
**Monitor information**
@@ -553,6 +554,8 @@ Tuning is the parameter information required for offline tuning.
[log]
# either "debug", "info", "warn", "error", "critical", default is "info"
level = info
# you can set log_dir to specify the location where the log is written. default location is /var/log/messages
# log_dir = "/var/log/atune"
#################################### monitor ###############################
[monitor]
@@ -651,6 +654,8 @@ You can modify the startup configuration as required.
[log]
# either "debug", "info", "warn", "error", "critical", default is "info"
level = info
# you can set log_dir to specify the location where the log is written. default location is /var/log/messages
# log_dir = "/var/log/atune"
#################################### database ###############################
[database]

View File

@@ -450,7 +450,8 @@ system为系统执行相关的优化需要用到的参数信息必须根据
**日志信息**
根据情况修改日志的级别默认为info级别日志信息打印在/var/log/messages中。
根据情况修改日志的级别默认为info级别日志信息默认打印在/var/log/messages中。
可以通过打开配置文件的log_dir选项指定日志写入的目录
**monitor信息**
@@ -531,6 +532,8 @@ tuning为系统进行离线调优时需要用到的参数信息。
[log]
# either "debug", "info", "warn", "error", "critical", default is "info"
level = info
# you can set log_dir to specify the location where the log is written. default location is /var/log/messages
# log_dir = "/var/log/atune"
#################################### monitor ###############################
[monitor]
@@ -629,7 +632,9 @@ A-Tune engine配置文件/etc/atuned/engine.cnf的配置项说明如下
[log]
# either "debug", "info", "warn", "error", "critical", default is "info"
level = info
# you can set log_dir to specify the location where the log is written. default location is /var/log/messages
# log_dir = "/var/log/atune"
#################################### database ###############################
[database]
# enable database server

View File

@@ -15,6 +15,7 @@
Flask application initialization, including log configuration, restful api registration.
"""
import os
import datetime
import ssl
import logging
from logging.handlers import SysLogHandler
@@ -35,26 +36,32 @@ class App:
self.api = Api(self.app)
@staticmethod
def config_log(level):
def config_log(level, log_dir):
"""app config log"""
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
logging_format = logging.Formatter('atuned: %(asctime)s [%(levelname)s] '
'%(module)s [%(pathname)s:%(lineno)d] : %(message)s')
syslog_handler = SysLogHandler(address="/dev/log", facility=SysLogHandler.LOG_LOCAL0)
syslog_handler.setFormatter(logging_format)
find_custom_suit_log_dir = None
log_handler = None
if log_dir is not None:
log_file = os.path.join(log_dir, f"atune-{datetime.date.today().strftime('%m%d')}")
if "/var/log/atune" in log_file:
log_handler = logging.FileHandler(log_file)
find_custom_suit_log_dir = log_file
if not find_custom_suit_log_dir:
log_handler = SysLogHandler(address="/dev/log", facility=SysLogHandler.LOG_LOCAL0)
log_handler.setFormatter(logging.Formatter('atuned: %(asctime)s [%(levelname)s] '
'%(module)s [%(pathname)s:%(lineno)d] : %(message)s'))
root_logger = logging.getLogger()
root_logger.setLevel(level)
root_logger.addHandler(syslog_handler)
root_logger.addHandler(log_handler)
def add_resource(self):
"""flask app add resource"""
def startup_app(self, host, port, tls, cert_file, key_file, ca_file, log_level):
def startup_app(self, host, port, tls, cert_file, key_file, ca_file, log_level, log_dir):
"""start flask app"""
level = logging.getLevelName(log_level.upper())
self.config_log(level)
self.config_log(level, log_dir)
self.add_resource()
context = None
if tls:

View File

@@ -46,7 +46,7 @@ def main(filename):
app_engine.startup_app(EngineConfig.engine_host, EngineConfig.engine_port,
EngineConfig.engine_tls,
EngineConfig.engine_server_cert, EngineConfig.engine_server_key,
EngineConfig.engine_ca_file, EngineConfig.level)
EngineConfig.engine_ca_file, EngineConfig.level, EngineConfig.log_dir)
if __name__ == '__main__':

View File

@@ -42,7 +42,7 @@ def main(filename):
app_engine = AppRest()
app_engine.startup_app(AtunedConfig.rest_host, AtunedConfig.rest_port, AtunedConfig.rest_tls,
AtunedConfig.rest_server_cert, AtunedConfig.rest_server_key,
AtunedConfig.rest_ca_file, AtunedConfig.level)
AtunedConfig.rest_ca_file, AtunedConfig.level, AtunedConfig.log_dir)
if __name__ == '__main__':

View File

@@ -47,6 +47,7 @@ class AtunedConfig:
engine_client_cert = ''
engine_client_key = ''
level = ''
log_dir = None
module = ''
disk = ''
network = ''
@@ -99,6 +100,9 @@ class AtunedConfig:
'tlsengineclientkeyfile',
ENGINE_CERT_PATH + 'client.key')
AtunedConfig.level = get_or_default(config, 'log', 'level', 'info')
AtunedConfig.log_dir = get_or_default(config, 'log', 'log_dir', None)
if AtunedConfig.log_dir:
AtunedConfig.log_dir.strip('"')
AtunedConfig.module = get_or_default(config, 'monitor', 'module', 'mem_topo, cpu_topo')
AtunedConfig.disk = get_or_default(config, 'system', 'disk', 'sda')
AtunedConfig.network = get_or_default(config, 'system', 'network', 'enp5s0')

View File

@@ -31,6 +31,7 @@ class EngineConfig:
engine_server_cert = ''
engine_server_key = ''
level = ''
log_dir = None
db_enable = False
database = ''
db_host = ''
@@ -60,6 +61,9 @@ class EngineConfig:
EngineConfig.engine_server_key = get_or_default(config, 'server',
'tlsengineserverkeyfile', ENGINE_CERT_PATH + 'server.key')
EngineConfig.level = get_or_default(config, 'log', 'level', 'info')
EngineConfig.log_dir = get_or_default(config, 'log', 'level', None)
if EngineConfig.log_dir:
EngineConfig.log_dir..strip('"')
EngineConfig.db_enable = get_or_default_bool(config, 'database', 'db_enable', False)
if EngineConfig.db_enable:
EngineConfig.database = get_or_default(config, 'database', 'database', 'PostgreSQL')