logging problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: logging problem (/thread-37771.html) |
logging problem - korenron - Jul-20-2022 Hello, I have a problem that I can't seem to understand when using logging with multi files I have created a simple code that ping IP address from a list and save them into a file maybe the structere of the code is wrong ? import os from multiprocessing import Pool import ipaddress import ping3 import logging from logging.handlers import RotatingFileHandler logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s', handlers=[RotatingFileHandler(filename=r'C:\\Test\\logs\\CheckOnLine.log', maxBytes=10000, backupCount=5)]) def get_ip_from_subnet(ip_subnet): ips = ipaddress.ip_network(ip_subnet) ip_list = [str(ip) for ip in ips] return ip_list def CheckOnLine(hostname): DeviceTTl = Getttl(hostname) if 200 > DeviceTTl > 0: message = hostname, " TTL - ", DeviceTTl, ' is up!' print(message) logging.info(message) return True elif DeviceTTl > 240: message = hostname + " TTL more then 240" # print(message) logging.info(message) return False else: # print(hostname, ' OFFLINE!') return False def Getttl(ip): try: result = os.popen("ping -n 1 -w 2000 " + ip).read() # print(result) except Exception as e: print(e) logging.info(e) return -1 else: n = result.find("TTL=") if n > 0: ttl = result[n + 4:] # print(ttl) n = ttl.find("\n") if n > 0: # print(ttl[:n]) return int(ttl[:n]) else: message = ip + ' Offline' print(message) logging.info(message) return -1 def main(): with Pool(50) as p: try: List = p.map(CheckOnLine, TestList) except Exception as t: print('Pool error - ', str(t)) logging.info(t) if __name__ == '__main__': List192 = get_ip_from_subnet('192.168.5.0/24') List101 = get_ip_from_subnet('10.10.2.0/24') List172 = get_ip_from_subnet('172.16.5.0/24') List106 = get_ip_from_subnet('10.68.30.0/24') TestList = List192+ List101 + List172 + List106 main()points: * the name of the file is HowToLog.py * I'm using PyCharm so I need to use "__main__" in otder to make it work with the multiprocessing (if now I'm getting error while try to run) * this is what I get while running this I guess I'm missing something in the setting \ config can someone help \ guide ? how to make this work Thanks, *** I took the same code to run on PI4 (Linux OS) there I removed the __main__ part - and it's working with Pool import os from multiprocessing import Pool import ipaddress #import ping3 import logging from logging.handlers import RotatingFileHandler logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s', handlers=[RotatingFileHandler(filename=r'/home/pi/logs/CheckOnLine.log', maxBytes=100000, backupCount=5)]) def get_ip_from_subnet(ip_subnet): ips = ipaddress.ip_network(ip_subnet) ip_list = [str(ip) for ip in ips] return ip_list def CheckOnLine(hostname): DeviceTTl = Getttl(hostname) try: if 200 > DeviceTTl > 0: message = hostname + " TTL - " + str(DeviceTTl) + ' is up!' print(message) logging.info(message) return True elif DeviceTTl > 240: message = hostname + " TTL more then 240" print(message) logging.info(message) return False else: # print(hostname, ' OFFLINE!') return False except TypeError as Te1: print(hostname + ' ' + str(Te1)) except Exception as e: print(hostname + ' ' + str(e)) def Getttl(ip): try: result = os.popen("ping -c 1 -w 2 " + ip).read() # print(result) except Exception as e: print(e) logging.info(e) return -1 else: n = result.find("ttl=") if n > 0: ttl = result[n + 4:] # print(ttl) n = ttl.find(" ") if n > 0: # print(ttl[:n]) return int(ttl[:n]) else: message = ip + ' Offline' print(message) logging.info(message) return -1 List192 = get_ip_from_subnet('192.168.5.0/24') List101 = get_ip_from_subnet('10.10.2.0/24') List172 = get_ip_from_subnet('172.16.5.0/24') List106 = get_ip_from_subnet('10.68.30.0/24') TestList = List199 + List109 + List136 + List137 with Pool(50) as p: try: List = p.map(CheckOnLine, TestList) except Exception as t: print('Pool error - ', str(t)) logging.info(t) logging.info('done!')but the logs are not OK I get 5 logs files but the order of the data is wrong - and also I don't have all the data in each file I only have 5-6 lines while when in the cmd I can see all the address 4*255 (1024) so what am I missing \ doing wrong with the logging? Thanks , **final update when I manually write to a file using with open(Data.log, 'a') as my_file: my_file.write(messge'\n\r') my_file.close()after the logging.info(messge) I get all the data correct so I'm pretty sure I'm missing something in the logging config |