Commit 549c20a5 authored by Mr Phong's avatar Mr Phong

add log level config

parent 796be4e3
......@@ -65,7 +65,7 @@ Chứa toàn bộ file log thực thi, mỗi thiết bị là 1 file với tên
### 5. File config.py
Về cơ bản có thể chạy các ứng dụng với các thông số mặc định. Nếu người dùng cần tùy chỉnh các thông số thì điều chỉnh ở file này. File này chứa cấu hình đường dẫn các file trên, có thể đổi tên file hay đường dẫn nếu muốn. Ngoài ra có format ghi file log, có thể điều chỉnh thêm thời gian ghi log nếu muốn bằng cách chỉnh lại định dạng log format (tham khảo thêm tại https://realpython.com/python-logging/)
Về cơ bản có thể chạy các ứng dụng với các thông số mặc định. Nếu người dùng cần tùy chỉnh các thông số thì điều chỉnh ở file này. File này chứa cấu hình đường dẫn các file trên, có thể đổi tên file hay đường dẫn nếu muốn. Ngoài ra có format, level và định dạng ghi file log (tham khảo thêm tại https://realpython.com/python-logging/)
## III. Cài đặt và chạy ứng dụng
......
......@@ -3,23 +3,8 @@ from typing import List
import sys
from netmiko import ConnectHandler, BaseConnection, SSHDetect
from pandas import read_csv, DataFrame
from config import host_file, command_file, log_file, output_dir, log_fomartter
from config import host_file, command_file, log_file, output_dir, log_level, log_fomartter
import logging
import re
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
fomartter = logging.Formatter(log_fomartter)
stream = logging.StreamHandler()
stream.setFormatter(fomartter)
logger.addHandler(stream)
file = logging.FileHandler(log_file)
file.setFormatter(fomartter)
logger.addHandler(file)
def run_commands(connection: BaseConnection, df_command: DataFrame):
......@@ -44,11 +29,24 @@ def run_commands(connection: BaseConnection, df_command: DataFrame):
logger.info(output)
logger = logging.getLogger(__name__)
logger.setLevel(log_level)
fomartter = logging.Formatter(log_fomartter)
stream = logging.StreamHandler()
stream.setFormatter(fomartter)
logger.addHandler(stream)
file = logging.FileHandler(log_file)
file.setFormatter(fomartter)
logger.addHandler(file)
logger.info("Start !")
df_host = read_csv(host_file)
df_command = read_csv(command_file)
with open(log_file, 'w') as file_output:
file_output.truncate()
with open(log_file, 'a') as file_output:
#file_output.truncate()
sys.stdout = file_output
for index, row in df_host.iterrows():
......@@ -72,3 +70,5 @@ with open(log_file, 'w') as file_output:
logger.error(ex)
sys.stdout.close()
logger.info("End !")
......@@ -2,6 +2,7 @@ host_file = "hosts.txt"
command_file = "commands.txt"
log_file = "log.txt"
output_dir = "./output/"
log_fomartter = "%(message)s"
# Nếu muốn thêm thời gian thì dùng format bên dưới
# log_fomartter = "%(asctime)s:%(message)s"
\ No newline at end of file
log_level = "ERROR" # DEBUG < INFO < WARNING < ERROR < CRITICAL < FATAL
#log_fomartter = "%(message)s"
# Nếu muốn thêm thời gian và loglevel thì dùng format bên dưới
log_fomartter = "%(asctime)s:%(levelname)s:%(message)s"
Connect to host: 42.117.236.56
Connect to host : 42.117.236.56 success !
/ip hotspot walled-garden
2022-06-03 13:44:53,627:INFO:Connect to host: 42.117.236.56
2022-06-03 13:44:54,203:INFO:Connect to host : 42.117.236.56 success !
2022-06-03 13:44:54,250:INFO:/ip hotspot walled-garden
[admin@MikroTik] /ip hotspot walled-garden>
add dst-host=awifi.vn action=all
2022-06-03 13:44:54,297:INFO:add dst-host=awifi.vn action=all
[admin@MikroTik] /ip hotspot walled-garden>
Connect to host: 172.16.20.2
Connect to host : 172.16.20.2 success !
ERROR when execute command with device 172.16.20.2
2022-06-03 13:44:54,300:INFO:Connect to host: 172.16.20.2
2022-06-03 13:44:57,454:INFO:Connect to host : 172.16.20.2 success !
2022-06-03 13:45:09,685:ERROR:ERROR when execute command with device 172.16.20.2
2022-06-03 13:45:09,685:ERROR:
Pattern not detected: 'walled-garden>' in output.
Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.
You can also look at the Netmiko session_log or debug log for more information.
2022-06-03 13:45:51,485:INFO:Connect to host: 42.117.236.56
2022-06-03 13:45:52,084:INFO:Connect to host : 42.117.236.56 success !
2022-06-03 13:45:52,270:INFO:/ip hotspot walled-garden
[admin@MikroTik] /ip hotspot walled-garden>
2022-06-03 13:45:52,317:INFO:add dst-host=awifi.vn action=all
[admin@MikroTik] /ip hotspot walled-garden>
2022-06-03 13:45:52,319:INFO:Connect to host: 172.16.20.2
2022-06-03 13:45:55,427:INFO:Connect to host : 172.16.20.2 success !
2022-06-03 13:46:07,687:ERROR:ERROR when execute command with device 172.16.20.2
2022-06-03 13:46:07,687:ERROR:
Pattern not detected: 'walled-garden>' in output.
Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.
You can also look at the Netmiko session_log or debug log for more information.
2022-06-03 13:50:14,307:ERROR:ERROR when execute command with device 172.16.20.2
2022-06-03 13:50:14,308:ERROR:
Pattern not detected: 'walled-garden>' in output.
Things you might try to fix this:
......
AWING-AP-TEST*>
AWING-AP-TEST*>terminal width 512
no page
/ip hotspot walled-garden
AWING-AP-TEST*>no page
/ip hotspot walled-garden
AWING-AP-TEST*>/ip hotspot walled-garden
^
% Invalid input detected 9999 at '^' marker.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment