Question

Write a decorator, called mylogging, that can be used to add a logging entry to a...

Write a decorator, called mylogging, that can be used to add a logging entry to a log file every time the decorated function is called. The log entry should include the function name and the values of the arguments (both positional and keyword) that are passed to the function. Test the code by using the decorator an a function and looking at the entries it creates in the log file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# module to log the data to file
import logging
# logging debug level to file
# basically there are five levels
# ERROR
# DEBUG
# WARNING
# INFO
# CRITICAL
logging.basicConfig(filename="week10.log",level=logging.DEBUG)
# decrator function to log files
def mylogging(func):
   def inner(arg1,arg2):
       dct = locals()
       argument_one_name = [ k for k,v in dct.items() if v == arg1][0]
       argument_two_name = [ k for k,v in dct.items() if v == arg2][0]
       logging.debug("Argument {}:{} and Argument {}:{} and function:{}".format(argument_one_name,arg1,argument_two_name,arg2,func.__name__))
       func(arg1,arg2)
   return inner
# calling decrator to log files
@mylogging
def myfunction(arg1,arg2):
   pass
# calling function to log things
myfunction(10,20)
myfunction("csk","rcb")
myfunction("srh","mi")
myfunction("rr","dc")

Add a comment
Know the answer?
Add Answer to:
Write a decorator, called mylogging, that can be used to add a logging entry to a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

  • General Requirements . . . Write a program that reads letters from a file called "letters.txt"....

    General Requirements . . . Write a program that reads letters from a file called "letters.txt". Your program will open the letters.txt file read in one character at a time For this assignment the test file will contain at least 30 letters, uppercase OR lowercase In your program you will change each letter (solution) to uppercase Use the function toupper in #include <ctype.h> You create a numerical version of the uppercase letter from the file . o Use int numberSolution...

  • 1. Write CppUnitLite tests to verify correct behavior for all the exercises. Using C++ 2. Please...

    1. Write CppUnitLite tests to verify correct behavior for all the exercises. Using C++ 2. Please show all outputs. Write functions to add one day, another function to add one month, and yet another function to add one year to a Date struct. struct Date { int year; int month; int day; }; Pass Dates by reference when appropriate (i.e., Date& or const Date&). For example, the following function returns by value a new Date instance with one day added...

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • In Python and in one file please. (Simple functions with an expressions) Create a function called...

    In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...

  • C++ Write a program with the following elements: in main() -opens the 2 files provided for...

    C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...

  • I need to write a function in C# that would validated user entry for Payment Code...

    I need to write a function in C# that would validated user entry for Payment Code in the form. The form is used to collect information about payments. User can enter ad many rows/entries as he/she wishes by clicking "Add another Payment" button on the buttom. There is a Payment Code field that needs to be validates. It's a drop down list, with many values but the user is eligible to select only two of them: Meal and Travel. Every...

  • In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filenam...

    In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...

  • 1- Write a program that creates a dictionary of the English words for all single digit...

    1- Write a program that creates a dictionary of the English words for all single digit numbers as follows: 1: 'one', 2: 'two', 3: 'three', etc. 2- Write a function called numConvert that receives the above dictionary and an integer number as parameters, and prints the English words for all the number's digits. For example, if the function receives 2311, it will print: two three one one 3- Test your program by adding a main function, which calls numConvert. 4-...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT