5

Please suggest what I have done wrong, I am new to coding.

1. first.py file:

import logging
class Log:
    def __init__(self,msg):
        self.msg = msg

    def debug(self,msg):
        FORMAT  = "%(asctime)-15s%(message)s"
        logging.basicConfig(filemode = "w", filename="file.log", format = FORMAT, level=logging.DEBUG)
        logging.debug(self.msg)

2. second.py file

import first
first.Log.debug("I am in debug mode")

When I run second.py file than I am getting error as Logging.Log.debug("I am in debug mode")

TypeError: debug() missing 1 required positional argument: 'msg'** 
rpanai
  • 12,515
  • 2
  • 42
  • 64
san
  • 81
  • 1
  • 1
  • 8

1 Answers1

2

I'm not sure what you are trying to do, but the first issue is that you need to initialize an instance of Log with the msg argument provided. What you are doing here first.Log.debug("I am in debug mode") is calling the debug method of Log without creating an instance.

In your debug method the msg argument is required, but it's never used. Instead, the method will simply try to fetch self.msg defined at __init__.

One way this code would work:

1. first.py file

import logging
class Log:
    def __init__(self,msg):
        self.msg = msg

    def debug(self):
        FORMAT  = "%(asctime)-15s%(message)s"
        logging.basicConfig(filemode = "w", filename="file.log", format = FORMAT, level=logging.DEBUG)
        logging.debug(self.msg)

2. second.py file

import first
# Note that Log is initialized with the msg you want, and only then we call debug()
first.Log("I am in debug mode").debug()
ODiogoSilva
  • 2,394
  • 1
  • 19
  • 20