Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a Log file
#1
Hi,
im new in this coding thing and im creating a "program" that Ping some Ip's that I have on a list
and I would like that when the program ends pinging, it saves the information/results in a eventlog file text in order to save later that info in a Table

this is the code that im using
import os
import csv
import time
import datetime
import logging

def check_ping(hostname):
    response = os.system("fping -r 10 -q " + hostname + " >/dev/null")
    if response == 0:
        check_ping = "[OK]"
    else:
        check_ping = "[Error]"
 
    return check_ping

with open('ip-source.txt') as file:
    dump = file.read()
    dump = dump.splitlines()

    
    for ip in dump:
        
        os.system('cls')
        print('Pinging now:', ip)
        print('-'*60)
        os.system('ping -n 2 {}'.format(ip))

        print('-'*60)
        time.sleep(5)
 


log = logging.getLogger('Loggername')
log.setLevel(logging.DEBUG)

# How the information is presented. You probably only want the message part, squiggly brackets included.
formatter = logging.Formatter('{message}', style="{")  

filehandler = logging.FileHandler('filename.txt', encoding='utf-8')
filehandler.setFormatter(formatter)
filehandler.setLevel(logging.DEBUG)
log.addHandler(filehandler)

ip = "192.168.100.5"
name = "LaptopEdd"

log.info(f'{time.time():<15}|IP:{ip:>15}|Equipo:{name:>5}')
log.debug('message')
# There are more iirc, like log.critical('message')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Choices from .ods file columns cspower 3 518 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,782 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 572 Nov-14-2023, 06:53 AM
Last Post: thomaskissas33
  Create csv file with 4 columns for process mining thomaskissas33 3 691 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  create exe file for linux? korenron 2 908 Mar-22-2023, 01:42 PM
Last Post: korenron
  my first file won't create itself MehHz2526 2 858 Nov-27-2022, 12:58 AM
Last Post: MehHz2526
  Create multiple/single csv file for each sql records mg24 6 1,319 Sep-29-2022, 08:06 AM
Last Post: buran
Sad pandas writer create "corrupted" file freko75 1 2,735 Jun-14-2022, 09:57 PM
Last Post: snippsat
  create new log file on logging? korenron 6 2,202 Mar-22-2022, 07:14 AM
Last Post: korenron
  Create zip file from the BLOB column in ORACLE DB nnsatpute 2 1,876 Dec-31-2021, 11:00 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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