для модуля
logger_class.py
Код: Выделить всё
class Logger(object):
"""A file-based message logger with the following properties
Attributes:
file_name: a string representing the full path of the log file to which this logger will write it messages
"""
def __init__(self, file_name):
"""Return a logger object whose file_name is * file_name*"""
self.file_name = file_name
def _write_log(self, level, msg):
"""Writes a message to the file_name for a specific Logger instance"""
with open(self.file_name, 'a') as log_file:
log_file.write('[{0} {1}\n'.format(level, msg))
def critical(self, level, msg):
self._write_log('CRITICAL', msg)
def error(self, level, msg):
self._write_log(self, level, msg)
def warn(self, level, msg):
self._write_log('WARN', msg)
def info(self, level, msg):
self._write_log('INFO', msg)
def debug(self, level, msg):
self._write_log('DEBUG', msg)
Код: Выделить всё
from logger_class import Logger
logger_object = Logger('/var/log/class_logger.log')
logger_object.info('this is an info message')
Когда я запускаю код из new_script_with_logger.py я получил сообщение об ошибке от PyCharm IDE
В консольной команде, запускающей его
Код: Выделить всё
python new_script_with_logger.py
Код: Выделить всё
Traceback (most recent call last):
File "C:\Users\Python Design Pattern\Practical_Design\Practical_Python_Design_PaTTERN\new_script_with_logger.py", line 4, in
logger_object.info('this is an info message')
TypeError: Logger.info() missing 1 required positional argument: 'msg'
Объяснение того, как работает код
Подробнее здесь: https://stackoverflow.com/questions/790 ... gger-class