I try to write a error or info logs to a log file. Here is the basicConfig
logging.basicConfig(
filename="script_log.log",
encoding="utf-8",
filemode="a",
format="{asctime} - {levelname} - {message}",
style="{",
datefmt="%Y-%m-%d %H:%M",
)
If I write script as below, the warning message is written in the script_log.log file without issue.
def getCertList(ip_address, username, password, file_path,prompt='admin:'):
# Connect to Server
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address, username=username, password=password, look_for_keys=False,
allow_agent=False, timeout=30)
interact = SSHClientInteraction(ssh_client, timeout=360,
display=False) # "display=True" is just to show you what script does in real time. While in production you can set it to False
interact.expect(prompt)
except:
logging.warning(f"Unable to connect to server {ip_address}, check network connectivity please.", exc_info=True)
If at the end of the script, everything is working, I would like to write a info logging like this, it won’t write the line in the log file.
logging.info("All Information has been collected sucessfully. ")
Did I miss anything?
I tried to use with open to open the script_log.log file and write the line, it worked, so it should be the logging.info problem, but I could not figure out.
user28623026 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3