Skip to content

Commit 9de4a95

Browse files
committed
Working logging levels and command line options
1 parent f80f0f9 commit 9de4a95

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

students/visokoo/lesson02/assignment/src/charges_calc.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,38 @@
1010

1111

1212
def init_logging(logging_level):
13-
""" Setting up the logger """
14-
# if lvl 1 = only show error msgs on console and in file
15-
# if lvl 2 = only show debug and warnings
16-
# if lvl 3 = show all msgs
17-
# if lvl 0 = show nothing and output nothing
18-
logging.config.fileConfig(
19-
fname='logging.conf', disable_existing_loggers=False)
13+
""" Setting up the logger
14+
lvl 1 = only show error msgs on console and in file
15+
lvl 2 = only show error and warnings on console and in file
16+
lvl 3 = show all msgs on console and in file
17+
lvl 0 = show nothing and output nothing
18+
"""
2019
logger = logging.getLogger(__name__)
20+
log_format = (
21+
"%(asctime)s %(filename)s:%(lineno)-3d %(levelname)s %(message)s")
22+
log_file = datetime.datetime.now().strftime("%Y-%m-%d")+'.log'
23+
formatter = logging.Formatter(log_format)
24+
file_handler = logging.FileHandler(log_file)
25+
file_handler.setFormatter(formatter)
26+
stream_handler = logging.StreamHandler()
27+
stream_handler.setFormatter(formatter)
28+
29+
if logging_level == 0:
30+
logger.disabled = True
31+
elif logging_level == 1:
32+
file_handler.setLevel(logging.ERROR)
33+
stream_handler.setLevel(logging.ERROR)
34+
logger.addHandler(file_handler)
35+
logger.addHandler(stream_handler)
36+
elif logging_level == 2:
37+
file_handler.setLevel(logging.WARNING)
38+
stream_handler.setLevel(logging.WARNING)
39+
logger.addHandler(file_handler)
40+
logger.addHandler(stream_handler)
41+
elif logging_level == 3:
42+
logging.config.fileConfig(
43+
fname='logging.conf', disable_existing_loggers=False)
44+
2145
return logger
2246

2347

0 commit comments

Comments
 (0)