Reputation: 371
I have written a class like below:
class Logger:
@staticmethod
def get_timestamp():
import datetime
return datetime.datetime.utcnow()
def print_log(self,color, write_level, msg):
return color
def log_level_print(self,log_level, write_level, msg):
if log_level == 'ERROR':
return print_log(bcolors.FAIL, write_level, msg)
if log_level == 'WARN':
return print_log(bcolors.WARNING, write_level, msg)
if log_level == 'INFO':
return print_log(bcolors.OKGREEN, write_level, msg)
if log_level == 'DEBUG':
return print_log(bcolors.OKBLUE, write_level, msg)
else:
print(f"{Logger.get_timestamp()} {bcolors.FAIL}: Invalid LOG type{bcolors.ENDC}")
return
Here, I am using this class :
from logger import Logger
demo = Logger()
print(demo.log_level_print('ERROR','ssdsd','sdsdsd'))
I am not able to call this function, I'm getting an error:
NameError: name 'print_log' is not defined
Upvotes: 3
Views: 4141
Reputation: 1196
You forgot to add self
as the first argument of class' methods and when you use the methods. Corrected code:
class Logger:
@staticmethod
def get_timestamp():
import datetime
return datetime.datetime.utcnow()
def print_log(self, color, write_level, msg):
return color
def log_level_print(self, log_level, write_level, msg):
if log_level == 'ERROR':
return self.print_log(bcolors.FAIL, write_level, msg)
if log_level == 'WARN':
return self.print_log(bcolors.WARNING, write_level, msg)
if log_level == 'INFO':
return self.print_log(bcolors.OKGREEN, write_level, msg)
if log_level == 'DEBUG':
return self.print_log(bcolors.OKBLUE, write_level, msg)
else:
print(f"{Logger.get_timestamp()} {bcolors.FAIL}: Invalid LOG type{bcolors.ENDC}")
return
Look, it's a code I'm running:
demo = Logger()
print(demo.log_level_print('ERROR','ssdsd','sdsdsd'))
and this is a result:
Upvotes: 5
Reputation: 291
I think you're missing a self
.
All (EDIT: non static, nor abstract) class methods should have a parameter which is typically named self
, like so:
class Logger():
@staticmethod
def get_timestamp():
...
def print_log(self, color, write_level, msg):
...
def log_level_print(self, log_level, write_level, msg):
...
demo = Logger()
print(demo.log_level_print('ERROR','ssdsd','sdsdsd'))
Upvotes: 2
Reputation: 579
As mentioned you need to call self
and scondly it will still throw error if bcolors
is not specified. You need to call bcolors
. I am assuming that it is some another dependency which you have not specified.
Here is a sample.
class Logger:
@staticmethod
def get_timestamp(self):
import datetime
return datetime.datetime.utcnow()
def print_log(self, color, write_level, msg):
return color
def log_level_print(self, log_level, write_level, msg):
bcolors = [] # temporary empty list
if log_level == 'ERROR':
return self.print_log(bcolors.FAIL, write_level, msg)
if log_level == 'WARN':
return self.print_log(bcolors.WARNING, write_level, msg)
if log_level == 'INFO':
return self.print_log(bcolors.OKGREEN, write_level, msg)
if log_level == 'DEBUG':
return self.print_log(bcolors.OKBLUE, write_level, msg)
else:
print(f"{Logger.get_timestamp()} {bcolors.FAIL}: Invalid LOG type{bcolors.ENDC}")
return
demo = Logger()
print(demo.log_level_print('ERROR','ssdsd','sdsdsd'))
Upvotes: 1