Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
logging problem
#1
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

Error:
--- Logging error --- Traceback (most recent call last): File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\logging\handlers.py", line 70, in emit self.doRollover() File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\logging\handlers.py", line 171, in doRollover self.rotate(self.baseFilename, dfn) File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\logging\handlers.py", line 111, in rotate os.rename(source, dest) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Test\\logs\\CheckOnLine.log' -> 'C:\\Test\\logs\\CheckOnLine.log.1' Call stack: File "<string>", line 1, in <module> File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py", line 116, in spawn_main exitcode = _main(fd, parent_sentinel) File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py", line 129, in _main return self._bootstrap(parent_sentinel) File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py", line 315, in _bootstrap self.run() File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py", line 108, in run self._target(*self._args, **self._kwargs) File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\pool.py", line 125, in worker result = (True, func(*args, **kwds)) File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\pool.py", line 48, in mapstar return list(map(*args)) File "C:\Users\David\PycharmProjects\pythonProject4\HowToLog.py", line 22, in CheckOnLine DeviceTTl = Getttl(hostname) File "C:\Users\David\PycharmProjects\pythonProject4\HowToLog.py", line 58, in Getttl logging.info(message) Message: '10.10.2.76 Offline' Arguments: () Process finished with exit code -1
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020