Code:(main.py)
import loggingfrom student import Studentfrom teacher import Teacherdef create(choice):def create_student():print("-----------------------------------------------")f_name = input("Enter First Name: ")l_name = input("Enter Last Name: ")roll_no = int(input("Enter Roll No: "))st1 = Student(f_name, l_name, roll_no)return Nonedef create_teacher():print("-----------------------------------------------")f_name = input("Enter First Name: ")l_name = input("Enter Last Name: ")pay = int(input("Enter Pay Amount: "))st1 = Teacher(f_name, l_name, pay)return Nonechoice_dict = {1: create_student,2: create_teacher}if choice not in choice_dict:raise RuntimeError("{} choice does not exist".format(choice))else:choice_dict[choice]()return Nonewhile True:print("-----------------------------------------------")print("1. Press 1 for Create Student")print("2. Press 2 for Create Teacher")print("3. Press . for Exit")print("-----------------------------------------------")print()choice = input("Enter Your Choice: ")if choice == ".":print("Thank You")breakelse:try:choice = int(choice)create(choice)except Exception as msg:print(msg)
Code:(student.py)
import logginglogger = logging.getLogger(__name__)formatter = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s",datefmt="%a, %d %b %Y %H:%M:%S")file_handler = logging.FileHandler("student.log")file_handler.setFormatter(formatter)logger.setLevel(logging.INFO)logger.addHandler(file_handler)class Student:def __init__(self, firstName, lastName, roll_no):self.firstName = firstNameself.lastName = lastNameself.roll_no = roll_nologger.info(f"Created Student: {self.firstName} {self.lastName} - {self.email}")@propertydef email(self):return "{}.{}@Company.com".format(self.firstName, self.lastName)def __str__(self):return "{} {}".format(self.firstName, self.lastName)
Code:(teacher.py)
import logginglogger = logging.getLogger(__name__)formatter = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s",datefmt="%a, %d %b %Y %H:%M:%S")file_handler = logging.FileHandler("teacher.log")file_handler.setFormatter(formatter)logger.setLevel(logging.INFO)logger.addHandler(file_handler)class Teacher:def __init__(self, firstName, lastName, pay):self.firstName = firstNameself.lastName = lastNameself.pay = paylogger.info(f"Created Teacher: {self.firstName} {self.lastName} - {self.email}")@propertydef email(self):return "{}.{}@Company.com".format(self.firstName, self.lastName)def __str__(self):return "{} {}".format(self.firstName, self.lastName)
Output:(Console)
Output:(Log File)
Sun, 11 Oct 2020 20:18:26 - student - INFO - Created Student: Robert Edison - Robert.Edison@Company.com
Explanation: When user press option 1 for create student, after entering student's first name, last name and roll number it will be logged in student.log file.
Output:(Console)
Output:(Log File)
Sun, 11 Oct 2020 20:20:27 - teacher - INFO - Created Teacher: Client Holland - Client.Holland@Company.com
Explanation:When user press option 2 for create teacher, after entering teacher's first name, last name and payable amount it will be logged in teacher.log file.
If user wants to close the program then just press "." and program will terminate.
This is a very basic and simple project of logging new admission of teacher and student. It will help to understand how logging should be used in day to day life or simple short software application.
Name | Views | Likes |
---|---|---|
Python logging log | 356 | 0 |
Python logging getChild | 1030 | 0 |
Python logging config | 378 | 0 |
Python logging getLogger | 403 | 0 |
Python logging getLevelName | 539 | 0 |
Python Restaurant Management | 559 | 1 |
Python logging info | 514 | 0 |
Python logging makeLogRecord | 556 | 0 |
Python logging getLoggerClass | 466 | 0 |
Python logging getEffectiveLevel | 364 | 0 |
Python logging setLoggerClass | 772 | 0 |
Python logging Logger | 311 | 0 |
Python logging LogRecord | 853 | 0 |
Python logging Introduction | 301 | 0 |
Python logging Filter | 965 | 0 |
Python logging Two Different Files with logging | 2791 | 0 |
Python logging LoggerAdapter | 823 | 0 |
Python logging isEnabledFor | 634 | 0 |
Python logging debug warning error | 270 | 0 |
Python logging basicConfig | 512 | 0 |
Python logging Formatter | 712 | 0 |
Python logging FileHandler | 737 | 0 |
Python logging StreamHandler | 410 | 0 |
Python logging exception | 311 | 0 |
Python logging hasHandlers | 661 | 0 |
Python logging addLevelName | 679 | 0 |
Python logging disable | 1925 | 0 |
Comments